Linux环境测试管道容量

来源:互联网 发布:乐视视频 mac 编辑:程序博客网 时间:2024/05/22 16:45

每一个进程都有各自独立的地址空间,热和一个进程都无法访问其他进程中的数据在,这样要实现进程间通信就要让两个进程能够看到一块公共的空间,这就是管道的原理。

管道有以下特点:

1:只能在有血缘关系的进程之间进行通信(也就是在父子进程之间通信)。

2:单向通信一个读端,一个写端,如果要双向通信就要建立两个管道。

3:接收数据流,与数据格式无关。

4:管道的生命周期随进程。

5:同步互斥原则。


管道是一种基本的IPC机制,由pipe()函数创建

#include<unistd.h>

pipe(int fileds[2])

fileds[0]是读端

fileds[1]是写段

#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




1 0
原创粉丝点击