Guest User

Untitled

a guest
Aug 13th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 1.36 KB | None | 0 0
  1.  #include <stdio.h>
  2. #include <sys/wait.h>
  3. #include <assert.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7.  
  8. int main(int argc, char *argv[])
  9.  
  10. {
  11.  
  12.     int pfd[2];
  13.     int pfd_2[2];
  14.     int x=0, y=0;
  15.     pid_t cpid;
  16.     struct liczby
  17.     {
  18.         int x;
  19.         int y;
  20.     };
  21.    
  22.     struct liczby bufor_dz, bufor_o;
  23.     bufor_dz.x=0;
  24.     bufor_dz.y=0;
  25.     bufor_o.x=0;
  26.     bufor_o.y=0;    
  27.     int suma=0;
  28.     int buf;
  29.     int suma_dziecko=0;
  30.  
  31.     if (pipe(pfd) == -1) { perror("pipe"); exit(EXIT_FAILURE); }
  32.     if (pipe(pfd_2) == -1) { perror("pipe_2"); exit(EXIT_FAILURE); }
  33.     cpid = fork();
  34.     if (cpid == -1) { perror("fork"); exit(EXIT_FAILURE); }
  35.     if (cpid == 0)
  36.     {  
  37.  
  38.         close(pfd[0]);  
  39.  
  40.  
  41.         scanf("%d %d", &bufor_dz.x, &bufor_dz.y);
  42.         write(pfd[1], &bufor_dz, sizeof(struct liczby));
  43.  
  44.     close(pfd_2[1]);          
  45.         read(pfd_2[0], &suma_dziecko, sizeof(struct liczby));
  46.         printf("%d\n", suma_dziecko);
  47.  
  48.         _exit(EXIT_SUCCESS);
  49.  
  50.     }
  51.     else
  52.     {          
  53.  
  54.         close(pfd[1]);
  55.         read(pfd[0], &bufor_o, sizeof(struct liczby));
  56.         suma=bufor_o.x+bufor_o.y;
  57.  
  58.  
  59.     close(pfd_2[0]);  
  60.         write(pfd_2[1], &suma, sizeof(int));
  61.  
  62.         wait(NULL);            
  63.  
  64.         exit(EXIT_SUCCESS);
  65.  
  66.     }
  67.  
  68. }
Add Comment
Please, Sign In to add comment