Today looked for a problem for about an hour which I thought might be worth sharing. The following (most likely common) scenario was to be implemented: one process wants to start another and be notified if something happens to the child process. The proper way to achieve this is to
Now, in I'd guess likewise common is to start with a test program that incorporates sleep(). Fail! After an hour it turns out that the sleep() call will block the
and
So either you need to unblock the signal after calling
fork()
, then execl()
the new program in the child, and have the parent call wait()
to check for state changes of children.
Now, in I'd guess likewise common is to start with a test program that incorporates sleep(). Fail! After an hour it turns out that the sleep() call will block the
SIGCHLD
signal (cf. sleep.c). But it does not unblock the signal afterwards. Since the wait()
call depends on SIGCHLD
, no event would be received
and
wait()
would starve.
So either you need to unblock the signal after calling
sleep()
, or better call usleep()
which does not suffer this problem (cf. usleep.c).