C   81

Sigtalk

Guest on 25th April 2022 10:26:02 PM

  1. /* sigtalk.c Example of how 2 processes can talk */
  2. /* compile with gcc: cc sigtalk.c -o sigtalk */
  3.  
  4. /* written by sHaDy */
  5. /* NOTE: mmmhhh... se vi va mettete qualche commento al codice */
  6.  
  7.  
  8. #include <signal.h>
  9. #include <stdio.h>
  10.  
  11.  
  12. void sighup()
  13.  
  14. {  signal(SIGHUP,sighup);
  15.    printf("CHILD: I have received a SIGHUP\n");
  16. }
  17.  
  18. void sigint()
  19.  
  20. {  signal(SIGINT,sigint);
  21.    printf("CHILD: I have received a SIGINT\n");
  22. }
  23.  
  24. void sigquit()
  25.  
  26. { printf("Ahi! Somebody has killed me!\n");
  27.   exit(0);
  28. }
  29.  
  30. void sighup();
  31. void sigint();
  32. void sigquit();
  33.  
  34. main()
  35. { int pid;
  36.  
  37.  
  38.    if ((pid = fork()) < 0) {
  39.                 perror("fork");
  40.                 exit(1);
  41.     }
  42.    
  43.    if (pid == 0)
  44.      {
  45.                 signal(SIGHUP,sighup);
  46.                 signal(SIGINT,sigint);
  47.                 signal(SIGQUIT, sigquit);
  48.                 for(;;);
  49.      }
  50.   else
  51.      {  
  52.                 printf("\nPARENT: sending SIGHUP\n\n");
  53.                 kill(pid,SIGHUP);
  54.                 sleep(3);
  55.                 printf("\nPARENT: sending SIGINT\n\n");
  56.                 kill(pid,SIGINT);
  57.                 sleep(3);
  58.                 printf("\nPARENT: sending SIGQUIT\n\n");
  59.                 kill(pid,SIGQUIT);
  60.                 sleep(3);
  61.  
  62. }
  63. }

Raw Paste


Login or Register to edit or fork this paste. It's free.