Advertisement
andruhovski

unix0104b

Feb 17th, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.23 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <signal.h>
  5. #include <unistd.h>
  6. #define TIMEOUT 10
  7. extern int f1(int), f2(int), f3(int);
  8. int pid0, pid1, pid2;
  9. void main(void)
  10. {
  11.     setpgrp();
  12.     pid0 = getpid();
  13.     pid1 = fork();
  14.     if (pid1 == 0) /* process 1 */
  15.     {
  16.         signal(SIGUSR1, f1);
  17.         pid1 = getpid();
  18.         pid2 = fork();
  19.         if (pid2 < 0 ) puts("Fork error");
  20.         if (pid2 > 0) for(;;);
  21.         else /* process 2 */
  22.         {
  23.             signal(SIGUSR2, f2);
  24.             pid2 = getpid();
  25.             kill(pid1,SIGUSR1);
  26.             for (;;);
  27.         }
  28.     }
  29.     else /* process 0 */
  30.     {
  31.         signal(SIGALRM, f3);
  32.         alarm(TIMEOUT);
  33.         pause();
  34.     }
  35.     exit(0);
  36. }
  37. int f1(int signum)
  38. {
  39.     signal(SIGUSR1, f1);
  40.     printf("Process 1 (%d) has got à signal from process 2 (%d)\n",pid1,pid2);
  41.     sleep(1);
  42.     kill(pid2, SIGUSR2);
  43.     return 0;
  44. }
  45. int f2(int signum)
  46. {
  47.     signal(SIGUSR2, f2);
  48.     printf("Process 2 (%d) has got à signal from process 1 (%d)\n",pid2,pid1);
  49.     sleep(1);
  50.     kill(pid1, SIGUSR1);
  51.     return 0;
  52. }
  53. int f3(int signum)
  54. {
  55.     printf("End of job - %d\n", pid0);
  56.     kill(0, SIGKILL);
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement