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
| #include <stdio.h>
#include <stdlib.h>
int main()
{
int tube[2];
int f1;
int f2;
pipe(tube);
if (fork() == 0)
{
fils1(tube);
exit(0);
}
if (fork() == 0)
{
fils2(tube);
exit(0);
}
wait(&f1);
wait(&f2);
printf("père terminé - f1=%d, f2=%d\n", f1, f2);
}
int fils1(int tube[2])
{
FILE *fp;
close(tube[0]);
fp=fopen("/etc/passwd", "r");
printf("Fils1: fp=%p\n", fp);
write(tube[1], &fp, sizeof(FILE*));
}
int fils2(int tube[2])
{
FILE *fp;
char ligne[80];
close(tube[1]);
read(tube[0], &fp, sizeof(FILE*));
printf("Fils2: fp=%p\n", fp);
if (fgets(ligne, 80, fp) == NULL)
perror("fgets");
printf("ligne=%s", ligne);
fclose(fp);
} |
Partager