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 58 59 60 61 62 63 64
|
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
{ int p[2];
pid_t pid;
/*le premier tube*/
if (pipe(p) == -1)
{ perror("pipe"); exit(1);
}
if ((pid = fork()) == -1)
{ perror("fork");
exit(1);
}
/*le pere*/
if (pid)
{ close(p[0]);
dup2(p[1],1);
execlp("ps", "ps", "-l", NULL);
perror("execlp"); exit(1);
}
close(p[1]);
dup2(p[0], 0);
/*le deuxieme tube*/
if (pipe(p) == -1)
{
perror("pipe");
exit(1);
}
if ((pid = fork()) == -1)
{
perror("fork");
exit(1);
}
/*le père*/
if (pid)
{ close(p[0]);
dup2(p[1],1);
execlp("grep", "grep", "wait", NULL);
perror("execlp"); exit(1);
}
/*le fils*/
close(p[1]);
dup2(p[0], 0);
execlp("wc", "wc", "-l", NULL);
perror("execlp");
exit(1);
} |