```cpp
#include "unistd.h"
#include "stdio.h"
#include "stdlib.h"
int main()
{
int ret;
int fd[2];
char buf[]="hello linux\n";
char readbuf[128]={0};
ret=pipe(fd);
if(ret<0)
{
printf("create pipe failure.");
return -1;
}else {
printf("create pipe success.\n");
printf("writefd:%d,readfd:%d",fd[1],fd[0]);
}
write(fd[1],buf,sizeof(buf));
read(fd[0],readbuf,128);
printf("%s\n",readbuf);
close (fd[0]);
close (fd[1]);
return 0;
}
管道中的读完就删除
无可读的东西就阻塞 sleep 状态 无名管道不能实现非父子进程之间的通信
写阻塞 写内容超过 缓存大小时就会阻塞。
```cpp
int fd[2];
int ret;
ret=pipe(fd);
if(ret <0)
{
printf("create pipe failure\n");
return -1;
}
printf("create pipe success fd[0]=%d,fd[1]=%d\n",fd[0],fd[1]);
pid_t pid;
pid=fork();
if(pid==0)
{
sleep(2);
char writebuf[]="hello linux\n";
write(fd[1],writebuf,sizeof(writebuf));
}
char readbuf[1024]={0};
read(fd[0],readbuf,1024);
printf("%s\n",readbuf);