#include <unistd.h>
#include <stdio.h>
#include <errno.h>
int main()
{
int fd[2];
char buf[4097];
int n;
pipe(fd);
if (!fork()) {
close(fd[1]);
printf(" Lecture dans un tuyau vide\n");
n = read(fd[0], buf, 1);
printf(" J'ai lu: %d\n", n);
_exit(0);
}
close(fd[0]);
sleep(1);
printf("Ecriture\n");
write(fd[1], buf, 1);
sleep(1);
printf("OK.\n\n");
pipe(fd);
if (!fork()) {
close(fd[1]);
sleep(1);
printf(" Lecture dans un tuyau vide, pas de redacteur (1)\n");
n = read(fd[0], buf, 1);
printf(" J'ai lu: %d\n", n);
_exit(0);
}
close(fd[0]);
close(fd[1]);
sleep(2);
printf("OK.\n\n");
pipe(fd);
if (!fork()) {
close(fd[1]);
printf(" Lecture dans un tuyau vide, pas de redacteur (2)\n");
n = read(fd[0], buf, 1);
printf(" J'ai lu: %d\n", n);
_exit(0);
}
close(fd[0]);
sleep(1);
close(fd[1]);
sleep(1);
printf("OK.\n\n");
pipe(fd);
if (!fork()) {
close(fd[0]);
printf(" Ecriture dans un tuyau plein\n");
n = write(fd[1], buf, 4097);
printf(" J'ai ecrit: %d\n", n);
_exit(0);
}
close(fd[1]);
sleep(1);
printf("Lecture\n");
read(fd[0], buf, 1);
sleep(1);
printf("OK.\n\n");
pipe(fd);
if (!fork()) {
close(fd[0]);
sleep(1);
printf(" Ecriture dans un tuyau plein, pas de lecteur (1)\n");
n = write(fd[1], buf, 4097);
printf(" J'ai ecrit: %d\n", n);
_exit(0);
}
close(fd[1]);
close(fd[0]);
sleep(2);
printf("OK.\n\n");
pipe(fd);
if (!fork()) {
close(fd[0]);
printf(" Ecriture dans un tuyau plein, pas de lecteur (2)\n");
n = write(fd[1], buf, 4097);
printf(" J'ai ecrit: %d\n", n);
_exit(0);
}
close(fd[1]);
sleep(1);
close(fd[0]);
sleep(1);
printf("OK.\n\n");
return 0;
}