83 lines
2.3 KiB
C
83 lines
2.3 KiB
C
// pipeline.c
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include <sys/wait.h>
|
|
|
|
int main(void) {
|
|
int tube1[2],tube2[2];
|
|
int res1 = pipe(tube1);
|
|
if (res1 == -1){
|
|
perror("Création Pipe 1"); exit(-1);
|
|
}
|
|
int res2 = pipe(tube2);
|
|
if (res2 == -1){
|
|
perror("Création Pipe 1"); exit(-1);
|
|
}
|
|
|
|
// --- CUT ---
|
|
pid_t pid_cut = fork();
|
|
if (pid_cut == -1) { perror("fork cut"); exit(EXIT_FAILURE); }
|
|
if (pid_cut == 0) {
|
|
close(tube1[1]);
|
|
close(tube2[0]);
|
|
if (dup2(tube1[0], STDIN_FILENO) == -1) { perror("dup2 cut in"); _exit(1); }
|
|
if (dup2(tube2[1], STDOUT_FILENO) == -1) { perror("dup2 cut out"); _exit(1); }
|
|
close(tube1[0]);
|
|
close(tube2[1]);
|
|
execlp("cut", "cut", "-d,", "-f3", NULL);
|
|
perror("execlp cut");
|
|
_exit(127);
|
|
}
|
|
|
|
|
|
// SORT
|
|
pid_t pid_sort = fork();
|
|
if (pid_sort == -1) { perror("fork sort"); exit(EXIT_FAILURE); }
|
|
if (pid_sort == 0) {
|
|
close(tube1[0]); close(tube1[1]);
|
|
close(tube2[1]);
|
|
if (dup2(tube2[0], STDIN_FILENO) == -1) { perror("dup2 sort in"); _exit(1); }
|
|
close(tube2[0]);
|
|
|
|
int fd = open("sortie", O_CREAT | O_WRONLY | O_TRUNC, 0644);
|
|
if (fd == -1) { perror("open sortie"); _exit(1); }
|
|
if (dup2(fd, STDOUT_FILENO) == -1) { perror("dup2 sortie"); _exit(1); }
|
|
close(fd);
|
|
|
|
execlp("sort", "sort", NULL);
|
|
perror("execlp sort");
|
|
_exit(127);
|
|
}
|
|
|
|
// --- GREP ---
|
|
pid_t pid_grep = fork();
|
|
if (pid_grep == -1) { perror("fork grep"); exit(EXIT_FAILURE); }
|
|
if (pid_grep == 0) {
|
|
close(tube2[0]); close(tube2[1]);
|
|
close(tube1[0]);
|
|
|
|
int fd = open("server.log", O_RDONLY);
|
|
if (fd == -1) { perror("open server.log"); _exit(1); }
|
|
if (dup2(fd, STDIN_FILENO) == -1) { perror("dup2 grep in"); _exit(1); }
|
|
close(fd);
|
|
|
|
if (dup2(tube1[1], STDOUT_FILENO) == -1) { perror("dup2 grep out"); _exit(1); }
|
|
close(tube1[1]);
|
|
|
|
execlp("grep", "grep", "invalid credentials", NULL);
|
|
perror("execlp grep");
|
|
_exit(127);
|
|
}
|
|
|
|
// Parent
|
|
close(tube1[0]); close(tube1[1]);
|
|
close(tube2[0]); close(tube2[1]);
|
|
|
|
int status;
|
|
waitpid(pid_grep, &status, 0);
|
|
waitpid(pid_cut, &status, 0);
|
|
waitpid(pid_sort, &status, 0);
|
|
return 0;
|
|
} |