Advertisement
Guest User

Untitled

a guest
May 24th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <sys/wait.h>
  5. #include <unistd.h>
  6. #include <stdio.h>
  7. #include <fcntl.h>
  8. #include <errno.h>
  9. #include <stdlib.h>
  10. #include <string.h> //nu era nevoie de toate astea
  11.  
  12. int afisareReplicaTata();
  13. int afisareReplicaFiu();
  14. int fd1,fd2,flag;
  15. int main(int argc,char*argv[])
  16. {
  17.   if(argc<4){perror("da 3 argumente: tata fiu si flag"); exit(0);}
  18.   if(-1==(fd1=open(argv[1],O_RDONLY))){perror("nu pot citi tata"); exit(0);}
  19.   if(-1==(fd2=open(argv[2],O_RDONLY))){perror("nu pot citi fiu"); exit(0);}
  20.   if(-1==(flag=open(argv[3],O_RDWR | O_TRUNC))){perror("nu pot citi flag"); exit(0);}
  21.  
  22.   pid_t pid_fiu;
  23.   if((pid_fiu=fork())==-1){perror("eroare la fork"); exit(1);}
  24.  
  25.   else if(pid_fiu==0) //fiu
  26.   {
  27.     char c='a';
  28.     while(1) //cat timp nu e eof
  29.     {
  30.       lseek(flag,0,SEEK_SET);
  31.       read(flag,&c,1);
  32.       if(c=='2')
  33.       {
  34.         while(afisareReplicaFiu()!=1);
  35.         break;
  36.       }
  37.       if(c=='1')
  38.       {
  39.         if(afisareReplicaFiu()==1)
  40.           break;
  41.       }
  42.       sleep(1);
  43.     }
  44.   }
  45.  
  46.   else //tata
  47.   {
  48.     char c='a';
  49.     afisareReplicaTata();
  50.     while(1) //cat timp nu e eof
  51.     {
  52.       lseek(flag,0,SEEK_SET);
  53.       read(flag,&c,1);
  54.       if(c=='2')
  55.       {
  56.         while(afisareReplicaTata()!=1);
  57.         break;
  58.       }
  59.       if(c=='0')
  60.       {
  61.         if(afisareReplicaTata()==1)
  62.           break;
  63.       }
  64.       else if(c=='2') break;
  65.       sleep(1);
  66.     }
  67.   }
  68.   close(fd1);
  69.   close(fd2);
  70.   return 0;
  71. }
  72.  
  73. int afisareReplicaTata()
  74. {
  75.   char c='a';
  76.   char*replica=malloc(100);
  77.   int i=0;
  78.   while(1){
  79.     read(fd1,&c,1);
  80.     if(c=='\n'){break;}
  81.     replica[i]=c; i++;
  82.   }
  83.   printf("%s \n", replica);
  84.  
  85.   int val=0;
  86.   if(read(fd1,&c,1)==0)
  87.    {c='2'; val=1;}
  88.    else c='1';
  89.  
  90.   lseek(fd1,-1,SEEK_CUR);
  91.   lseek(flag,0,SEEK_SET);
  92.   write(flag,&c,1);
  93.  
  94.   return val;
  95. }
  96.  
  97. int afisareReplicaFiu()
  98. {
  99.   char c='a';
  100.   char*replica=malloc(100);
  101.   int i=0;
  102.   while(1){
  103.     read(fd2,&c,1);
  104.     if(c=='\n'){break;}
  105.     replica[i]=c; i++;
  106.   }
  107.   printf("%s \n", replica);
  108.  
  109.   int val=0;
  110.   if(read(fd2,&c,1)==0)
  111.    {c='2'; val=1;}
  112.   else c='0';
  113.  
  114.   lseek(fd2,-1,SEEK_CUR);
  115.   lseek(flag,0,SEEK_SET);
  116.   write(flag,&c,1);
  117.  
  118.   return val;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement