Advertisement
Guest User

Untitled

a guest
Jun 19th, 2021
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. Q1:
  2.  
  3. https://d2vlcm61l7u1fs.cloudfront.net/media%2Fcee%2Fceea072b-0857-447b-b62b-92acb2f7d0f4%2FphpWaABFp.png
  4.  
  5. https://d2vlcm61l7u1fs.cloudfront.net/media%2F0ad%2F0adf5ffc-2f7f-4d0e-99c8-dff08e34a4b3%2FphpZnX2VB.png
  6.  
  7. Q2:
  8.  
  9. https://d2vlcm61l7u1fs.cloudfront.net/media%2F91b%2F91b8b33d-feb1-4029-8b5a-7d3505f664f6%2Fphpv4ZL0I.png
  10.  
  11. https://d2vlcm61l7u1fs.cloudfront.net/media%2F9ed%2F9ed61264-5b6a-4d70-a676-1271c5bdf4b9%2FphpYfCUEN.png
  12.  
  13. Q3:
  14.  
  15. The process sleeps for 5 seconds after receiving each signal.
  16.  
  17. Here is the working code:
  18.  
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. #include <signal.h>
  22. #include <sys/ipc.h>
  23. #include <sys/shm.h>
  24. // #include <signal.h>
  25. #include <stdlib.h>
  26. #include <unistd.h>
  27.  
  28.  
  29. void sighup(int); /* routines child will call upon sigtrap */
  30. void sigint(int);
  31. void sigquit(int);
  32. void main(void)
  33. {
  34. signal(SIGHUP,sighup); /* set function calls */
  35. signal(SIGINT,sigint);
  36. signal(SIGQUIT, sigquit);
  37.  
  38. int pid;
  39. if ((pid = fork()) < 0) {
  40. perror("fork");
  41. exit(1);
  42. }
  43. if(pid==0){
  44.  
  45. for(;;); /* loop for ever */
  46. }
  47. else {
  48. signal(SIGHUP, SIG_DFL);
  49. signal(SIGINT, SIG_DFL);
  50. signal(SIGQUIT, SIG_DFL);
  51.  
  52. /* parent */
  53. /* pid hold id of child */
  54. printf("\nPARENT: sending SIGHUP\n\n");
  55. kill(pid,SIGHUP);
  56. sleep(5); /* pause for 5 secs */
  57. printf("\nPARENT: sending SIGINT\n\n");
  58. kill(pid,SIGINT);
  59. sleep(5); /* pause for 5 secs */
  60. printf("\nPARENT: sending SIGQUIT\n\n");
  61. kill(pid,SIGQUIT);
  62. sleep(5);
  63. }
  64. }
  65. void sighup(int signo) {
  66. signal(SIGHUP,sighup); /* reset signal */
  67. printf("CHILD: I have received a SIGHUP\n");
  68. }
  69.  
  70. void sigint(int signo) {
  71. signal(SIGINT,sigint); /* reset signal */
  72. printf("CHILD: I have received a SIGINT\n");
  73. }
  74.  
  75. void sigquit(int signo) {
  76. printf("I have received SIGQUIT\n");
  77. exit(0);
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement