进程的信号如何影响其子线程?

发布于 2024-12-12 00:56:53 字数 251 浏览 0 评论 0原文

各位大师,我在 Linux 函数方面遇到了 2 个问题:

  1. sleep

    据我所知,进程中的睡眠将被发送到其进程的任何信号中断。这是对的吗?

  2. 还在睡觉

    如果我在进程中创建多个线程,并在线程中插入睡眠函数,然后如果我向进程发送信号,睡眠会被中断吗?

顺便说一句,如果我向进程发送信号,当进程收到信号时,它会发送给其子线程吗?

感谢您的回答

gurus, I got 2 problems with the linux functions:

  1. sleep

    As far as I know sleep in process will get interrupted by any signals sent its process. Is this right?

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦途 2024-12-19 00:56:54
  1. 您应该使用条件等待而不是睡眠。

https://computing.llnl.gov/tutorials/pthreads/#ConVarOverview

您仅需要阅读“条件变量”部分。它可以帮助您了解 pthread 的概述以实现您的目的。

  1. You should use condition wait instead of sleep.

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.

抚笙 2024-12-19 00:56:53

1:睡觉,
据我所知,进程中的睡眠将被发送到其进程的任何信号中断。
对吗?

除非使用 sigprocmask< 忽略或阻止特定信号,否则它将被中断/代码>

2:还在睡觉,
如果我在进程中创建多个线程,并且插入睡眠函数
在线程中,然后如果我向进程发送信号,睡眠会
被打断吗?

即使该进程中有多个线程未阻止该信号,也只有一个线程接收发送到该进程的信号。有关更多详细信息,请参阅信号概念。在多线程进程中处理信号的标准方法是在所有线程中阻塞所有信号,但处理信号的线程(通常是主线程)除外。

顺便说一句:如果我向进程发送信号,当进程收到信号时,
它会发送给它的子线程吗?

不会的。

然而,如果进程是控制终端的会话组领导者,则当会话领导者终止时,其组将收到SIGHUP信号。此外,当进程组成为孤立进程时,它的进程将被发送 SIGHUP ,后跟 SIGCONT

1: sleep,
as i know sleep in process will get interrupted by any signals sent its process.
is it right?

It will get interrupted, unless the particular signal is ignored or blocked using sigprocmask.

2: 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?

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).

BTW: If i send a signal to a process , when the process get the signal,
will it send to its child threads?

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 sent SIGHUP followed by a SIGCONT.

孤者何惧 2024-12-19 00:56:53

1:是的,没错。如果您在 sleep() 之后检查 errno,它将被设置为 EINTR(已中断)。

2:每个线程都可以使用 pthread_sigmask() 设置自己的信号掩码,如果它阻止信号,则不会发送该信号。但是,如果没有显式阻止信号,则未定义哪个线程接收信号。

不过,您可以使用 pthread_kill() 直接向特定线程发送信号。

1: Yes that's right. If you examine errno after the sleep() it will be set to EINTR (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.

ゃ懵逼小萝莉 2024-12-19 00:56:53

进程定向信号被传递到进程的一个线程。如果该线程正在休眠,它将被唤醒。

A process-directed signal is delivered to one of the process's threads. If that thread is sleeping, it will be woken.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文