54 lines
1.2 KiB
C
54 lines
1.2 KiB
C
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
int main(void)
|
|
{
|
|
int tube[2];
|
|
int tube2[2];
|
|
int res = pipe(tube);
|
|
if (res == -1) {
|
|
perror("Création Pipe"); exit(-1);
|
|
}
|
|
int res2 = pipe(tube2);
|
|
if (res2 == -1) {
|
|
perror("Création Pipe"); exit(-1);
|
|
}
|
|
|
|
pid_t pid_wc = fork();
|
|
if (pid_wc == 0) {
|
|
// lit depuis tube -> stdin
|
|
close(tube[1]);
|
|
dup2(tube[0], 0);
|
|
close(tube[0]);
|
|
|
|
// écrit vers tube2 -> stdout
|
|
close(tube2[0]); // on ne lit pas tube2 ici
|
|
dup2(tube2[1], 1);
|
|
close(tube2[1]);
|
|
|
|
execlp("cat", "cat",NULL); // ou "wc", "-c", NULL pour compter octets, etc.
|
|
perror("cat wc");
|
|
_exit(1);
|
|
}
|
|
pid_t pid_data = fork();
|
|
if (pid_data == 0) {
|
|
close(tube[0]);
|
|
write(tube[1],"Bonjour tous le monde\nJe m'appelle Ronan\n",strlen("Bonjour tous le monde\nJe m'appelle Ronan\n"));
|
|
close(tube[1]);
|
|
}
|
|
pid_t pid_read = fork();
|
|
if (pid_read == 0) {
|
|
char temp[500];
|
|
close(tube2[1]);
|
|
read(tube2[0],&temp,sizeof(temp));
|
|
printf("%s",temp);
|
|
close(tube2[0]);
|
|
}
|
|
|
|
return 0;
|
|
} |