在 C 中向 pthread 发送和捕获信号

发布于 2024-10-03 09:05:02 字数 137 浏览 3 评论 0原文

我知道如何使用 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 技术交流群。

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

发布评论

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

评论(4

冰之心 2024-10-10 09:05:02

信号被发送到整个进程。发送到进程的每个信号都由单个线程(代表整个程序)接收。每个线程的信号掩码会影响特定线程是否有资格处理特定信号。

因此,您需要一个信号处理程序 - 可能仅在一个线程中。但请注意,您在线程的信号处理程序中应该执行的操作是有限的。请小心不要远远超出标准所做出的承诺(这是最低限度的)。

但是,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.

禾厶谷欠 2024-10-10 09:05:02

对于 POSIX 线程,您可以使用函数 pthread_cond_waitpthread_cond_signal

int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex)
int pthread_cond_signal(pthread_cond_t *cond)

发出信号的线程应阻塞在 pthread_cond_wait 调用上,直到另一个线程使用具有相同条件变量的 pthread_cond_signal 发送信号。

考虑到传递给进程的信号的类比,这有点不同,因为有信号的线程已经挂起其执行等待信号,这与简单地被中断并继续的进程不同。

With POSIX threads, you have the functions pthread_cond_wait and pthread_cond_signal.

int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex)
int pthread_cond_signal(pthread_cond_t *cond)

The signaled thread should block on a pthread_cond_wait call until another thread sends a signal using pthread_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.

梦途 2024-10-10 09:05:02

信号不具有线程关联性。它们是完全异步处理的。当您使用 signal(2)sigaction(2) 指定信号处理程序时,它是全局信号处理程序。当发出信号时,信号处理程序在当时正在运行的任何线程的堆栈顶部运行,并且您无法控制它。

听起来您想要某种其他类型的线程间通信。最简单的方法是使用 易失性共享变量:

volatile bool should_terminate = false;

void ChildThread()
{
    while(!should_terminate)
    {
        // do stuff
    }
}

void MainThread()
{
    // To terminate child thread:
    should_terminate = true;
}

如果您需要更强的并发控制,请研究互斥体、条件变量和信号量。

Signals do not have thread affinity. They are handled completely asynchronously. When you specify a signal handler with signal(2) or sigaction(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:

volatile bool should_terminate = false;

void ChildThread()
{
    while(!should_terminate)
    {
        // do stuff
    }
}

void MainThread()
{
    // To terminate child thread:
    should_terminate = true;
}

If you need stronger concurrency control, look into mutexes, condition variables, and semaphores.

情愿 2024-10-10 09:05:02

我不确定这是否可能,因为它依赖于平台和实现,我强烈建议您不要使用信号在线程之间进行通信。有时只有特定线程会接收信号,有时所有线程都会接收信号。

存在更好的线程通信机制,例如队列、信号量和锁。

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.

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