How to tell if a forked process is done?
bellman at lysator.liu.se
Wed Sep 24 22:16:03 CEST 2003
François Pinard <pinard at iro.umontreal.ca> wrote:
> [Klaus Alexander Seistrup]
>> This will yield a false positive and potential damage if the OS has
>> spawned another process with the same pid, and running under your uid,
>> as the task you wanted to supervise.
> Granted in theory, yet this does not seem to be considered a real
> problem in practice. To generate another process with the same pid, the
> system would need to generate so many intermediate processes that the
> process counter would overflow and come back to its current value.
There it least one Unix that reuse process ids immediately when
they are free. A vague memory says that it is AIX that does
this, but I'm not sure; it could be some of the BSD dialects too.
And on most other Unices start to reuse process ids *long* before
they reach 2^31.
However, in this very case, *that* isn't a problem. The process
id won't be free to reuse until the parent has called wait(2) to
reap its child. On the other hand, that means that kill(pid, 0)
won't signal an error even after the child has died; the zombie
is still there...
>>> import os, time
>>> def f():
... child = os.fork()
... if child == 0:
... print "Exit:", time.ctime()
... return child
>>> p = f()
Wed Sep 24 21:52:38 2003
>>> os.kill(p, 0)
>>> Exit: Wed Sep 24 21:53:38 2003
>>> os.kill(p, 0) # Note no error
>>> os.kill(p, 0) # Still no error
>>> os.kill(p, 0) # *Now* we get an error
Traceback (most recent call last):
File "<stdin>", line 1, in ?
OSError: [Errno 3] No such process
> `kill(pid, 0)' trick is still the way people seem to do it.
If they do so when waiting for a child process to exit, they will
> Do you know anything reasonably simple, safer, and that does the job?
See my .signature. :-)
Thomas Bellman, Lysator Computer Club, Linköping University, Sweden
"Life IS pain, highness. Anyone who tells ! bellman @ lysator.liu.se
differently is selling something." ! Make Love -- Nicht Wahr!
More information about the Python-list