Linux Virtual Timer Behavior on Clone Threads
I have done the following:
- Create a virtual timer that triggers repeatedly.
- Install signal handler for SIGVTALRM
- Call clone开发者_JS百科 syscall
- Set sched_affinity such that the cloned thread runs on a different CPU
Will the cloned thread also be listening for SIGVTALRM? So will both the threads call the signal handler when SIGVTALRM is triggered? Also, after creating the new thread, can I change its signalhandler for SIGVTALRM to another function without affecting the main threads signalhandler?
I'm guessing it depends on the flags passed to clone(). Mainly, I'm using CLONE_SIGHAND and SIGCHLD. Does it depend on other flags as well?
It depends entirely on whether you specify CLONE_THREAD
to the clone syscall. If you do not, then the itimer is not inherited by the child (so it will not be signalled when the timer expires). It will still have a signal handler installed though.
If you do specify CLONE_THREAD
, then the child is considered to belong to the same process as the parent. When the timer expires, one of the threads will be signalled (and run the signal handler) - but it's not specified which one.
What happens if when you try to change the signal handler in the child depends on the CLONE_SIGHAND
flag. If it's not set, then the child can happily call sigaction
to change the signal handler without affecting the parent; but if CLONE_SIGHAND
is set, then when the child calls sigaction
, the signal handler is changed for the entire process. Note also that if you specify CLONE_THREAD
, you also have to specify CLONE_SIGHAND
.
The child can, however, use sigprocmask
to mask out the SIGVTALRM
signal, without affecting the parent.
精彩评论