// pipe.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
int main(void)
{
int fd[2], nbytes;
pid_t childpid;
char string[] = "Hello, world!\n";
char readbuffer[80];
pipe(fd);
if ((childpid = fork()) == -1) {
perror("fork");
exit(1);
}
if (childpid == 0) {
/* Потомок: закрывает вход, пишет в выход */
close(fd[0]);
write(fd[1], string, strlen(string));
close(fd[1]); // сигнал EOF для читающего
exit(0);
} else {
/* Родитель: закрывает выход, читает из входа */
close(fd[1]);
nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
printf("Received string: %s", readbuffer);
close(fd[0]);
wait(NULL); // ждём потомка
}
return 0;
}