I have done the following:
- Create a virtual timer that triggers repeatedly.
- Install signal handler for SIGVTALRM
- Call clone 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_THREADto 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_SIGHANDflag. If it’s not set, then the child can happily callsigactionto change the signal handler without affecting the parent; but ifCLONE_SIGHANDis set, then when the child callssigaction, the signal handler is changed for the entire process. Note also that if you specifyCLONE_THREAD, you also have to specifyCLONE_SIGHAND.The child can, however, use
sigprocmaskto mask out theSIGVTALRMsignal, without affecting the parent.