3 ms·
Sure, it's probably a hack if you write a loop that looks like this: while (!waitpid(child_pid, &status, WNOHANG)) usleep(10); } But WNOHANG i
by tomjakubowski 10y ago
Sure, it's probably a hack if you write a loop that looks like this:
while (!waitpid(child_pid, &status, WNOHANG))
usleep(10);
}
But WNOHANG is perfectly fine, even necessary, if you're calling it from the body of some periodically scheduled event loop which you mustn't block, like in a video game or something.
- klodolph 10y agoI think some of the context for "WNOHANG is a bit of a hack" got lost. > You can sort of work around it by calling waitpid(...WNOHANG) every tenth of a second or so. For event loops, you can still use SIGCHLD (which is portable) or signalfd (which integrates better into event loops). This avoids the extra syscall every loop through the event loop.
- geofft 10y agoSIGCHLD (via a signal handler, signalfd, sigtimedwait, whatever) only tells you that some child exited, and also only one part of the process can deal with SIGCHLD. (You can't correctly compose a signal handler + signalfd.) waitpid on the specific pid will check that specific process and no others, and not interfere with any other part of the program that might be waiting for some other child.