Quantcast
Channel: niemueller.de
Viewing all articles
Browse latest Browse all 10

To sleep or not to sleep

$
0
0
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 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).

Viewing all articles
Browse latest Browse all 10

Trending Articles