@(#)Is the process alive 30 AUG 2000 Rob Thomas robt@cymru.com How to check the state of another process from within a C program. Is the process still alive? From within a program, you may wish to determine if a corollary process is running. Within a shell script or Perl, this can be accomplished with the use of ps(1). However, it is less than efficient to use popen() or (the infamous and evil) system() within a C program. Here is a simple solution using, of all things, kill(): /* * @(#)isit.c Rob Thomas robt@cymru.com http://www.cymru.com/~robt * A program that determines if a given PID is still receiving signals, * e.g. "alive." * Usage: ./isit * */ #include #include #include #include extern int errno; int main(int argc, char **argv) { int ret_val; pid_t mypid; mypid = atoi(argv[1]); ret_val = kill(mypid, 0); if (0 != ret_val) { /* Look for ESRCH */ if (3 == errno) { fprintf(stderr, "Proc %d not alive.\n", mypid); exit(1); } else { perror("kill"); } } fprintf(stdout, "Proc %d still active.\n", mypid); exit(0); } By simply sending a signal 0 to the process, we can determine if the process still exists. Remember that permissions still count with signals, and only root can send a signal to any process. This snippet of code can be cut and pasted into any application for which process monitoring is necessary. -- Rob Thomas, robt@cymru.com http://www.cymru.com/~robt