BUT2/SCR/SCR2/TP05/pipe-ex.c
2023-10-23 13:23:36 +02:00

36 lines
709 B
C

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
int ecrit;
int lu;
int p[2];
pipe(p);
pid_t pf = fork();
if (pf == 0) {
pid_t pf1 = fork();
if (pf1 == 0) {
while (read(p[0],&lu,sizeof(int)) > 0){
printf("%d received %d\n", getpid(), lu);
}
}
}
else{
pid_t pf2 = fork();
if (pf2 == 0) {
}
else{
while(1){
sleep(3);
ecrit = getpid();
printf("%d sent %d\n", ecrit, ecrit);
write(p[1],&ecrit,sizeof(int));
}
}
}
}