Path: utzoo!attcan!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!auspex!guy From: guy@auspex.auspex.com (Guy Harris) Newsgroups: comp.unix.questions Subject: Re: How to tell if a process exists Message-ID: <3817@auspex.auspex.com> Date: 4 Aug 90 18:20:08 GMT References: <26B867F8.38BB@wilbur.coyote.trw.com> Organization: Auspex Systems, Santa Clara Lines: 33 >In a C program, how do you tell if a certain process exists? A >kill(0,pid) works only if you own the process (or you're root). Wrong. "kill(0, pid)" can be made to work just as well if you don't have permission to send the signal to the process as it does if you do have permission to send the signal to the process; you just have to be a little more careful about checking the value of "errno" if it fails. (Remember, "kill(0, pid)" does everything that "kill(, pid)" does, except that it doesn't actually send the signal. If the process doesn't exist, it returns -1 and sets "errno" to ESRCH; if the process exists, but you don't have permission to send to it, it returns -1 and sets "errno" to EPERM.) >The reason I need this is that I have a user interface that depends on >another prcoess to exist to run properly. The other process writes its >process id to a file and then I want the user interface to check to >see if that process id is still there. Is there a better way to do >this? There is, however, a reason why I said "just as well" above. What "kill(0, pid)" does is check whether *a* process with process ID "pid" exists; there is some chance that *your* process with that process ID terminated, and the process IDs wrapped around such that some other unrelated process now has that process ID. The chance may be small, but it's not zero. In addition, in some versions of UNIX, "kill", when told to send a signal to a "zombie" process (i.e., one that has exited but not been reaped by its parent), will fail and set "errno" to ESRCH. In other versions, it will succeed, and this may be what POSIX requires; if so, other systems will do so as well in the future. Ultrix, at present, is *probably* one of the systems where it fails, but beware....