Vous êtes sur la page 1sur 2

Corrig TD-TP Les tuyaux

Exercice 1 :
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>

#define LECTURE 0
#define ECRITURE 1
int main(int argc, char *argv[]) {
int x, tuyau1[2], tuyau2[2];

pipe(tuyau1); (0.5)
pipe(tuyau2):
switch (fork()) { (0.5)
case -1 : /* erreur */
perror("Erreur dans fork()");
exit(EXIT_FAILURE);
case 0 : /* processus fils */
close(tuyau2[LECTURE]); (0.5)
close(tuyau1[ECRITURE]);
fp_lect1=fdopen(tuyau2[LECTURE]); (0.5)
fp_ecrit1= fdopen(tuyau1[ECRITURE]);
for(i=0 ;i<10 ;i++) (0.5)
{
fscanf(fp_lect1, %d ,&x) ; (0.5)
printf( %d ,x)
x++ ; (0.5)
fprintf(fp-ecrit1, , %d ,x) ;
fflush() ;
}

default : /* processus pere */


close(tuyau1[LECTURE]); (0.5)
close(tuyau2[ECRITURE]);
fp_lect2=fdopen(tuyau1[LECTURE]); (0.5)
fp_ecrit2= fdopen(tuyau2[ECRITURE]);
x=0 ;
for(i=0 ;i<10 ;i++) (0.5)
{
fprintf(fp-ecrit2, %d ,x) ; (0.5)
fflush() ;
x++ ; (0.5)
fscanf(fp_lect2, %d ,&x) ;
printf( %d ,x) ;
}
}