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

Create a program that creates a child process and the child process prints when

ID: 3694062 • Letter: C

Question

Create a program that creates a child process and the child process prints when it is created and then goes into a infinite loop to wait for a signal. In the main function for the parent process, list several signals the the user can choose from to send to the child process, get the number for the signal from the user, and send it to the child process. When the child process receives a signal, print out that it has and which signal it received. Have the parent process wait on the child to process to finish and then print out "All done!".

Explanation / Answer

Answer:

#include <stdio.h>
#include <signal.h>

void sighup(); /* routines child will call upon sigtrap */
void sigint();
void sigquit();

main()
{ int pid;

/* get child process */

if ((pid = fork()) < 0) {
perror("fork");
exit(1);
}

if (pid == 0)
{ /* child */
signal(SIGHUP,sighup­); /* set function calls */
signal(SIGINT,sigint­);
signal(SIGQUIT, sigquit);
for(;;); /* loop for ever */
}
else /* parent */
{ /* pid hold id of child */
printf(" PARENT: sending SIGHUP ");
kill(pid,SIGHUP);
sleep(3); /* pause for 3 secs */
printf(" PARENT: sending SIGINT ");
kill(pid,SIGINT);
sleep(3); /* pause for 3 secs */
printf(" PARENT: sending SIGQUIT ");
kill(pid,SIGQUIT);
sleep(1000);
}
}

void sighup()

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

void sigint()

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

void sigquit()

{ printf("All done!!! ");
exit(0);
}

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