In: Computer Science
Linux Create two subprocesses using the system call fork(), and then use the system call signal() to let the parent process capture the interrupt signal on the keyboard (i.e., press the DEL key); The subprocess terminates after the subprocess captures the signal and outputs the following information separately: “Child Process l is Killed by Parent!” “Child Process 2 is Killed by Parent!” After the two child processes terminate, the parent process output the following information and terminate itself: “Parent Process is Killed!” Program with screen shot and explanation. Thank you
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
void sigint() // signal Handler
{
signal(SIGINT, sigint); /* reset signal */
printf("CHILD: I have received a SIGINT %d \n",
getpid());
}
int main()
{
pid_t child_a, child_b;
child_a = fork();
if (child_a == 0)
{
/* Child A code */
printf("First Child
%d \n", getpid());
signal(SIGINT,
sigint); // Register signal
pause(); //
Wait for signal
} else {
child_b = fork();
if (child_b == 0)
{
/* Child B code */
printf("Second Child %d \n", getpid());
signal(SIGINT, sigint); // Register signal
pause(); // wait for signal
} else {
/* Parent Code */
sleep(5); // Lets child run first
printf("Parent Start %d \n", getpid());
kill(child_a, SIGINT); // send signal to first
child
kill(child_b, SIGINT); // send signal to second
child
wait(NULL); // wait for all child termination
printf("Parent END %d \n", getpid());
}
}
return 0;
}
******************* OUTPUT *****************
First Child 23827
Second Child 23828
Parent Start 23826
CHILD: I have received a SIGINT 23827
CHILD: I have received a SIGINT 23828
Fisrt child start after Pause
Second child start after Pause
Parent END 23826