管道的写阻塞问题!
时间:2010-08-18
来源:互联网
请问对一个管道写时,如果管道缓冲区不够,是不是会阻塞?
如果是的话,那么下面这个程序运行的时候,为什么一开始立马就打印了5000?而不是等读进程读取后再写?
如果不是,那么关于管道写数据时的阻塞问题,正解是什么?
复制代码
如果是的话,那么下面这个程序运行的时候,为什么一开始立马就打印了5000?而不是等读进程读取后再写?
如果不是,那么关于管道写数据时的阻塞问题,正解是什么?
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <unistd.h>
-
- //PIPE_BUF = 4096
- int main()
- {
- int fd[2];
- int ret,request;
- char rbuf[4096];
- char wbuf[4096*2];
- char *msg="hello,pipe!",*msg2="hello,I come here ,too!";
- pid_t pid;
- int stat_val;
-
- if(pipe(fd)<0)
- {
- printf("pipe error\n");
- exit(1);
- }
-
- if((pid=fork())==0)
- {
- //child process write
- close(fd[0]);
- ret=write(fd[1],wbuf,1024);
- printf("I am writer and I write %d Byte data\n",ret);
- ret=write(fd[1],wbuf,5000);
- printf("I am writer and I write %d Byte data\n",ret);
- printf("writer has over\n");
- close(fd[1]);
- exit(0);
- }
- else if(pid>0)
- {
- //parent process read
- close(fd[1]);
- sleep(5);
- while(1)
- {
- sleep(1);
- ret=read(fd[0],rbuf,1000);
- printf("I am reader and the num of reading data is %d\n",ret);
- }
- close(fd[0]);
- exit(0);
- }
- }
作者: edsionte 发布时间: 2010-08-18
QUOTE:
请问对一个管道写时,如果管道缓冲区不够,是不是会阻塞?
如果是的话,那么下面这个程序运行的时候,为什 ...
edsionte 发表于 2010-08-18 23:11
如果是的话,那么下面这个程序运行的时候,为什 ...
edsionte 发表于 2010-08-18 23:11
有问题?
QUOTE:
I am writer and I write 1024 Byte data
I am writer and I write 5000 Byte data
writer has over
I am reader and the num of reading data is 1000
I am reader and the num of reading data is 1000
I am reader and the num of reading data is 1000
I am reader and the num of reading data is 1000
I am reader and the num of reading data is 1000
I am reader and the num of reading data is 1000
I am reader and the num of reading data is 24
I am reader and the num of reading data is 0
I am reader and the num of reading data is 0
I am reader and the num of reading data is 0
I am writer and I write 5000 Byte data
writer has over
I am reader and the num of reading data is 1000
I am reader and the num of reading data is 1000
I am reader and the num of reading data is 1000
I am reader and the num of reading data is 1000
I am reader and the num of reading data is 1000
I am reader and the num of reading data is 1000
I am reader and the num of reading data is 24
I am reader and the num of reading data is 0
I am reader and the num of reading data is 0
I am reader and the num of reading data is 0
作者: 叶叶叶Yeah 发布时间: 2010-08-19
回复 edsionte
是的,上一个帖子我已经说过了。你知道你这里的pipe大小是多少?(大小是 PIPE_SIZE,在内核中定义)man7 pipe好好看看再说
是的,上一个帖子我已经说过了。你知道你这里的pipe大小是多少?(大小是 PIPE_SIZE,在内核中定义)man7 pipe好好看看再说
作者: churchmice 发布时间: 2010-08-19
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28