在 C 中向 pthread 发送和捕获信号
我知道如何使用 kill(pid_t pid, int sig)
函数向 C 中的子进程发送信号。向线程发送信号怎么样?是否可以?。如果是这样,如何捕获“子”线程上的信号。例如,如果主线程向我发送终止信号,我如何在另一个线程中捕获它。
I know how to send signals to child process in C using the kill(pid_t pid, int sig)
function. What about sending signals to threads? is it possible?. If so, how to catch signals on the "child" thread. For example, if the main thread sends me a terminate signal, how can I in the other thread catch it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
信号被发送到整个进程。发送到进程的每个信号都由单个线程(代表整个程序)接收。每个线程的信号掩码会影响特定线程是否有资格处理特定信号。
因此,您需要一个信号处理程序 - 可能仅在一个线程中。但请注意,您在线程的信号处理程序中应该执行的操作是有限的。请小心不要远远超出标准所做出的承诺(这是最低限度的)。
但是,
pthread_kill()
函数可用于发送信号只要当前线程可以识别(可以访问识别的线程 ID 值 (pthread_t
))仍在进程内执行的线程,就可以访问其他线程。您可能决定使用与最初捕获的信号号不同的信号号将信号中继到其他线程(因此一个线程接收外部信号,但许多线程接收内部信号)。或者您可以使用另一个 Pthread 同步或通信原语来代替信号。Signals are sent to a process as a whole. Each signal sent to the process is received by a single thread (on behalf of the whole program). There are per-thread signal masks which influence whether a particular thread is eligible to handle a particular signal.
So, you need a signal handler - possibly in just one thread. Note that there are limits on what you're supposed to do in a thread's signal handler, though. Be wary of stepping far outside the promises made by the standard (which are minimal).
However, the
pthread_kill()
function can be used to send signals to other threads as long as the current thread can identify (has access to the thread ID values (pthread_t
) that identify) the threads that are still executing within the process. You might decide to relay the signal to the other threads using a different signal number from the one originally caught (so one thread receives the external signal, but many threads receive the internal signal). Or you might use another Pthread synchronization or communication primitive instead of signals.对于 POSIX 线程,您可以使用函数
pthread_cond_wait
和pthread_cond_signal
。发出信号的线程应阻塞在
pthread_cond_wait
调用上,直到另一个线程使用具有相同条件变量的pthread_cond_signal
发送信号。考虑到传递给进程的信号的类比,这有点不同,因为有信号的线程已经挂起其执行等待信号,这与简单地被中断并继续的进程不同。
With POSIX threads, you have the functions
pthread_cond_wait
andpthread_cond_signal
.The signaled thread should block on a
pthread_cond_wait
call until another thread sends a signal usingpthread_cond_signal
, with the same condition variable.Considering the analogy with signals delivered to processes, this is a bit different because the signaled thread has already suspended its execution waiting for a signal, unlike a process that simply gets interrupted and goes on.
信号不具有线程关联性。它们是完全异步处理的。当您使用
signal(2)
或sigaction(2)
指定信号处理程序时,它是全局信号处理程序。当发出信号时,信号处理程序在当时正在运行的任何线程的堆栈顶部运行,并且您无法控制它。听起来您想要某种其他类型的线程间通信。最简单的方法是使用 易失性共享变量:
如果您需要更强的并发控制,请研究互斥体、条件变量和信号量。
Signals do not have thread affinity. They are handled completely asynchronously. When you specify a signal handler with
signal(2)
orsigaction(2)
, it's a global signal handler. When a signal is raised, the signal handler runs on top of the stack of whatever thread happens to be running at the time, and you can't control that.It sounds like you want some other sort of interthread communication. The simplest way to do this is with a
volatile
shared variable:If you need stronger concurrency control, look into mutexes, condition variables, and semaphores.
我不确定这是否可能,因为它依赖于平台和实现,我强烈建议您不要使用信号在线程之间进行通信。有时只有特定线程会接收信号,有时所有线程都会接收信号。
存在更好的线程通信机制,例如队列、信号量和锁。
I'm not sure this is possible as it is platform and implementation dependant and I highly suggest you don't use signals to communicate between threads. Sometimes only a specific thread will receive signals and sometimes all threads receive signals.
Better thread communucation mechanisms exist like queues, semaphores, and locks.