Imagine that I have a process that starts several child processes. The parent must know when the baby will be released.
I can use waitpid , but then when / when the parent should exit, I canβt tell the thread that is blocked in waitpid to legally exit and join it. It's nice that things cleanse themselves, but it may not be that important.
I can use waitpid with WNOHANG and then sleep for some arbitrary time to prevent waiting for the wait. However, then I can only know if the child went out so often. In my case, it may not be very critical that I know when the child leaves immediately, but I would like to know as soon as possible ...
I can use the signal handler for SIGCHLD , and in the signal handler do what I was going to do when the child exits, or send a message to another thread to do some action. But using a signal handler confuses the code stream a bit.
What I really would like to do is use waitpid in some timeout, say 5 seconds. Since exiting the process is not a time-critical operation, I can lazily signal the exit of the stream, while maintaining that it is blocked in waitpid the rest of the time, always ready to respond. Is there such a call on Linux? Of the alternatives, which one is better?
EDIT:
Another answer-based method would block SIGCHLD in all threads using pthread \ _sigmask() . Then in one thread continue to call sigtimedwait() , looking for SIGCHLD . This means that I can disconnect this call and check if the thread should exit, and if not, stay blocked while waiting for the signal. As soon as a SIGCHLD delivered to this stream, we can immediately respond to it in the line of the wait stream as well, without using a signal handler.
c ++ c linux
Greg Rogers Nov 11 '08 at 21:19 2008-11-11 21:19
source share