Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8.  
  9. int main(int argc, char* argv[]){
  10.     int pipe1[2], pipe2[2], sync[2];
  11.  
  12.     int pid1;
  13.     int pid2;
  14.  
  15.     if(((pipe(pipe1)) < 0) || ((pipe(pipe2)) <0) || ((pipe(sync)) <0)){
  16.         perror("errore pipe");
  17.         exit(1);
  18.     }
  19.  
  20.     if((pid1 = fork()) < 0){
  21.         perror("errore fork");
  22.         exit(1);
  23.     }
  24.  
  25.     if(pid1 == 0){  //primo figlio
  26.         int inputF;
  27.         if((inputF = open(argv[1], O_RDONLY)) < 0){
  28.             perror("errore open input");
  29.             exit(1);
  30.         }
  31.  
  32.         char buff[2];
  33.         char outBuff1[4];
  34.  
  35.         close(pipe1[0]);
  36.         close(sync[0]);
  37.  
  38.         int nread = 0;
  39.         while(read(inputF, buff, 2) == 2){
  40.             if(nread == 4){
  41.                 write(sync[1], "S", 1);
  42.                 nread = 0;
  43.             }
  44.             else{
  45.                 write(sync[1], "N", 1);
  46.                 nread += 2;
  47.             }
  48.  
  49.             write(pipe1[1], buff, 2);
  50.  
  51.         }
  52.  
  53.         close(pipe1[1]);
  54.         close(sync[1]);
  55.         close(inputF);
  56.     }
  57.  
  58.     else{
  59.         if((pid2 = fork()) < 0){
  60.             perror("errore seconda fork");
  61.             exit(1);
  62.         }
  63.  
  64.         if(pid2 == 0){ //secondo figlio
  65.             close(pipe2[1]);
  66.  
  67.             char buff_2[4];
  68.             char outBuff[2];
  69.             int outputF;
  70.             if((outputF = open(argv[2], O_RDWR)) < 0){
  71.                 perror("errore apertura output");
  72.                 exit(1);
  73.             }
  74.             while(read(pipe2[0], buff_2, 4) == 4){
  75.                 outBuff[0] = buff_2[0];
  76.                 outBuff[1] = buff_2[3];
  77.                 write(outputF, outBuff, 2);
  78.             }
  79.             close(pipe2[0]);
  80.             close(outputF);
  81.         }
  82.  
  83.         else{   //padre
  84.             char buff_padre[4];
  85.             char flag;
  86.             close(pipe1[1]);
  87.             close(pipe2[0]);
  88.             close(sync[1]);
  89.  
  90.             while(read(sync[0], &flag, 1) == 1){
  91.                 if(flag == 'S'){
  92.                     if(read(pipe1[0], buff_padre, 4) == 4){
  93.                         if(buff_padre[0] != buff_padre[3])
  94.                             write(pipe2[1], buff_padre, 4);
  95.                     }
  96.                 }
  97.             }
  98.  
  99.             close(pipe1[0]);
  100.             close(pipe2[1]);
  101.             close(sync[0]);
  102.         }
  103.     }
  104.  
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement