Is there a system call to check the status of any processes?

wolfgang wolfgang at samsa.pcs.com
Sun May 27 08:22:52 AEST 1990


In comp.unix.questions you write:


->I knew the "ps" command will show the status of any processes, but is there
->a system call that can check the status of a process, probably based on
->the process id? If there is no such a call, how can I check the status

Of course there is one. It is kill(2)! Extract from man:

SYNOPSIS
        int kill (pid, sig)
        int pid, sig;

DESCRIPTION
          kill sends a signal to a process or a group of processes.
          The process or group of processes to which the signal is to
          be sent is specified by pid.  The signal that is to be sent
          is specified by sig and is either one from the list given in
-->       signal(2), or 0.  If sig is 0 (the null signal), error
-->       checking is performed but no signal is actually sent.  This
-->       can be used to check the validity of pid.

So you can write code like the following:

/*
 * ended: inform user when a process has ended
 *        must be owned by root, with setuid bit on
 *        usage:  ended pid ...
 */

#include <stdio.h>

main(argc, argv)
        int argc;
        char *argv[];
{
        int pid;
        if (--argc == 0) {
                error("usage:  ended pid ...");
                exit(1);
        }
        while (argc--) {
                argv++;
                pid = atoi(*argv);
                if (pid <= 0) {
                        error("ended:  invalid process id: %s", *argv);
                        continue;
                }
                if (kill(pid, 0) < 0) {
                        error("ended:  no such process id: %s", *argv);
                        continue;
                }
                if (fork() == 0) {
                        while (kill(pid, 0) == 0)
                                sleep(10);
                        printf("\007Process %s has ended.\n", *argv);
                        exit(0);
                }
        }
        exit(0);
}

error(arglist)
int *arglist;
{
        fprintf(stderr, "%r", &arglist);
        fputc('\n', stderr);
}


The only problem in this program might be the "%r" (recursive) format
element in the call to "fprintf" in function "error". But that is
easy to change.

Ok, I hope you get lucky with it!

==================================================================
Name    : Wolfgang Denk
Company : PCS GmbH, Pfaelzer-Wald-Str. 36, 8000 Munich W-Germany.
UUCP    : ..[pyramid ;uunet!unido]!pcsbst!wd  (PYRAMID PREFERRED!!)
DOMAIN  : wd at pcsbst.pcs.[ COM From rest of world; DE From Europe ]



More information about the Comp.unix.questions mailing list