[comp.unix.questions] Is there a system call to check the status of any processes?

wolfgang@samsa.pcs.com (wolfgang) (05/27/90)

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@pcsbst.pcs.[ COM From rest of world; DE From Europe ]

e89hse@rigel.efd.lth.se (05/28/90)

In article <9005220904.AA01656@samsa.pcs.com>, wolfgang@samsa.pcs.com (wolfgang) writes:
>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: [...]
>[ Program ended...]
>                        while (kill(pid, 0) == 0)
>                                sleep(10);
>                        printf("\007Process %s has ended.\n", *argv);
>                        exit(0);

 Isn't there any more graceful way to do this? I looked the HP-UX manual and it
turned out they had something called waitpid(pid,stat_loc,opt) that can be used
to wait for one (out of many) child process.

guy@auspex.auspex.com (Guy Harris) (05/30/90)

 > Isn't there any more graceful way to do this? I looked the HP-UX manual and
 >it turned out they had something called waitpid(pid,stat_loc,opt) that can
 >be used to wait for one (out of many) child process.

Unfortunately, "this" is checking for the status of a process that's
*not* a child of the checker, so "waitpid()" won't help you a bit.