简体   繁体   中英

Not receiving signal from the parent process

I am learning communication between two process using signals in C on GeeksForGeeks https://www.geeksforgeeks.org/signals-c-set-2/?ref=lbp . And I was trying to run the code provided on the website. I simply copy the code to an online C compiler.

// C program to implement sighup(), sigint() 
// and sigquit() signal functions 
#include <signal.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <sys/types.h> 
#include <unistd.h> 

// function declaration 
void sighup(); 
void sigint(); 
void sigquit(); 

// driver code 
void main() 
{ 
    int pid; 

    /* get child process */
    if ((pid = fork()) < 0) { 
        perror("fork"); 
        exit(1); 
    } 

    if (pid == 0) { /* child */
        signal(SIGHUP, sighup); 
        signal(SIGINT, sigint); 
        signal(SIGQUIT, sigquit); 
        for (;;) 
            ; /* loop for ever */
    } 

    else /* parent */
    { /* pid hold id of child */
        printf("\nPARENT: sending SIGHUP\n\n"); 
        kill(pid, SIGHUP); 

        sleep(3); /* pause for 3 secs */
        printf("\nPARENT: sending SIGINT\n\n"); 
        kill(pid, SIGINT); 

        sleep(3); /* pause for 3 secs */
        printf("\nPARENT: sending SIGQUIT\n\n"); 
        kill(pid, SIGQUIT); 
        sleep(3); 
    } 
} 

// sighup() function definition 
void sighup() 

{ 
    signal(SIGHUP, sighup); /* reset signal */
    printf("CHILD: I have received a SIGHUP\n"); 
} 

// sigint() function definition 
void sigint() 

{ 
    signal(SIGINT, sigint); /* reset signal */
    printf("CHILD: I have received a SIGINT\n"); 
} 

// sigquit() function definition 
void sigquit() 
{ 
    printf("My DADDY has Killed me!!!\n"); 
    exit(0); 
} 

However, all I got is like this

PARENT: sending SIGHUP                                                                                                                                                             
                                                                                                                                                                                   
                                                                                                                                                                                   
PARENT: sending SIGINT                                                                                                                                                             
                                                                                                                                                                                   
                                                                                                                                                                                   
PARENT: sending SIGQUIT

I am wondering is it my computer problem?

The code incorrectly assumes that the child installs the handlers before the parent sends the signals. It could be so, but it is not guaranteed. Of course, if the handlers are not installed, the child is killed immediately. You may confirm that by testing that kill returns -1 and errno is set to ESRCH .

Also keep in mind that printf is not signal safe.

Not all online C compilers/environments are made equally. Try repl.it, I just tried it, and got the exact results that geeks4geeks posted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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