简体   繁体   English

从孩子到父母发送信号

[英]Sending signal from child to parent

Here I want to send SIGINT signal to parent while it is sleeping. 在这里,我想在父母睡觉时向他们发送SIGINT信号。 I have tried it by writing following the program. 我通过编写程序来尝试它。 In this program, I am not getting why the signal handler for SIGINT from the parent is not executing at all? 在这个程序中,我不明白为什么来自父节点的SIGINT的信号处理程序根本没有执行? here is the code: 这是代码:

#include<stdio.h>
#include<signal.h>
#include<unistd.h>
#include<stdlib.h>
void sig_usr(int signo){
    if(signo == SIGINT)
    printf("Signal caught!");
    return;
}

int main(void){
    pid_t pid, ppid;
    ppid = getpid();
    printf("ppid = %d\n", ppid);
    if((pid = fork()) == 0){ 
        printf("killing parent...\n");
        kill(ppid, SIGINT);
        printf("After killing parent...\n");
    }
    else{
        sleep(5);
        printf("%d %d ",ppid, pid);
        if(signal(SIGINT,sig_usr) == SIG_ERR)
            printf("Signal processed ");
    }
    return 0;
}

Output: The output is printing only this much content. 输出:输出仅打印这么多内容。 I think parent is not executing at all. 我认为父母根本没有执行。

输出只打印这么多内容。我认为父母根本没有执行。

You need to set the signal handler before SIGINT is sent to the parent process, otherwise, the handler will not be executed. 您需要在将SIGINT发送到父进程之前设置信号处理程序,否则将不会执行处理程序。 Also, the parent process is being killed before it executes anything. 此外,父进程在执行任何操作之前都被杀死。 The easy way to fix this would be to move the sleep call after the code for the parent process, and add a delay to the child process. 解决这个问题的简单方法是在父进程的代码之后移动sleep调用,并为子进程添加延迟。

#include<stdio.h>
#include<signal.h>
#include<unistd.h>
#include<stdlib.h>
void sig_usr(int signo){
    if(signo == SIGINT)
    printf("Signal caught!");
    return;
}

int main(void){
    pid_t pid, ppid;
    ppid = getpid();
    printf("ppid = %d\n", ppid);
    if((pid = fork()) == 0){ 
        sleep(1); // Wait for parent to finish setting up
        printf("killing parent...\n");
        kill(ppid, SIGINT);
        printf("After killing parent...\n");
    }
    else{
        printf("%d %d ",ppid, pid);
        if(signal(SIGINT,sig_usr) == SIG_ERR)
            printf("Signal processed ");
        sleep(5); // Wait to be killed
    }
    return 0;
}

When you send SIGINT signal has not been called yet. 当您发送SIGINT signal尚未被调用时。

I think you want to set signal handler before sending SIGINT: 我想你想在发送SIGINT之前设置信号处理程序:

#include<stdio.h>
#include<signal.h>
#include<unistd.h>
#include<stdlib.h>
void sig_usr(int signo){
    if(signo == SIGINT)
    printf("Signal caught!");
    return;
}

int main(void){
    pid_t pid, ppid;
    ppid = getpid();
    printf("ppid = %d\n", ppid);
    if((pid = fork()) == 0){ 
        sleep(1);
        printf("killing parent...\n");
        kill(ppid, SIGINT);
        printf("After killing parent...\n");
    }
    else{
        printf("%d %d ",ppid, pid);
        if(signal(SIGINT,sig_usr) == SIG_ERR)
            printf("Signal processed ");
        sleep(5);
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM