#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int fd[2] = {0};
int ret = pipe(fd);
if (ret < 0)
{
perror("pipe faild");
exit(1);
}
pid_t pid = fork();
if (pid < 0)
{
perror("fork faild");
exit(1);
}
else if (pid == 0)
{
char buf[] = "hello world";
close(fd[0]);
write(fd[1], buf, strlen(buf));
printf("子进程完成写操作\n");
close(fd[1]);
}
else
{
char buf[256] = {0};
close(fd[1]);
read(fd[0], buf, sizeof(buf));
printf("父进程完成读操作\n");
printf("buf = %s\n", buf);
close(fd[0]);
}
return 0;
}
