#include <iostream>
#include <unistd.h>
int main()
{
pid_t pid;
int pipefds[2];
pipe(pipefds);
pid=fork();
if(0==pid)
{
close(pipefds[1]);
dup2(pipefds[0],0);
char buf[100];
scanf("%s",buf);
printf("buf: %s\n",buf);
}else
{
char bufx[100]="hello";
write(pipefds[1],bufx,sizeof(bufx));
}
return 0;
}
运行结果:
0 - 标准输入文件描述符 <-> 匿名管道的读端pipefds[0]
buf: hello