[comp.unix.questions] Summary to detection of server process status

TAYBENGH%NUSDISCS.BITNET@cunyvm.cuny.edu (05/27/90)

From:   com%"wolfgang@samsa.pcs.com" 22-MAY-1990 15:44:55.36
To:     TAYBENGH%NUSDISCS.BITNET@cunyvm.cuny.edu
CC:
Subj:   Re: Is there a system call to check the status of any processes?

Received: From CUNYVMV2(MAILER) by NUSDISCS with Jnet id 6153
          for TAYBENGH@NUSDISCS; Tue, 22 May 90 15:44 H
Received: from CUNYVM by CUNYVM.BITNET (Mailer R2.03B) with BSMTP id 4387; Tue,
 22 May 90 03:38:37 EDT
Received: from ames.arc.nasa.gov by CUNYVM.CUNY.EDU (IBM VM SMTP R1.2.2MX) with
 TCP; Tue, 22 May 90 03:38:34 EDT
Received: by ames.arc.nasa.gov (5.61/1.2); Tue, 22 May 90 00:39:35 -0700
Received: by pyramid.pyramid.com (5.61/OSx5.0-890710)
        id AA19826; Tue, 22 May 90 00:30:09 -0700
Received: by pcsbst.pcs.com (smail2.5)
        id AA23697; 22 May 90 09:05:26 MSZ (Tue)
Received: by samsa.pcs.com (smail2.5)
        id AA01658; 22 May 90 09:04:23 MSZ (Tue)
To: TAYBENGH%NUSDISCS.BITNET@cunyvm.cuny.edu
Subject: Re: Is there a system call to check the status of any processes?
Newsgroups: comp.unix.questions
References: <23329@adm.BRL.MIL>
Message-Id: <9005220904.AA01656@samsa.pcs.com>
Date: 22 May 90 09:04:21 MSZ (Tue)
From: wolfgang@samsa.pcs.com (wolfgang)

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 ]