// pipe2.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.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[1]);
nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
printf("Received string: %s", readbuffer);
close(fd[0]);
exit(0);
} else {
/* Родитель: теперь пишет -> закрывает вход */
close(fd[0]);
write(fd[1], string, strlen(string));
close(fd[1]);
wait(NULL);
}
return 0;
}