多线程C程序;如何杀死线程所产生的进程? [英] Multithreaded C program; how to kill processes spawned by threads?

查看:236
本文介绍了多线程C程序;如何杀死线程所产生的进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:

我写在C程序维护多个线程。一旦线程结束,一个新的被创建。

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

每个线程叉 - 孩子通过EXEC运行的进程()和父等待它完成

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

在此外,还有一个信号处理器线程等待信号。如果检测到SIGINT然后它告诉主线程停止创建线程所以最终所有的线程结束,程序可以退出。

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.

目的:

我希望能够通过发送SIGTERM终止程序。这将通过停止主线程创建新线程,并通过终止线程创建的正在运行的进程的工作。

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.

问题:

如果信号被阻断在所有的线程,我怎么能发送信号到正在运行的进程将它们终止?

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?

推荐答案

创建一个新的进程组的所有的子进程,然后将信号发送到该组。

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

下面是一些最起码的code,显示过程是如何改变它的组和对照子进程用的信号。

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 );
}

这篇关于多线程C程序;如何杀死线程所产生的进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆