信号发送到两个孩子,父进程 [英] Signal sent to both child and parent process

查看:98
本文介绍了信号发送到两个孩子,父进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解发送到父进程的信号不应被送到儿童。那么,为什么信号情报达到两个孩子,并在实施例的父以下

 的#include<&stdio.h中GT;
#包括LT&;&signal.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&unistd.h中GT;无效sigCatcher(INT);诠释主要(无效){
    如果(信号(SIGINT,sigCatcher)== SIG_ERR){
        fprintf中(标准错误,无法注册信号处理程序\\ n);
        出口(1);
    }
    如果(叉()== 0){
        的char * argv的[] = {查找,/,NULL。};
        execvp(查找,argv的);
    }
    为(;;){
        睡眠(10);
        写(STDOUT_FILENO,W \\ N,3);
    }    返回0;
}无效sigCatcher(INT theSignal){
        写(STDOUT_FILENO,C:\\ N,3);
}


解决方案

如果您是通过键入^ -C发送SIGINT,信号在前台处理组发送到所有进程。如果你使用杀-2 ,它只会去到父(或任何过程中,你指示。)

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

解决方案

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.)

这篇关于信号发送到两个孩子,父进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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