Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.90 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.         write(sync[1], "E", 1);
  54.         close(pipe1[1]);
  55.         close(sync[1]);
  56.         close(inputF);
  57.     }
  58.  
  59.     else{
  60.         if((pid2 = fork()) < 0){
  61.             perror("errore seconda fork");
  62.             exit(1);
  63.         }
  64.  
  65.         if(pid2 == 0){ //secondo figlio
  66.             close(pipe2[1]);
  67.  
  68.             char buff_2[4];
  69.             char outBuff[2];
  70.             int outputF;
  71.             if((outputF = open(argv[2], O_RDWR)) < 0){
  72.                 perror("errore apertura output");
  73.                 exit(1);
  74.             }
  75.             while(read(pipe2[0], buff_2, 4) == 4){
  76.                 outBuff[0] = buff_2[0];
  77.                 outBuff[1] = buff_2[3];
  78.                 write(outputF, outBuff, 2);
  79.             }
  80.             close(pipe2[0]);
  81.             close(outputF);
  82.         }
  83.  
  84.         else{   //padre
  85.             char buff_padre[4];
  86.             char flag;
  87.             close(pipe1[1]);
  88.             close(pipe2[0]);
  89.             close(sync[1]);
  90.  
  91.             while(read(sync[0], &flag, 1) == 1){
  92.                 if(flag == 'S'){
  93.                     if(read(pipe1[0], buff_padre, 4) == 4){
  94.                         if(buff_padre[0] != buff_padre[3])
  95.                             write(pipe2[1], buff_padre, 4);
  96.                     }
  97.                 }
  98.                 else if (flag == 'E')
  99.                     break;
  100.             }
  101.  
  102.             close(pipe1[0]);
  103.             close(pipe2[1]);
  104.             close(sync[0]);
  105.         }
  106.     }
  107.  
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement