Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending signal from child to parent

Tags:

c

signals

Here I want to send SIGINT signal to parent while it is sleeping. 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? 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.

The output is printing only this much content. I think parent is not executing at all.

like image 876
Vijesh Avatar asked Jan 03 '23 06:01

Vijesh


2 Answers

You need to set the signal handler before SIGINT is sent to the parent process, otherwise, the handler will not be executed. 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.

#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;
}
like image 93
id01 Avatar answered Jan 16 '23 07:01

id01


When you send SIGINT signal has not been called yet.

I think you want to set signal handler before sending 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;
}
like image 32
cshu Avatar answered Jan 16 '23 07:01

cshu