代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
//创建两个无名管道
//父进程发送,子进程接收
int pfd1[2] = {0};
int ret = pipe(pfd1);
if(ret < 0)
{
perror("pipe");
return -1;
}
//子进程发送,父进程接收
int pfd2[2] = {0};
ret = pipe(pfd2);
if(ret < 0)
{
perror("pipe");
exit(1);
}
//创建进程
char buf[50];
pid_t pid = fork();
if(pid > 0)
{
//父进程
close(pfd1[0]); //父进程 关闭1的读端
close(pfd2[1]); //父进程 关闭2的写端
while(1)
{
bzero(buf, sizeof(buf));
//父进程写
printf("父进程说:");
scanf("%s",buf);
ret = write(pfd1[1], buf, sizeof(buf));
if(ret < 0)