Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Q1:
- https://d2vlcm61l7u1fs.cloudfront.net/media%2Fcee%2Fceea072b-0857-447b-b62b-92acb2f7d0f4%2FphpWaABFp.png
- https://d2vlcm61l7u1fs.cloudfront.net/media%2F0ad%2F0adf5ffc-2f7f-4d0e-99c8-dff08e34a4b3%2FphpZnX2VB.png
- Q2:
- https://d2vlcm61l7u1fs.cloudfront.net/media%2F91b%2F91b8b33d-feb1-4029-8b5a-7d3505f664f6%2Fphpv4ZL0I.png
- https://d2vlcm61l7u1fs.cloudfront.net/media%2F9ed%2F9ed61264-5b6a-4d70-a676-1271c5bdf4b9%2FphpYfCUEN.png
- Q3:
- The process sleeps for 5 seconds after receiving each signal.
- Here is the working code:
- #include <stdio.h>
- #include <sys/types.h>
- #include <signal.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- // #include <signal.h>
- #include <stdlib.h>
- #include <unistd.h>
- void sighup(int); /* routines child will call upon sigtrap */
- void sigint(int);
- void sigquit(int);
- void main(void)
- {
- signal(SIGHUP,sighup); /* set function calls */
- signal(SIGINT,sigint);
- signal(SIGQUIT, sigquit);
- int pid;
- if ((pid = fork()) < 0) {
- perror("fork");
- exit(1);
- }
- if(pid==0){
- for(;;); /* loop for ever */
- }
- else {
- signal(SIGHUP, SIG_DFL);
- signal(SIGINT, SIG_DFL);
- signal(SIGQUIT, SIG_DFL);
- /* parent */
- /* pid hold id of child */
- printf("\nPARENT: sending SIGHUP\n\n");
- kill(pid,SIGHUP);
- sleep(5); /* pause for 5 secs */
- printf("\nPARENT: sending SIGINT\n\n");
- kill(pid,SIGINT);
- sleep(5); /* pause for 5 secs */
- printf("\nPARENT: sending SIGQUIT\n\n");
- kill(pid,SIGQUIT);
- sleep(5);
- }
- }
- void sighup(int signo) {
- signal(SIGHUP,sighup); /* reset signal */
- printf("CHILD: I have received a SIGHUP\n");
- }
- void sigint(int signo) {
- signal(SIGINT,sigint); /* reset signal */
- printf("CHILD: I have received a SIGINT\n");
- }
- void sigquit(int signo) {
- printf("I have received SIGQUIT\n");
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement