Advertisement
Guest User

Untitled

a guest
May 24th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <sys/wait.h>
  6.  
  7. int main(int argc, char const *argv[]) {
  8.   int p1[2],p2[2],p3[2];
  9.   pipe(p1); pipe(p2); pipe(p3);
  10.   pid_t fiu = fork();
  11.   pid_t fiu2 = fork();
  12.   if(fiu==-1) {perror("nu"); exit(0);}
  13.   if(fiu2==-1) {perror("nu"); exit(1);}
  14.  
  15.   if(fiu==0 && fiu2!=0)
  16.   {
  17.     printf("fiu1\n");
  18.     close(p1[1]); close(p3[1]);
  19.     close(p2[0]); close(p3[0]);
  20.     char c;
  21.     int r,w;
  22.     while(1){
  23.       r=read(p1[0],&c,1);
  24.       if(r==-1) {perror("nu a putut citi date.txt"); exit(2);}
  25.       if(c>='a' && c<='z')
  26.        w=write(p2[1],&c,1);
  27.       if(w==-1) {perror("nu a putut fi scris pe canalul dintre fiu1 si tata"); exit(2);}
  28.       if(c=='\n') {write(p2[1],&c,1);break;}
  29.     }
  30.     close(p1[0]);
  31.     close(p2[1]);
  32.   }
  33.   else if(fiu2==0 && fiu!=0)
  34.   {
  35.     printf("fiu2\n");
  36.     int fd;
  37.     int nr[100];
  38.     for(int i=0;i<99;i++)nr[i]=0;
  39.     if(-1==(fd=open("statistica.txt",O_CREAT | O_TRUNC | O_WRONLY,0600))) {perror("nu"); exit(3);}
  40.     char c;
  41.     int a;
  42.     while(1){
  43.       read(p2[0],&c,1);
  44.       a=c-'a';
  45.       nr[a]++;
  46.       if(c=='\n') {break;}
  47.     }
  48.  
  49.     char nrLitDif='0';
  50.     int n='z'-'a'+1;
  51.     for(int j=0;j<n;j++)
  52.     {
  53.       if(nr[j]>0)
  54.       {
  55.         nrLitDif++;
  56.         c='a'+j;
  57.         write(fd,&c,1);
  58.         c=' ';
  59.         write(fd,&c,1);
  60.         c=nr[j]+'0';
  61.         write(fd,&c,1);
  62.         c='\n';
  63.         write(fd,&c,1);
  64.        // printf("%c %d\n",'a'+j,nr[j]);
  65.       }
  66.     }
  67.     close(p2[1]);
  68.     write(p3[1],&nrLitDif,1);
  69.     close(p3[1]);
  70.   }
  71.   else if(fiu!=0 && fiu2!=0){
  72.     printf("tata\n");
  73.     close(p1[0]);
  74.     close(p2[0]);
  75.     close(p2[1]);
  76.     close(p3[1]);
  77.     int fd;
  78.     if(-1==(fd=open("date.txt",O_RDONLY))) {perror("nu a putut citi date.txt"); exit(2);}
  79.     char c;
  80.     int r,w;
  81.     while(1){
  82.       r=read(fd,&c,1);
  83.       if(r==-1) {perror("nu a putut citi date.txt"); exit(2);}
  84.       w=write(p1[1],&c,1);
  85.       if(w==-1) {perror("nu a putut fi scris pe canalul dintre fiu1 si tata"); exit(2);}
  86.       if(c=='\n') break;
  87.     }
  88.     close(p1[1]);
  89.     read(p3[0],&c,1);
  90.     printf("NUMARUL DE CARA DIFERITE %c\n",c );
  91.     close(p3[0]);
  92.   }
  93.   return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement