Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Why won\'t the terminal print out the Childs statements? And how can i fix this?

ID: 3911117 • Letter: W

Question

Why won't the terminal print out the Childs statements? And how can i fix this? (ie: i need the statements in 'void sighup()', 'void sigint(), 'void sigquit()' to print out)

#include

#include

#include

#include

#include

void sighup();

void sigint();

void sigquit();

main() {

pid_t pid;

            if ((pid = fork()) == 0) {   

                        signal(SIGHUP, sighup);

                        signal(SIGINT, sigint);

                        signal(SIGQUIT, sigquit);

//Infinite loop

                        for(;;);

            } else {

                        printf(" Parent is sending SIGHUP ");

                        kill (pid,SIGHUP);

                        sleep(5);

                        printf(" Parent is sending SIGINT ");

                        kill (pid,SIGINT);

                        sleep(5);

                        printf(" Parent is sending SIGQUIT ");

                        kill (pid,SIGQUIT);

                        sleep(5);

            }

}

void sighup() {

   printf(" CHILD: I have received a SIGHUP ");

}

void sigint() {

   printf(" CHILD: I have received a SIGINT ");

}

void sigquit() {

printf(" CHILD: I have received SIGQUIT");

exit(0);

}

Explanation / Answer

In order to receive the signals, you will need to setup the handlers even before forking. Given below is the modified code. Please do rate the answer if it helped. Thank you
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>

void sighup(int sig);

void sigint(int sig);

void sigquit(int sig);

int main() {

pid_t pid;

signal(SIGHUP, sighup);
signal(SIGINT, sigint);
signal(SIGQUIT, sigquit);

if ((pid = fork()) == 0) {
//Infinite loop

for(;;);

} else {

printf(" Parent is sending SIGHUP ");
kill (pid,SIGHUP);
sleep(3);

printf(" Parent is sending SIGINT ");
kill (pid,SIGINT);
sleep(3);

printf(" Parent is sending SIGQUIT ");
kill (pid,SIGQUIT);

}

}

void sighup(int sig) {
printf("CHILD: I have received a SIGHUP ");
}

void sigint(int sig) {

printf("CHILD: I have received a SIGINT ");

}

void sigquit(int sig) {
printf("CHILD: I have received SIGQUIT ");
exit(0);
}


output
------
Parent is sending SIGHUP
CHILD: I have received a SIGHUP

Parent is sending SIGINT
CHILD: I have received a SIGINT

Parent is sending SIGQUIT
CHILD: I have received SIGQUIT

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote