进程的信号如何影响其子线程?
各位大师,我在 Linux 函数方面遇到了 2 个问题:
sleep
据我所知,进程中的睡眠将被发送到其进程的任何信号中断。这是对的吗?
还在睡觉
如果我在进程中创建多个线程,并在线程中插入睡眠函数,然后如果我向进程发送信号,睡眠会被中断吗?
顺便说一句,如果我向进程发送信号,当进程收到信号时,它会发送给其子线程吗?
感谢您的回答
gurus, I got 2 problems with the linux functions:
sleep
As far as I know sleep in process will get interrupted by any signals sent its process. Is this right?
still the sleep
If I create several threads in the process, and I insert sleep functions in the threads, and, then if I send signals into the process, will the sleep be interrupted?
BTW if I send a signal to a process, when the process get the signal, will it send to its child threads?
Thanx for your answers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
https://computing.llnl.gov/tutorials/pthreads/#ConVarOverview
您仅需要阅读“条件变量”部分。它可以帮助您了解 pthread 的概述以实现您的目的。
https://computing.llnl.gov/tutorials/pthreads/#ConVarOverview
You only need to read section "Condition Variables". It helps you get overview of pthread for your purpose.
除非使用
sigprocmask< 忽略或阻止特定信号,否则它将被中断/代码>
。
即使该进程中有多个线程未阻止该信号,也只有一个线程接收发送到该进程的信号。有关更多详细信息,请参阅信号概念。在多线程进程中处理信号的标准方法是在所有线程中阻塞所有信号,但处理信号的线程(通常是主线程)除外。
不会的。
然而,如果进程是控制终端的会话组领导者,则当会话领导者终止时,其组将收到
SIGHUP
信号。此外,当进程组成为孤立进程时,它的进程将被发送SIGHUP
,后跟SIGCONT
。It will get interrupted, unless the particular signal is ignored or blocked using
sigprocmask
.Only one thread receives a signal sent to the process even if there are several threads within that process that have that signal unblocked. See Signal Concepts for more details. The standard way to handle signals in a multi-threaded process is to have all signals blocked in all threads but the one that handles the signals (normally it's the main thread).
It won't.
However, if the process is a session group leader controlling the terminal, its group will receive
SIGHUP
signal when the session leader terminates. Also, when a process group becomes orphaned its processes get sentSIGHUP
followed by aSIGCONT
.1:是的,没错。如果您在
sleep()
之后检查errno
,它将被设置为EINTR
(已中断)。2:每个线程都可以使用 pthread_sigmask() 设置自己的信号掩码,如果它阻止信号,则不会发送该信号。但是,如果没有显式阻止信号,则未定义哪个线程接收信号。
不过,您可以使用 pthread_kill() 直接向特定线程发送信号。
1: Yes that's right. If you examine
errno
after thesleep()
it will be set toEINTR
(interrupted).2: Each thread can set it's own signal mask using
pthread_sigmask()
and if it blocks the signal it won't be sent it. However it's undefined which of the threads receives the signal, if they don't block the signal explicitly.You can send a signal directly to a specific thread using
pthread_kill()
, however.进程定向信号被传递到进程的一个线程。如果该线程正在休眠,它将被唤醒。
A process-directed signal is delivered to one of the process's threads. If that thread is sleeping, it will be woken.