在 C 语言中杀死一个子进程

本文将演示有关如何在 C 语言中杀死子进程的多种方法。

使用 SIGKILL 信号终止 C 语言中的子进程

有多种信号设计用于在进程交付时终止该进程,但是发送 SIGKILL 信号是执行此任务的最强大,最可靠的方法。通常,程序可以注册称为信号处理程序的特殊功能,一旦将相应的信号传递到程序,这些特殊功能便会自动调用。用户实现处理程序功能代码,该代码通常会对程序进行一些清理工作。除函数处理程序外,对传递的信号可能有默认操作,例如阻塞和忽略。虽然,给定功能不能忽略,阻止或处理 SIGKILL 信号。因此,在尝试终止进程时,此方法应该是最后的选择。

可以通过 kill 系统调用来发送 SIGKILL 信号。但是请注意,在以下代码示例中注册的 SIGTERM 处理程序无法捕获提供的 SIGKILL,它会立即终止给定的子进程。

以下程序产生一个子进程,并在其中注册 SIGTERM 处理程序。然后,除非传递信号,否则子进程将执行无限循环,这将翻转 while 表达式变量。因此,父级传递的 SIGKILL 信号不会调用处理程序并立即终止子级进程。

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/wait.h>#include <signal.h>volatile sig_atomic_t shutdown_flag = 1;
void cleanupRoutine(int signal_number)
{
    shutdown_flag = 0;
}
int main(void) {
    int wstatus;
    pid_t c_pid = fork();
    if (c_pid == -1) {
        perror("fork");
        exit(EXIT_FAILURE);
    }
    if (c_pid == 0) {
        printf("printed from child process - %d\n", getpid());
        int count = 0;
        struct sigaction sigterm_action;
        memset(&sigterm_action, 0, sizeof(sigterm_action));
        sigterm_action.sa_handler = &cleanupRoutine;
        sigterm_action.sa_flags = 0;
        // Mask other signals from interrupting SIGTERM handler
        if (sigfillset(&sigterm_action.sa_mask) != 0)
        {
            perror("sigfillset");
            exit(EXIT_FAILURE);
        }
        // Register SIGTERM handler
        if (sigaction(SIGTERM, &sigterm_action, NULL) != 0)
        {
            perror("sigaction SIGTERM");
            exit(EXIT_FAILURE);
        }
        while (shutdown_flag) {
            count += 1;
        }
        printf("count = %d\n", count);
        exit(EXIT_SUCCESS);
    } else {
        printf("printed from parent process - %d\n", getpid());
        int ret;
        sleep(5);
        ret = kill(c_pid, SIGKILL);
        if (ret == -1) {
            perror("kill");
            exit(EXIT_FAILURE);
        }
        if (waitpid(c_pid, &wstatus, WUNTRACED | WCONTINUED) == -1) {
            perror("waitpid");
            exit(EXIT_FAILURE);
        }
    }
    exit(EXIT_SUCCESS);
}

使用 SIGTERM 信号终止 C 语言中的子进程

或者,可以使用 SIGTERM 信号终止子进程,该信号可以由程序处理。下一个代码示例重复了以前的程序实现,只不过它用 SIGTERM 代替了 SIGKILL 信号。sigfillset 函数用于防止其他信号中断已注册的处理函数执行。处理程序代码修改全局 sig_atomic_t 类型的变量,该变量在子进程中停止 while 循环并打印 count 变量的值。请注意,在注册处理程序时,最好使用 signal 函数调用上面的 sigaction,因为 POSIX 标准没有详细规定后者。

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/wait.h>#include <signal.h>volatile sig_atomic_t shutdown_flag = 1;
void cleanupRoutine(int signal_number)
{
    shutdown_flag = 0;
}
int main(void) {
    int wstatus;
    pid_t c_pid = fork();
    if (c_pid == -1) {
        perror("fork");
        exit(EXIT_FAILURE);
    }
    if (c_pid == 0) {
        printf("printed from child process - %d\n", getpid());
        int count = 0;
        struct sigaction sigterm_action;
        memset(&sigterm_action, 0, sizeof(sigterm_action));
        sigterm_action.sa_handler = &cleanupRoutine;
        sigterm_action.sa_flags = 0;
        // Mask other signals from interrupting SIGTERM handler
        if (sigfillset(&sigterm_action.sa_mask) != 0)
        {
            perror("sigfillset");
            exit(EXIT_FAILURE);
        }
        // Register SIGTERM handler
        if (sigaction(SIGTERM, &sigterm_action, NULL) != 0)
        {
            perror("sigaction SIGTERM");
            exit(EXIT_FAILURE);
        }
        while (shutdown_flag) {
            count += 1;
        }
        printf("count = %d\n", count);
        exit(EXIT_SUCCESS);
    } else {
        printf("printed from parent process - %d\n", getpid());
        int ret;
        sleep(5);
        ret = kill(c_pid, SIGTERM);
        if (ret == -1) {
            perror("kill");
            exit(EXIT_FAILURE);
        }
        if (waitpid(c_pid, &wstatus, WUNTRACED | WCONTINUED) == -1) {
            perror("waitpid");
            exit(EXIT_FAILURE);
        }
    }
    exit(EXIT_SUCCESS);
}