#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];
pid_t childpid;
char string[] = "Hello from parent!\n";
char readbuffer[80];
pipe(fd);
childpid = fork();
if (childpid == -1)
{
perror("fork");
exit(1);
}
if (childpid == 0)
{
// Потомок читает
close(fd[1]);
read(fd[0], readbuffer, sizeof(readbuffer));
printf("Child received: %s", readbuffer);
close(fd[0]);
exit(0);
}
else
{
// Родитель пишет
close(fd[0]);
write(fd[1], string, strlen(string));
close(fd[1]);
wait(NULL);
}
return 0;
}