访问量:AmazingCounters.com

我的github链接:https://github.com/GuoChuanrui

linux管道的容量和内部组织方式

  1.管道容量  count=65536,即64KB

#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;
}

 

  2.管道的内部组织方式

   在 Linux 中,管道的实现并没有使用专门的数据结构,而是借助了文件系统的file结构和VFS的索引节点inode。通过将两个 file 结构指向同一个临时的 VFS 索引节点,而这个 VFS 索引节点又指向一个物理页面而实现的。

  有两个 file 数据结构,但它们定义文件操作例程地址是不同的,其中一个是向管道中写入数据的例程地址,而另一个是从管道中读出数据的例程地址。这样,用户程序的系统调用仍然是通常的文件操作,而内核却利用这种抽象机制实现了管道这一特殊操作。

posted @ 2016-07-16 23:35  郭传瑞  阅读(379)  评论(0编辑  收藏  举报