Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- int main(int argc, char* argv[]){
- int pipe1[2], pipe2[2], sync[2];
- int pid1;
- int pid2;
- if(((pipe(pipe1)) < 0) || ((pipe(pipe2)) <0) || ((pipe(sync)) <0)){
- perror("errore pipe");
- exit(1);
- }
- if((pid1 = fork()) < 0){
- perror("errore fork");
- exit(1);
- }
- if(pid1 == 0){ //primo figlio
- int inputF;
- if((inputF = open(argv[1], O_RDONLY)) < 0){
- perror("errore open input");
- exit(1);
- }
- char buff[2];
- char outBuff1[4];
- close(pipe1[0]);
- close(sync[0]);
- int nread = 0;
- while(read(inputF, buff, 2) == 2){
- if(nread == 4){
- write(sync[1], "S", 1);
- nread = 0;
- }
- else{
- write(sync[1], "N", 1);
- nread += 2;
- }
- write(pipe1[1], buff, 2);
- }
- close(pipe1[1]);
- close(sync[1]);
- close(inputF);
- }
- else{
- if((pid2 = fork()) < 0){
- perror("errore seconda fork");
- exit(1);
- }
- if(pid2 == 0){ //secondo figlio
- close(pipe2[1]);
- char buff_2[4];
- char outBuff[2];
- int outputF;
- if((outputF = open(argv[2], O_RDWR)) < 0){
- perror("errore apertura output");
- exit(1);
- }
- while(read(pipe2[0], buff_2, 4) == 4){
- outBuff[0] = buff_2[0];
- outBuff[1] = buff_2[3];
- write(outputF, outBuff, 2);
- }
- close(pipe2[0]);
- close(outputF);
- }
- else{ //padre
- char buff_padre[4];
- char flag;
- close(pipe1[1]);
- close(pipe2[0]);
- close(sync[1]);
- while(read(sync[0], &flag, 1) == 1){
- if(flag == 'S'){
- if(read(pipe1[0], buff_padre, 4) == 4){
- if(buff_padre[0] != buff_padre[3])
- write(pipe2[1], buff_padre, 4);
- }
- }
- }
- close(pipe1[0]);
- close(pipe2[1]);
- close(sync[0]);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement