This is a discussion on In a C program. How to find if a process is still running within the AIX Operating System forums, part of the Unix Operating Systems category; --> Is there any way in a program of finding if a process id is still valid? I know I ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| Is there any way in a program of finding if a process id is still valid? I know I could do a popen("ps -p pid"), etc, but that is slow. Is there a library call that may be made (On Linux, we look for /proc/pid directory. On HPUX, we use pstat_getproc call). Regards, Ian. |
| |||
| Hi Ian, "Ian Collins" <ianc@kcbbs.gen.nz> wrote in message news:bf4hkt$nur$1@aklobs.kc.net.nz... > Is there any way in a program of finding if a process id is still valid? There are two ways that I can think of, the getprocs() function will work on 4.33, 5.1 and 5.2, the /proc/ filesystem ia also present in 5.1 and 5.2. Be careful here though, the fact that a process is still around doesn't mean it's alive (it could well be a zombie process). Cheers, Shaun |
| |||
| In article <bf4hkt$nur$1@aklobs.kc.net.nz>, Ian Collins <ianc@kcbbs.gen.nz> writes: > Is there any way in a program of finding if a process id is still valid? > > I know I could do a popen("ps -p pid"), etc, but that is slow. > > Is there a library call that may be made > (On Linux, we look for /proc/pid directory. On HPUX, we use > pstat_getproc call). > > Regards, > Ian. > Note that the approach of checking /proc/pid is not reliable, neither checking with "ps". The problem is that once a process dies, its PID may be reused by the system for other processes, maybe a new one with the same 'name'. This is certainly difficult to happen with a long PID space unless your system is hard working running countless processes continuously, but it is possible nevertheless. Hence, by looking up existence of a PID you can't guarantee the same process is there (you might have a great confidence, but not certainty). If the monitor process is the parent, then simply check with the 'wait' calls. You may keep a table of spawned processes and mark them as they die. Knowing if one is alive reduces to a table lookup. If the monitored process cooperates you may check for some shared knowledge, say a file that's open while the process is alive, or some such. If you have extra knowledge, it may be used, e.g. if only one instance of the process may be running (e.g. it locks a resource, and subsequent instances will find the resource locked and refuse to run), then you may search for the process by name (f it is there, then it must be 'the' process) or try to lock the resource yourself (if you succeed then it isn't locked and the process died --if you don't, maybe it died without unlocking the resource, beware). All in all, the best solution is to either be the parent or have the process cooperate (IMHO). Cooperation may be achieved with a wrapper script that sets a centinnel, runs the process and removes the centinnel when the process dies (but that's sort of a variety of the parent/child issue). You would just check for the centinnel. "truss" comes to mind as a suitable alternative to run and monitor a program. Note that there may be better ways, so stay tuned. Actually, I remember there used to be a call to monitor system calls, which maybe can be used to monitor when a process dies. Probably using the accounting subsystem, but I can't quite remember it offhand now. Which takes me to consider: you might be able to monitor the accounting data and check when the process terminates (you may need privileges though). This is Q&D, I know, but I'm not at one of my best moments and too busy to look it up. Should get you on track though. j -- These opinions are mine and only mine. Hey man, I saw them first! José R. Valverde De nada sirve la Inteligencia Artificial cuando falta la Natural |
| ||||
| Ian Collins wrote: > Is there any way in a program of finding if a process id is still valid? > > I know I could do a popen("ps -p pid"), etc, but that is slow. The normal way is via kill() and the special signal number 0: if (kill(pid, 0) == 0) { /* process is still valid */ } else { /* process is gone */ } As one of the posters suggested, mechanisms like this that assume the pid hasn't been reused are not perfect. However, many programs use this successfully and since they make the check much more frequently than the wrapping of pids they will notice the monitored process has gone away before some other process has used the same pid. |