Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <string.h> //nu era nevoie de toate astea
- int afisareReplicaTata();
- int afisareReplicaFiu();
- int fd1,fd2,flag;
- int main(int argc,char*argv[])
- {
- if(argc<4){perror("da 3 argumente: tata fiu si flag"); exit(0);}
- if(-1==(fd1=open(argv[1],O_RDONLY))){perror("nu pot citi tata"); exit(0);}
- if(-1==(fd2=open(argv[2],O_RDONLY))){perror("nu pot citi fiu"); exit(0);}
- if(-1==(flag=open(argv[3],O_RDWR | O_TRUNC))){perror("nu pot citi flag"); exit(0);}
- pid_t pid_fiu;
- if((pid_fiu=fork())==-1){perror("eroare la fork"); exit(1);}
- else if(pid_fiu==0) //fiu
- {
- char c='a';
- while(1) //cat timp nu e eof
- {
- lseek(flag,0,SEEK_SET);
- read(flag,&c,1);
- if(c=='2')
- {
- while(afisareReplicaFiu()!=1);
- break;
- }
- if(c=='1')
- {
- if(afisareReplicaFiu()==1)
- break;
- }
- sleep(1);
- }
- }
- else //tata
- {
- char c='a';
- afisareReplicaTata();
- while(1) //cat timp nu e eof
- {
- lseek(flag,0,SEEK_SET);
- read(flag,&c,1);
- if(c=='2')
- {
- while(afisareReplicaTata()!=1);
- break;
- }
- if(c=='0')
- {
- if(afisareReplicaTata()==1)
- break;
- }
- else if(c=='2') break;
- sleep(1);
- }
- }
- close(fd1);
- close(fd2);
- return 0;
- }
- int afisareReplicaTata()
- {
- char c='a';
- char*replica=malloc(100);
- int i=0;
- while(1){
- read(fd1,&c,1);
- if(c=='\n'){break;}
- replica[i]=c; i++;
- }
- printf("%s \n", replica);
- int val=0;
- if(read(fd1,&c,1)==0)
- {c='2'; val=1;}
- else c='1';
- lseek(fd1,-1,SEEK_CUR);
- lseek(flag,0,SEEK_SET);
- write(flag,&c,1);
- return val;
- }
- int afisareReplicaFiu()
- {
- char c='a';
- char*replica=malloc(100);
- int i=0;
- while(1){
- read(fd2,&c,1);
- if(c=='\n'){break;}
- replica[i]=c; i++;
- }
- printf("%s \n", replica);
- int val=0;
- if(read(fd2,&c,1)==0)
- {c='2'; val=1;}
- else c='0';
- lseek(fd2,-1,SEEK_CUR);
- lseek(flag,0,SEEK_SET);
- write(flag,&c,1);
- return val;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement