克隆线程上的 Linux 虚拟计时器行为
我已完成以下操作:
- 创建一个重复触发的虚拟计时器。
- 为 SIGVTALRM 安装信号处理
- 程序 调用克隆系统
- 调用 设置 sched_affinity 以便克隆线程在不同的 CPU 上运行
克隆线程是否也会侦听 SIGVTALRM?那么当 SIGVTALRM 被触发时,两个线程都会调用信号处理程序吗?另外,创建新线程后,我可以将其 SIGVTALRM 信号处理程序更改为另一个函数而不影响主线程信号处理程序吗?
我猜这取决于传递给clone()的标志。主要是,我使用 CLONE_SIGHAND 和 SIGCHLD。它也依赖于其他标志吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这完全取决于您是否为克隆系统调用指定
CLONE_THREAD
。如果您不这样做,则子进程不会继承该定时器(因此当定时器到期时不会发出信号)。但它仍然会安装一个信号处理程序。如果您确实指定
CLONE_THREAD
,则子进程将被视为与父进程属于同一进程。当计时器到期时,一个线程将收到信号(并运行信号处理程序) - 但没有指定是哪一个。如果您尝试更改子级中的信号处理程序,会发生什么取决于
CLONE_SIGHAND
标志。如果没有设置,那么子进程可以愉快地调用 sigaction 来更改信号处理程序,而不会影响父进程;但如果设置了 CLONE_SIGHAND ,那么当子进程调用 sigaction 时,整个进程的信号处理程序都会更改。另请注意,如果您指定CLONE_THREAD
,则还必须指定CLONE_SIGHAND
。但是,子级可以使用 sigprocmask 来屏蔽 SIGVTALRM 信号,而不会影响父级。
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 callsigaction
to change the signal handler without affecting the parent; but ifCLONE_SIGHAND
is 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
sigprocmask
to mask out theSIGVTALRM
signal, without affecting the parent.