Advertisement
romko11l

Untitled

Nov 26th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/wait.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6.  
  7. int a,b,p,fd[2],temp;
  8.  
  9. void hndlr(int s)
  10. {
  11.     sleep(1);
  12.     read(fd[0], &temp, sizeof(int));
  13.     printf("Пишет процесс %d : %d\n", getpid(), temp);
  14.     temp++;
  15.     write(fd[1], &temp, sizeof(int));
  16.     kill(p,SIGUSR1);
  17. }
  18.  
  19.  
  20. int main()
  21. {
  22.     // a->parent->b->a...
  23.     signal(SIGUSR1, hndlr);
  24.     pipe(fd);
  25.     a=fork();
  26.     if (a==0) // сын a
  27.     {
  28.         p=getppid();
  29.         temp=1;
  30.         write(fd[1],&temp, sizeof(int));
  31.         kill(p,SIGUSR1);
  32.         while (1)
  33.         {
  34.         }
  35.     }
  36.     else
  37.     {
  38.         b=fork();
  39.         if (b==0) // сын b
  40.         {
  41.             p=a;
  42.             while(1)
  43.             {
  44.             }
  45.         }
  46.         else //parent
  47.         {
  48.             p=b;
  49.             while(1)
  50.             {
  51.             }
  52.         }
  53.     }
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement