小编kmk*_*kmk的帖子

我的POSIX信号处理程序中的竞争条件

以下程序分叉一个孩子,反复运行"/ bin/sleep 10".父级为SIGINT安装一个信号处理程序,它将SIGINT传递给子级.但是,有时向孩子发送SIGINT失败.为什么这样,我错过了什么?

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

pid_t foreground_pid = 0;

void sigint_handler(int sig)
{
    printf("sigint_handler: Sending SIGINT to process %d\n",
            foreground_pid);

    if ((foreground_pid != 0) && kill(foreground_pid, SIGCONT) == -1) {
        perror("sending SIGINT to forground process failed");
        printf("foreground_pid == %d", foreground_pid);
        exit(EXIT_FAILURE);
    }

    foreground_pid = 0;
}

int main(int argc, const char *argv[])
{
    while (1) {
        pid_t child_pid;

        if ((child_pid = fork()) == -1) {
            perror("fork failed");
            exit(EXIT_FAILURE); …
Run Code Online (Sandbox Code Playgroud)

c posix signals race-condition waitpid

4
推荐指数
1
解决办法
590
查看次数

标签 统计

c ×1

posix ×1

race-condition ×1

signals ×1

waitpid ×1