PIPE
来源:互联网 发布:转行程序员 编辑:程序博客网 时间:2024/06/06 09:19
必须在 fork中调用pipe(),否则子进程不会继承文件描述符。
两个进程不共享祖先进程,就不能使用pipe。但是可以使用命名管道
int fd[2],nbytes;
pid_t pid
int *write_fd = &fd[1];
int *read_fd=&fd[0];
result=pipe(fd);
if(-1==result)
{
printf("fail to create pipe\n");
return -1;
}
pid=fork();
if(-1==pid)
{
printf("fail to fork\n");
return -1;
}
if(0==pid)
{
close(*read_fd);
result=write(*write_fd,string,strlen(string));
return 0;
}
else
{
close(*write_fd);
result=read(*read_fd,readbuffer,sizeof(readbuffer));
printf("the parent receive %d bytes data %s\n",nbytes,readbuffer);
}
对管道进行写入操作的时候,如果写入的数据小于128k则是非原子的
如果大于128k字节,缓冲区的数据将被连续的写入
管道,直到全部数据写完为止,如果没有进程读写数据,则将一直阻塞
int fd[2],nbytes;
pid_t pid;
int *read_fd=&fd[0];
int *write_fd=&fd[1];
result=pipe(fd);
if(-1 == result)
{
}
两个进程不共享祖先进程,就不能使用pipe。但是可以使用命名管道
int fd[2],nbytes;
pid_t pid
int *write_fd = &fd[1];
int *read_fd=&fd[0];
result=pipe(fd);
if(-1==result)
{
printf("fail to create pipe\n");
return -1;
}
pid=fork();
if(-1==pid)
{
printf("fail to fork\n");
return -1;
}
if(0==pid)
{
close(*read_fd);
result=write(*write_fd,string,strlen(string));
return 0;
}
else
{
close(*write_fd);
result=read(*read_fd,readbuffer,sizeof(readbuffer));
printf("the parent receive %d bytes data %s\n",nbytes,readbuffer);
}
对管道进行写入操作的时候,如果写入的数据小于128k则是非原子的
如果大于128k字节,缓冲区的数据将被连续的写入
管道,直到全部数据写完为止,如果没有进程读写数据,则将一直阻塞
int fd[2],nbytes;
pid_t pid;
int *read_fd=&fd[0];
int *write_fd=&fd[1];
result=pipe(fd);
if(-1 == result)
{
}
阅读全文
0 0
- pipe()
- pipe
- pipe
- pipe
- pipe
- PIPE
- pipe
- pipe
- pipe
- pipe
- pipe
- pipe
- pipe
- pipe()
- Pipe
- pipe
- pipe
- PIPE
- 关系数据库规范化理论---范式
- 两数和-输入已排序的数组-LintCode
- vue2.0 父子传值,子父传值
- 软件设计师考试
- 顺序表作业
- PIPE
- Leetcode206
- Unable to inject views for 。。。。
- 三级菜单
- spring data jpa使用错误记录
- ubuntu 防火墙操作
- 解决Caffe训练过程中loss不变问题
- Python Windows pyhs2下模块安装
- 朴素算法