fork()&pipe()多线程###
//fork()建立子进程经典code
https://img2018.cnblogs.com/blog/1027722/201810/1027722-20181011201518927-1431303100.jpg
结果:
https://img2018.cnblogs.com/blog/1027722/201810/1027722-20181011201816616-1985312695.jpg(复制链接到搜索框即可查看)
//fork() 新建子进程, 父子共用pipe
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
int x, fd[2];
char buf[50], s[50];//buf缓冲区, s输出区
pipe(fd);
if(x==0)//子进程
{
sprintf(buf, "This is an example of pipe
");
write(fd[1], buf, 50);
exit(0);
}
else
{
wait(0);
read(fd[0], s, 50);
printf(" %s", s);
}
}
结果:
![](https://img2018.cnblogs.com/blog/1027722/201810/1027722-20181011202445001-1532793219.jpg)
Ubuntu 运行通过!