zoukankan      html  css  js  c++  java
  • 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
    ");
            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
    ",strerror(errno));
                break;
            }
            count++;
        }
        printf("count=%d
    ",count);
        return 0;
    }
    

      2.管道的内部组织方式

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

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

  • 相关阅读:
    XMPP核心协议客户端
    平安中国
    读写XML的API们
    IM只是可以用来玩的东西
    再骂自己一句
    淡定
    自己打造SAX和DOM Parser
    Nickel Instant Messeging System
    XMPP RFC阅读笔记(二)
    think in java 笔记
  • 原文地址:https://www.cnblogs.com/guochuanrui/p/5677535.html
Copyright © 2011-2022 走看看