您的位置:首页 > 运维架构 > Linux

探索linux管道的容量

2016-07-02 14:35 579 查看
管道特点:

(1)、单向通信。数据只能由一个进程流向另一个进程(其中一个读管道,一个写管道);如果要进行双工通信,需要建 立两个管道。

(2)、管道只能用于有血缘关系的进程间通信。

(3)、流式服务。发送和接收大小不受特定格式的限制。

(4)、管道的生命周期和进程有关。

(5)、同步与互斥原则。

fcntl()可以改变已打开的文件性质

F_GETFL 取得文件描述符状态旗标,此旗标为open()的参数flags。

F_SETFL 设置文件描述符状态旗标,参数arg为新旗标,但只允许O_APPEND、O_NONBLOCK和O_ASYNC位的改变,其他位的改变将不受影响。

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int _pipe[2];
if(pipe(_pipe)==-1)
{
printf("pipe error\n");
return 1;
}
int ret;
int count=0;
int flag=fcntl(_pipe[1],F_GETFL);
fcntl(_pipe[1],F_SETFL,flag|O_NONBLOCK);
while(1)
{
ret=write(_pipe[1],"A",1);
if(ret==-1)
{
printf("error %s\n",strerror(errno));
break;
}
count++;
}
printf("count=%d\n",count);
return 0;
}




所以管道的容量是64kb。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息