Note that there are some explanatory texts on larger screens.

plurals
  1. POWaitpid equivalent with timeout?
    text
    copied!<p>Imagine I have a process that starts several child processes. The parent needs to know when a child exits.</p> <p>I can use <code>waitpid</code>, but then if/when the parent needs to exit I have no way of telling the thread that is blocked in <code>waitpid</code> to exit gracefully and join it. It's nice to have things clean up themselves, but it may not be that big of a deal.</p> <p>I can use <code>waitpid</code> with <code>WNOHANG</code>, and then sleep for some arbitrary time to prevent a busy wait. However then I can only know if a child has exited every so often. In my case it may not be super critical that I know when a child exits right away, but I'd like to know ASAP...</p> <p>I can use a signal handler for <code>SIGCHLD</code>, and in the signal handler do whatever I was going to do when a child exits, or send a message to a different thread to do some action. But using a signal handler obfuscates the flow of the code a little bit.</p> <p>What I'd really like to do is use <code>waitpid</code> on some timeout, say 5 sec. Since exiting the process isn't a time critical operation, I can lazily signal the thread to exit, while still having it blocked in <code>waitpid</code> the rest of the time, always ready to react. <em>Is there such a call in linux? Of the alternatives, which one is best?</em></p> <hr> <p>EDIT:</p> <p>Another method based on the replies would be to block <code>SIGCHLD</code> in all threads with <code>pthread</code> \ <code>_sigmask()</code>. Then in one thread, keep calling <code>sigtimedwait()</code> while looking for <code>SIGCHLD</code>. This means that I can time out on that call and check whether the thread should exit, and if not, remain blocked waiting for the signal. Once a <code>SIGCHLD</code> is delivered to this thread, we can react to it immediately, and in line of the wait thread, without using a signal handler.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload