allocator

kr-2

Dec 20th, 2012
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.54 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <fcntl.h>
  6. #include <stdarg.h>
  7. #include <stdio.h>
  8. #include <sys/wait.h>
  9. #include <sys/sem.h>
  10. #include <time.h>
  11. #include <signal.h>
  12.  
  13. pid_t other_pid;
  14.  
  15. pid_t p1, p2, p3, p4;
  16.  
  17. void termhandler(int sgn) {
  18.     signal(SIGINT, termhandler);
  19.     exit(0);
  20. }
  21.  
  22. void hndlr1(int sgn) {
  23.     signal(SIGUSR1, hndlr1);
  24.     // printf("hndlr1\n");
  25.     // fflush(stdout);
  26.     static int cnt = 0;
  27.     ++cnt;
  28.     if (cnt < 2) {
  29.         return;
  30.     }
  31.     if (cnt == 2) {
  32.         kill(getppid(), SIGUSR1);
  33.         return;
  34.     }
  35.     if (cnt == 3) {
  36.         printf("1");
  37.         fflush(stdout);
  38.         kill(getppid(), SIGUSR1);
  39.     }
  40.     if (cnt == 4) {
  41.         kill(p3, SIGUSR1);
  42.         return;
  43.     }
  44.     if (cnt == 5) {
  45.         kill(p4, SIGUSR1);
  46.         return;
  47.     }
  48.     if (cnt == 6) {
  49.         kill(getppid(), SIGUSR1);
  50.     }
  51.     return;
  52. }
  53.  
  54. void hndlr2(int sgn) {
  55.     signal(SIGUSR1, hndlr2);
  56.     // printf("hndlr2\n");
  57.     // fflush(stdout);
  58.     static int cnt = 0;
  59.     ++cnt;
  60.     if (cnt == 1) {
  61.         kill(p1, SIGUSR1);
  62.         return;
  63.     }
  64.     if (cnt == 2) {
  65.         printf("2");
  66.         fflush(stdout);
  67.         kill(p1, SIGUSR1);
  68.         return;
  69.     }
  70.     if (cnt == 3) {
  71.         kill(0, SIGINT);
  72.     }
  73.     return;
  74. }
  75.  
  76. void hndlr3(int sgn) {
  77.     signal(SIGUSR1, hndlr2);
  78.     // printf("hndlr3\n");
  79.     // fflush(stdout);
  80.     static int cnt = 0;
  81.     ++cnt;
  82.     if (cnt == 1) {
  83.         printf("3");
  84.         fflush(stdout);
  85.         kill(getppid(), SIGUSR1);
  86.         return;
  87.     }
  88.     return;
  89. }
  90.  
  91. void hndlr4(int sgn) {
  92.     signal(SIGUSR1, hndlr2);
  93.     // printf("hndlr4\n");
  94.     // fflush(stdout);
  95.     static int cnt = 0;
  96.     ++cnt;
  97.     if (cnt == 1) {
  98.         printf("4");
  99.         fflush(stdout);
  100.         kill(getppid(), SIGUSR1);
  101.         return;
  102.     }
  103.     return;
  104. }
  105.  
  106.  
  107. int main(int argc, char **argv) {
  108.     signal(SIGINT, termhandler);
  109.     signal(SIGUSR1, hndlr2);
  110.     p2 = getpid();
  111.     p1 = fork();
  112.     if (p1 == 0) {
  113.         signal(SIGUSR1, hndlr1);
  114.         p3 = fork();
  115.         if (p3 == 0){
  116.             signal(SIGUSR1, hndlr3);
  117.             kill(getppid(), SIGUSR1);
  118.             for(;;)
  119.                 sleep(1);
  120.         }
  121.         p4 = fork();
  122.         if (p4 == 0) {
  123.             signal(SIGUSR1, hndlr4);
  124.             kill(getppid(), SIGUSR1);
  125.             for(;;)
  126.                 sleep(1);
  127.         }
  128.         for(;;)
  129.             sleep(1);
  130.     }
  131.     for (;;)
  132.         sleep(1);
  133.     return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment