Signal sent to both child and parent process

点点圈 提交于 2019-12-06 07:42:01

问题


As far as I understand signals sent to a parent process should not be sent to children. So why does SIGINT reach both the child and the parent in the example below?

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

void sigCatcher( int );

int main ( void ) {
    if (signal(SIGINT, sigCatcher) == SIG_ERR) {
        fprintf(stderr, "Couldn't register signal handler\n");
        exit(1);
    }
    if(fork() == 0) {
        char *argv[] = {"find","/",".",NULL};
        execvp("find",argv);
    }
    for (;;) {
        sleep(10);
        write(STDOUT_FILENO, "W\n",3);
    }

    return 0;
}

void sigCatcher( int theSignal ) {
        write(STDOUT_FILENO, "C\n",3);
}

回答1:


If you are sending SIGINT by typing ^-C, the signal is sent to all processes in the foreground processing group. If you use kill -2, it will only go to the parent (or whichever process you indicate.)



来源:https://stackoverflow.com/questions/12897306/signal-sent-to-both-child-and-parent-process

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!