歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 探索linux管道的容量

探索linux管道的容量

日期:2017/3/3 11:37:23   编辑:Linux技術

管道特點:

(1)、單向通信。數據只能由一個進程流向另一個進程(其中一個讀管道,一個寫管道);如果要進行雙工通信,需要建 立兩個管道。

(2)、管道只能用於有血緣關系的進程間通信。

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

(4)、管道的生命周期和進程有關。

(5)、同步與互斥原則。

fcntl()可以改變已打開的文件性質

F_GETFL 取得文件描述符狀態旗標,此旗標為open()的參數flags。

F_SETFL 設置文件描述符狀態旗標,參數arg為新旗標,但只允許O_APPEND、O_NONBLOCK和O_ASYNC位的改變,其他位的改變將不受影響。

[code]#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。

Copyright © Linux教程網 All Rights Reserved