多线程C程序;如何杀死线程产生的进程?

发布于 2024-08-21 16:51:51 字数 457 浏览 4 评论 0原文

情况:

我正在用 C 编写一个程序,该程序维护多个线程。一旦线程结束,就会创建一个新线程。

每个线程都分叉 - 子线程通过 exec() 运行一个进程,而父线程等待它完成。

此外,还有一个等待信号的信号处理线程。如果检测到 SIGINT,那么它会告诉主线程停止创建线程,因此最终所有线程都会结束并且程序可以退出。

信号在所有线程中都被阻塞,当然信号处理程序线程除外。

目标:

我希望能够通过发送 SIGTERM 来终止程序。这可以通过停止主线程创建新线程并终止线程创建的正在运行的进程来实现。

问题:

如果信号在所有线程中都被阻塞,我如何向正在运行的进程发送信号来终止它们?

有没有办法让生成的进程只接收从主程序发送的信号而不接收发送到主程序的信号?

Situation:

I am writing a program in C that maintains a number of threads. Once a thread ends, a new one is created.

Each thread forks - the child runs a process via exec() and the parent waits for it to finish.

In addition, there is a signal handler thread that waits for signals. If SIGINT is detected then it tells the main thread to stop creating threads so eventually all the threads end and the program can exit.

Signals are blocked in all threads except of course the signal handler thread.

Aim:

I want to be able to terminate the program by sending SIGTERM. This would work by stopping the main thread creating new threads and also terminating the running processes created by threads.

Problem:

If signals are blocked in all the threads, how can I send a signal to the running processes to terminate them?

Is there some way to make the spawned processes only receive signals sent from the main program and not signals sent to the main program?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

全部不再 2024-08-28 16:51:51

在新的进程组中创建所有子进程,然后向该组发送信号。

编辑:

这里有一些最小的代码,显示进程如何使用信号更改其组和控制子进程。

#include <err.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

pid_t pgid; /* process group to kill */

void terminator( int s ) /* signal handler */
{
    printf( "[%d:%d] received signal %d, exiting\n",
        getpid(), getpgrp(), s );
    exit( 1 );
}

int main() /* program entry */
{
    printf( "[%d:%d] before first fork\n",
        getpid(), getpgrp() );

    switch ( fork()) /* try with and without first fork */
    {
        case -1: err( 1, "fork" );
        case 0: break;
        default: exit( 0 );
    }

    if ( signal( SIGTERM, terminator ) == SIG_ERR )
        err( 1, "signal" );

    if ( setpgrp() < 0 ) err( 1, "setpgrp" );
    if (( pgid = getpgrp()) < 0 ) err( 1, "getpgrp" );

    printf( "[%d:%d -> %d] before second fork\n",
        getpid(), getpgrp(), pgid );

    switch ( fork())
    {
        case -1: err( 1, "fork" );
        case 0: /* child */
        {
            printf( "child [%d:%d]\n", getpid(), getpgrp());
            sleep( 20 );
            break;
        }
        default: /* parent */
        {
            printf( "parent [%d:%d]\n", getpid(), getpgrp());
            sleep( 5 );
            killpg( pgid, SIGTERM );
            sleep( 20 );
        }
    }

    printf( "[%d:%d] exiting\n", getpid(), getpgrp());
    exit( 1 );
}

Create all child processes in a new process group, then send the signal to the group.

Edit:

Here's some minimal code that shows how process can change it's group and control child processes with a signal.

#include <err.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

pid_t pgid; /* process group to kill */

void terminator( int s ) /* signal handler */
{
    printf( "[%d:%d] received signal %d, exiting\n",
        getpid(), getpgrp(), s );
    exit( 1 );
}

int main() /* program entry */
{
    printf( "[%d:%d] before first fork\n",
        getpid(), getpgrp() );

    switch ( fork()) /* try with and without first fork */
    {
        case -1: err( 1, "fork" );
        case 0: break;
        default: exit( 0 );
    }

    if ( signal( SIGTERM, terminator ) == SIG_ERR )
        err( 1, "signal" );

    if ( setpgrp() < 0 ) err( 1, "setpgrp" );
    if (( pgid = getpgrp()) < 0 ) err( 1, "getpgrp" );

    printf( "[%d:%d -> %d] before second fork\n",
        getpid(), getpgrp(), pgid );

    switch ( fork())
    {
        case -1: err( 1, "fork" );
        case 0: /* child */
        {
            printf( "child [%d:%d]\n", getpid(), getpgrp());
            sleep( 20 );
            break;
        }
        default: /* parent */
        {
            printf( "parent [%d:%d]\n", getpid(), getpgrp());
            sleep( 5 );
            killpg( pgid, SIGTERM );
            sleep( 20 );
        }
    }

    printf( "[%d:%d] exiting\n", getpid(), getpgrp());
    exit( 1 );
}

离旧人 2024-08-28 16:51:51
  1. 在新的进程组中创建所有子进程,然后向该组发送信号。 (感谢 Nokolai)
  2. 保留所有创建的进程的记录,并在需要时使用它来向它们发出信号。
  1. Create all child processes in a new process group, then send the signal to the group. (thanks Nokolai)
  2. Keep records on all processes created and use that to signal them when desired.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文