[Tutor] Handling SIGCHLD signal to kill zombie processes

John P Speno speno at isc.upenn.edu
Wed Dec 3 18:58:51 EST 2003

On Wed, Dec 03, 2003 at 07:02:27PM -0000, rafael.sousa wrote:
> Hi!
> I need to know what to write in a function that handles the SIGCHLD signal, in order to kill zombie processes that are left running.
> -> In C ANSI, what I do is start by specifying what function is going to handle the signal:
> signal(SIGCHLD, handleSIGCHLD);
> and then one handler that does the trick is:
> void handleSIGCHLD() {
>    int stat;
>    /*Kills all the zombie processes*/
>    while(waitpid(-1, &stat, WNOHANG) > 0);
> }
> -> In Python, specifying the handler is easy:
> import signal
> signal.signal(signal.SIGCHLD, handleSIGCHLD)
> What I don't know how to do is an equivalent to the C function above...

How much help do you need?

If you are looking for python's version of waitpid, you can find it in the
os module:

import os

def handleSIGCHLD():
    os.waitpid(-1, os.WNOHANG)

On some unixes, you don't have to reap children if you ignore SIGCHLD.

signal.signal(signal.SIGCHLD, signal.SIG_IGN)

More information about the Tutor mailing list