#include #include #include #include int pipe1[2]; int pipe2[2]; int pipe3[2]; void closeAll() { close(pipe1[0]); close(pipe1[1]); close(pipe2[0]); close(pipe2[1]); close(pipe3[0]); close(pipe3[1]); } int launch(int in, int out, int synch /* alínea b)*/, char* prog) { dup2(out, 1); dup2(in, 0); closeAll(pipe1, pipe2, pipe3); read(synch, NULL, 1); // alínea b) execlp(prog, prog, NULL); return -1; } int main() { if (pipe(pipe1) == -1) { perror("pipe 1"); return errno; } if (pipe(pipe2) == -1) { perror("pipe 2"); return errno; } if (pipe(pipe3) == -1) { perror("pipe 3"); return errno; } // alínea b) int pipeSynch[2]; if (pipe(pipeSynch) == -1) { perror("pipe synch"); return errno; } // P1 if (fork() == 0) if (launch(pipe3[0], pipe1[1], pipeSynch[0], "P1.exe") == -1) { perror("P1"); return errno; } // P2 if (fork() == 0) if (launch(pipe1[0], pipe2[1], pipeSynch[0], "P2.exe") == -1) { perror("P2"); return errno; } // P3 if (fork() == 0) if (launch(pipe2[0], pipe3[1], pipeSynch[0], "P3.exe") == -1) { perror("P3"); return errno; } // alínea b) write(pipeSynch[1], "123", 3); close(pipeSynch[1]); // alínea c) int i = 0; for (; i< 3; i++) wait(NULL); return 0; }