Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!samsung!sdd.hp.com!elroy.jpl.nasa.gov!ames!aplvax!ames!think!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplcen!haven!adm!news From: wolfgang@samsa.pcs.com (wolfgang) Newsgroups: comp.unix.questions Subject: Re: Is there a system call to check the status of any processes? Message-ID: <9005220904.AA01656@samsa.pcs.com> Date: 26 May 90 22:22:52 GMT References: <23329@adm.BRL.MIL> Sender: news@adm.BRL.MIL Lines: 81 To: TAYBENGH%NUSDISCS.BITNET@cunyvm.cuny.edu Subj: Re: Is there a system call to check the status of any processes? To: TAYBENGH%NUSDISCS.BITNET@cunyvm.cuny.edu 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 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 ]