ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I have a multi-threaded daemon which starts child processes and reads the output from them via a pipe, with a controlling thread for each child process. The daemon needs to listen for SIGCHLD to tell when the child has been stopped/continued or has exited.
The problem I'm having is that LinuxThreads doesn't send the SIGCHLD to the daemon process, but instead sends it to the thread that started the child - which is contrary to POSIX.
I can't handle the SIGCHLD asynchronously because none of the pthread functions are asnyc-signal-safe, and at the very least I'd need to lock the variable I'm accessing. So, it looks like I'll have to have two threads for each child - one to read its output via the pipe, and another to sit and wait synchronously with sigwait(). However, the docs for sigwait() say that you should only have one thread in the whole process waiting for a particular signal at any one time - but I would have as many sigwait()s as I have child processes.
It seems then that there's no safe way to proceed. Can anyone suggest another way of doing this?
I've found a solution that appears to work, so in case someone else has a similar problem, I'll add it here.
I tried doing multiple sigwait()s, but that doesn't work at all. So, I replaced the sigwait() loop with a sleep() loop and installed an async signal handler. Despite none of the pthread functions being async-signal safe, they should be okay to use anyway because the interrupted thread is always in a known state - ie., it is sleeping. I might try changing this to only use one thread per child in the future, which should be possible by using pthread_sigmask() to control when an async signal is delivered by blocking and unblocking the signals when the thread isn't holding any mutexes.
Of course both of these solutions could be prone to deadlocks if there's some locking being done by library functions that I'm not aware of, but if that's the case then I haven't seen it yet. Here's hoping.