Voici qui marche ls|wc|wc
Merci à tous :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| #define _XOPEN_SOURCE 600
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
static pid_t
pipe_fork(void)
{
int pfd[2];
pid_t pid;
if (pipe(pfd) < 0)
return -1;
if ((pid = fork()) < 0)
return -1;
if (pid == 0) {
close(pfd[0]);
dup2(pfd[1], STDOUT_FILENO);
} else {
close(pfd[1]);
dup2(pfd[0], STDIN_FILENO);
}
return pid;
}
int
main(void)
{
pid_t pid;
if ((pid = pipe_fork()) < 0)
return EXIT_FAILURE;
if (pid == 0)
switch (pipe_fork()) {
case -1:
return EXIT_FAILURE;
case 0:
execlp("ls", "ls", (char *)0);
break;
default:
execlp("wc", "wc", (char *)0);
break;
}
else
execlp("wc", "wc", (char *)0);
return EXIT_SUCCESS;
} |