一、 当没有数据可读时
O_NONBLOCK disable:read调用阻塞,即进程暂停执行,一直等到有数据来到为止。

O_NONBLOCK enable:read调用返回-1,errno值为EAGAIN。

示例程序如下:

C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*************************************************************************
    > File Name: process_.c
    > Author: Simba
    > Mail: dameng34@163.com
    > Created Time: Sat 23 Feb 2013 02:34:02 PM CST
 ************************************************************************/
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<signal.h>
#define ERR_EXIT(m) \
    do { \
        perror(m); \
        exit(EXIT_FAILURE); \
    } while(0)

int main(int argc, char *argv[])
{
    int pipefd[2];
    if (pipe(pipefd) == -1)
        ERR_EXIT("pipe error");

pid_t pid;
    pid = fork();
    if (pid == -1)
        ERR_EXIT("fork error");

if (pid == 0)
    {
        sleep(3);
        close(pipefd[0]);
        write(pipefd[1], "hello", 5);
        close(pipefd[1]);
        exit(EXIT_SUCCESS);
    }

close(pipefd[1]);
    char buf[10] = {0};
    int flags = fcntl(pipefd[0], F_GETFL);
    fcntl(pipefd[0], F_SETFL, flags | O_NONBLOCK); //enable fd的O_NONBLOCK
    int ret = read(pipefd[0], buf, 10); //默认是disable fd的O_NONBLOCK
    if (ret == -1) // 父进程不会阻塞,出错返回
        ERR_EXIT("read error");
    printf("buf=%s\n", buf);

return 0;
}

特意在子进程中sleep了3s,让父进程先被调度运行,而且读端文件状态标志设置为非阻塞,即立刻出错返回,如下。

simba@ubuntu:~/Documents/code/linux_programming/APUE/pipe$ ./pipe_block 
read error: Resource temporarily unavailable

二、当管道满的时候
O_NONBLOCK disable: write调用阻塞,直到有进程读走数据
O_NONBLOCK enable:调用返回-1,errno值为EAGAIN

管道是一块内存缓冲区,可以写个小程序测试一下管道的容量Pipe Capacity:

C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*************************************************************************
    > File Name: process_.c
    > Author: Simba
    > Mail: dameng34@163.com
    > Created Time: Sat 23 Feb 2013 02:34:02 PM CST
 ************************************************************************/
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<signal.h>
#define ERR_EXIT(m) \
    do { \
        perror(m); \
        exit(EXIT_FAILURE); \
    } while(0)

int main(int argc, char *argv[])
{
    int pipefd[2];
    if (pipe(pipefd) == -1)
        ERR_EXIT("pipe error");

int ret;
    int count = 0;
    int flags = fcntl(pipefd[1], F_GETFL);
    fcntl(pipefd[1], F_SETFL, flags | O_NONBLOCK); // 设置为非阻塞
    while (1)
    {
        ret = write(pipefd[1], "A", 1);
        if (ret == -1)
        {
            printf("err=%s\n", strerror(errno));
            break;
        }

count++;
    }
    printf("count=%d\n", count); //管道容量

return 0;
}

程序中将写端文件状态标志设置为非阻塞,当管道被写满时不会等待其他进程读取数据,而是直接返回-1并置errno,输出如下:
simba@ubuntu:~/Documents/code/linux_programming/APUE/pipe$ ./pipe_capacity 
err=Resource temporarily unavailable
count=65536

打印了错误码,可以看到管道的容量是64kB,man 7 pipe中也有提到在2.6.11内核以前是4096,现在是65536。

三、如果所有管道读端对应的文件描述符被关闭(管道读端的引用计数等于0),则write操作会产生SIGPIPE信号,默认终止当前进程

示例代码如下:

C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*************************************************************************
    > File Name: process_.c
    > Author: Simba
    > Mail: dameng34@163.com
    > Created Time: Sat 23 Feb 2013 02:34:02 PM CST
 ************************************************************************/
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<signal.h>
#define ERR_EXIT(m) \
    do { \
        perror(m); \
        exit(EXIT_FAILURE); \
    } while(0)

void handler(int sig)
{
    printf("recv sig=%d\n", sig);
}

int main(int argc, char *argv[])
{
    signal(SIGPIPE, handler);

int pipefd[2];
    if (pipe(pipefd) == -1)
        ERR_EXIT("pipe error");

pid_t pid;
    pid = fork();
    if (pid == -1)
        ERR_EXIT("fork error");

if (pid == 0)
    {
        close(pipefd[0]);
        exit(EXIT_SUCCESS);
    }
    close(pipefd[0]);
    sleep(1);
    int ret = write(pipefd[1], "hello", 5);
    if (ret == -1)
    {
        printf("err=%s\n", strerror(errno));
    }

return 0;
}

输出测试:

simba@ubuntu:~/Documents/code/linux_programming/APUE/pipe$ ./close_fd_read 
recv sig=13
err=Broken pipe

父进程睡眠1s确保所有读端文件描述符都已经关闭,如果没有安装SIGPIPE信号的处理函数,则默认终止当前进程,即write函数不会返回,现在write错误返回-1,并置errno=EPIPE,对应的出错信息是Broken pipe。

四、如果所有管道写端对应的文件描述符被关闭(管道写端的引用计数等于0),那么管道中剩余的数据都被读取后,再次read会返回0

示例程序如下:

C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*************************************************************************
    > File Name: process_.c
    > Author: Simba
    > Mail: dameng34@163.com
    > Created Time: Sat 23 Feb 2013 02:34:02 PM CST
 ************************************************************************/
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<signal.h>
#define ERR_EXIT(m) \
    do { \
        perror(m); \
        exit(EXIT_FAILURE); \
    } while(0)

void handler(int sig)
{
    printf("recv sig=%d\n", sig);
}

int main(int argc, char *argv[])
{
    signal(SIGPIPE, handler);

int pipefd[2];
    if (pipe(pipefd) == -1)
        ERR_EXIT("pipe error");

pid_t pid;
    pid = fork();
    if (pid == -1)
        ERR_EXIT("fork error");

if (pid == 0)
    {
        close(pipefd[1]);
        exit(EXIT_SUCCESS);
    }

close(pipefd[1]);
    sleep(1);
    char buf[10] = {0};
    int ret = read(pipefd[0], buf, 10);
    printf("ret = %d\n", ret);

return 0;
}

输出测试如下:

simba@ubuntu:~/Documents/code/linux_programming/APUE/pipe$ ./close_fd_write 
ret = 0

同样地父进程睡眠1s确保所有的写端文件描述符都已经关闭,read返回0。

五、当要写入的数据量不大于PIPE_BUF时,linux将保证写入的原子性;当要写入的数据量大于PIPE_BUF时,linux将不再保证写入的原子性。

On  Linux, PIPE_BUF is 4096 bytes。

The precise semantics depend on whether the file descriptor is nonblocking (O_NONBLOCK), whether there are multiple writers to the pipe, and on n, the number of bytes to be written。即由文件描述符是否是非阻塞的,是否有多个进程向管道写入以及写入的字节数所决定准确的语义,总共分4种情况,具体可man一下。

下面的程序演示 O_NONBLOCK disabled ,size > PIPE_BUF(4K)的情况 :

C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*************************************************************************
    > File Name: process_.c
    > Author: Simba
    > Mail: dameng34@163.com
    > Created Time: Sat 23 Feb 2013 02:34:02 PM CST
 ************************************************************************/
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<signal.h>
#define ERR_EXIT(m) \
    do { \
        perror(m); \
        exit(EXIT_FAILURE); \
    } while(0)

#define TEST_SIZE 68*1024 // 68KB
/* 默认O_NONBLOCK disabled ,这里验证 size > PIPE_BUF(4K)的情况 */
int main(int argc, char *argv[])
{
    char a[TEST_SIZE];
    char b[TEST_SIZE];

memset(a, 'A', sizeof(a));
    memset(b, 'B', sizeof(b));

int pipefd[2];
    int ret = pipe(pipefd);
    if (ret == -1)
        ERR_EXIT("pipe error");

int pid = fork();
    if (pid == 0)
    {

close(pipefd[0]);
        ret = write(pipefd[1], a, sizeof(a)); // 全部写完才返回
        printf("apid=%d write %d bytes to pipe\n", getpid(), ret);
        exit(0);
    }

pid = fork();

if (pid == 0)
    {

close(pipefd[0]);
        ret = write(pipefd[1], b, sizeof(b));
        printf("bpid=%d write %d bytes to pipe\n", getpid(), ret);
        exit(0);
    }

close(pipefd[1]);

sleep(1);

int fd = open("test.txt", O_WRONLY | O_CREAT | O_TRUNC, 0664);
    char buf[1024 * 4] = {0};
    int n = 1;
    while (1)
    {
        ret = read(pipefd[0], buf, sizeof(buf)); //当管道被写入数据,就已经可以开始读了,每次读取4k
        if (ret == 0) // 管道写端全部关闭,即读到了结尾
            break;
        printf("n=%02d pid=%d read %d bytes from pipe buf[4095]=%c\n",
               n++, getpid(), ret, buf[4095]);
        write(fd, buf, ret);
    }

return 0;
}

输出测试如下:

simba@ubuntu:~/Documents/code/linux_programming/APUE/pipe$ ./pipe_buf 
n=01 pid=7137 read 4096 bytes from pipe buf[4095]=B
n=02 pid=7137 read 4096 bytes from pipe buf[4095]=B
n=03 pid=7137 read 4096 bytes from pipe buf[4095]=B
n=04 pid=7137 read 4096 bytes from pipe buf[4095]=B
n=05 pid=7137 read 4096 bytes from pipe buf[4095]=B
n=06 pid=7137 read 4096 bytes from pipe buf[4095]=B
n=07 pid=7137 read 4096 bytes from pipe buf[4095]=B
n=08 pid=7137 read 4096 bytes from pipe buf[4095]=B
n=09 pid=7137 read 4096 bytes from pipe buf[4095]=B
n=10 pid=7137 read 4096 bytes from pipe buf[4095]=B
n=11 pid=7137 read 4096 bytes from pipe buf[4095]=B
n=12 pid=7137 read 4096 bytes from pipe buf[4095]=B
n=13 pid=7137 read 4096 bytes from pipe buf[4095]=B
n=14 pid=7137 read 4096 bytes from pipe buf[4095]=B
n=15 pid=7137 read 4096 bytes from pipe buf[4095]=B
n=16 pid=7137 read 4096 bytes from pipe buf[4095]=B
n=17 pid=7137 read 4096 bytes from pipe buf[4095]=A
n=18 pid=7137 read 4096 bytes from pipe buf[4095]=A
n=19 pid=7137 read 4096 bytes from pipe buf[4095]=A
n=20 pid=7137 read 4096 bytes from pipe buf[4095]=A
n=21 pid=7137 read 4096 bytes from pipe buf[4095]=A
n=22 pid=7137 read 4096 bytes from pipe buf[4095]=A
n=23 pid=7137 read 4096 bytes from pipe buf[4095]=A
n=24 pid=7137 read 4096 bytes from pipe buf[4095]=A
n=25 pid=7137 read 4096 bytes from pipe buf[4095]=A
n=26 pid=7137 read 4096 bytes from pipe buf[4095]=A
apid=7138 write 69632 bytes to pipe
n=27 pid=7137 read 4096 bytes from pipe buf[4095]=A
n=28 pid=7137 read 4096 bytes from pipe buf[4095]=A
n=29 pid=7137 read 4096 bytes from pipe buf[4095]=A
n=30 pid=7137 read 4096 bytes from pipe buf[4095]=A
n=31 pid=7137 read 4096 bytes from pipe buf[4095]=A
n=32 pid=7137 read 4096 bytes from pipe buf[4095]=A
n=33 pid=7137 read 4096 bytes from pipe buf[4095]=A
n=34 pid=7137 read 4096 bytes from pipe buf[4095]=B
bpid=7139 write 69632 bytes to pipe

分析一下:现在的情况是有两个子进程在对管道进行阻塞写入各68k,即每个子进程完全写入68k才返回,而父进程对管道进行阻塞读取,每次读取4k,打印每4k中的最后一个字符,如果没有数据到达就阻塞等待,如果管道剩余数据不足4k,read 很可能返回 < 4k,但因为我们写入68k是4k整数倍,故不存在这种情况。需要注意的是是边写边读,因为前面说过管道的容量只有64k,当管道被写满时子进程就阻塞等待父进程读取后再写入。由上面输出可以看出B进程先写入64k的B,然后A进程写入68k的A之后B进程接着写完最后4K的B,然后write返回。由A进程write完毕输出的提示可知此时A进程已经写完成了,但父进程还没读取A完毕,当两个子进程全部写完退出时关闭写端文件描述符,则父进程read就会返回0,退出while循环。可以得出结论:当多个进程对管道进行写入,且一次性写入数据量大于PIPE_BUF时,则不能保证写入的原子性,即可能数据是穿插着的。man 手册的解释如下:

O_NONBLOCK disabled, n > PIPE_BUF
 The write is nonatomic: the data given to write(2) may be interleaved with write(2)s by other process;  the write(2) blocks until n bytes have been written.

注意我们这里设定了size=68k,则写端不能设置成非阻塞,因为Pipe Capacity 只有64k,不能一次性写入68k,如果此时管道是满的(64k),则只能返回-1并置错误码为EAGAIN,且一个字符也不写入,若不是满的,则写入的字节数是不确定的,需要检查write的返回值,而且这些字节很可能也与其他进程写入的数据穿插着。读端也不能设置为非阻塞,如果此时尚未有数据写入(管道为空)则返回-1并置错误码为EAGAIN,如果有部分数据已经写入,则读取的数据字节数也是不确定的,需要检查read的返回值。总之测试4种不同情形下的情况也应设置不同的条件。

O_NONBLOCK disabled, n <= PIPE_BUF
              All n bytes are written atomically; write(2) may block if there is not room for n bytes to be written imme‐
              diately

O_NONBLOCK enabled, n <= PIPE_BUF
              If  there  is  room  to write n bytes to the pipe, then write(2) succeeds immediately, writing all n bytes;
              otherwise write(2) fails, with errno set to EAGAIN.

O_NONBLOCK disabled, n > PIPE_BUF
              The write is nonatomic: the data given to write(2) may be interleaved with write(2)s by other process;  the
              write(2) blocks until n bytes have been written.

O_NONBLOCK enabled, n > PIPE_BUF
              If  the  pipe  is full, then write(2) fails, with errno set to EAGAIN.  Otherwise, from 1 to n bytes may be
              written (i.e., a "partial write" may occur; the caller should check the return value from write(2)  to  see
              how many bytes were actually written), and these bytes may be interleaved with writes by other processes.

管道的前4种读写规则具有普遍意义,Tcp socket 也具有管道的这些特性。

参考:《APUE》

linux系统编程之管道(二):管道读写规则和Pipe Capacity、PIPE_BUF相关推荐

  1. 嵌入式Linux系统编程学习之二常用命令

    嵌入式Linux系统编程学习之二常用命令 文章目录 嵌入式Linux系统编程学习之二常用命令 前言 一.常用命令 1.su(用户切换) 2.useradd(添加用户) 3.passwd(修改密码) 4 ...

  2. 【Linux系统编程学习】匿名管道pipe与有名管道fifo

    此为牛客Linux C++和黑马Linux系统编程课程笔记. 0. 关于进程通信 Linux环境下,进程地址空间相互独立,每个进程各自有不同的用户地址空间.任何一个进程的全局变量在另一个进程中都看不到 ...

  3. 进程返回linux系统编程之管道(二):管道读写规则和Pipe Capacity、PIPE_BUF

    题记:写这篇博客要主是加深自己对进程返回的认识和总结实现算法时的一些验经和训教,如果有错误请指出,万分感谢. 一. 当没有数据可读时 O_NONBLOCK disable:read调用阻塞,即进程暂停 ...

  4. 嵌入式Linux系统编程学习之二十一命名管道(FIFO)

    文章目录 前言 一.创建.删除FIFO文件 1. 用函数创建和删除 FIFO 文件 2. 用命令创建和删除 FIFO 文件 二.打开.关闭FIFO文件 三.读写FIFO 前言   无名管道只能在有亲缘 ...

  5. 嵌入式Linux系统编程学习之二十无名管道(PIPE)

      管道是 Linux 进程间通信的一种方式,如命令 ps -ef | grep ntp .   无名管道的特点包括: 只能在亲缘关系进程间通信(父子或兄弟): 半双工(固定的读端和固定的写端): 它 ...

  6. 【Linux系统编程】进程间通信--无名管道(pipe)

    管道的概述 管道也叫无名管道,它是是 UNIX 系统 IPC(进程间通信) 的最古老形式,所有的 UNIX 系统都支持这种通信机制. 无名管道有如下特点: 1.半双工,数据在同一时刻只能在一个方向上流 ...

  7. 【Linux系统编程】进程间通信--有名管道

    命名管道的概述 无名管道,由于没有名字,只能用于亲缘关系的进程间通信(更多详情,请看<无名管道>).为了克服这个缺点,提出了命名管道(FIFO),也叫有名管道.FIFO 文件. 命名管道( ...

  8. Linux系统编程——进程间通信:命名管道(FIFO)

    命名管道的概述 无名管道,由于没有名字,只能用于亲缘关系的进程间通信(更多详情,请看<无名管道>).为了克服这个缺点,提出了命名管道(FIFO),也叫有名管道.FIFO 文件. 命名管道( ...

  9. Linux系统编程:fifo有名管道的使用

    fifo介绍 我们可以利用管道进行进程间通信,已经有匿名管道 为啥还要fifo 有名管道呢?有名管道是对匿名管道的一个补充,匿名管道是用在有血缘关系的进程间通信.fifo有名管道呢,可以用在任何进程间 ...

最新文章

  1. 基于EasyNVR摄像机网页无插件直播服务二次开发实现H5播放页面的简单集成方案...
  2. 选红叶家装不就得了,用得着这么费事吗?!
  3. docker部署django项目、mysql主从搭建、django实现读写分离
  4. PHP的urlencode
  5. react-native 打包apk 并解决 图片 不出现问题
  6. 在Eclipse中使用Java 12
  7. office数据集dslr_DSLR的完整形式是什么?
  8. BZOJ5020: [THUWC 2017]在美妙的数学王国中畅游(LCT,泰勒展开,二项式定理)
  9. 神经网络中的分类器该如何改成生成器?
  10. zk可实现分布式锁,Redis也可实现,之间有什么区别?
  11. VC2005绿色编译器--命令行下编译C++代码
  12. 深度学习中的优化算法之Adadelta
  13. python 经典图书排行榜_书榜 | 计算机书籍(3.30-4.5)销售排行榜
  14. 如何将本机的的git仓库提交到gitbub中
  15. 中国象棋棋盘java_中国象棋棋子及棋盘的绘制
  16. 人人商城之导出excel表
  17. 微信投票的自动运行脚本
  18. 【从零搭建后端基础设施系列(九)】-- VM容器化
  19. 清华叉院弋力:从谷歌研究科学家到清华任教,我想看远一点
  20. (转)Visual SourceSafe (VSS的使用方法)使用方法

热门文章

  1. 内链和外链到底是什么?
  2. 郑清 - 日常作息表 - July - 2019
  3. codefrces 1203 E. Boxers(贪心)
  4. 室内空气污染超标,新房装修如何去除甲醛浓度?
  5. java double 后缀_Java double数据类型
  6. 双十一夺冠的荣耀,扛住世界的阿特拉斯
  7. vs2015 打包程序(摘抄自博客园-流浪阿丁)
  8. hu不变矩--python
  9. android 录屏软件,如何制作教学视频?教学视频录制方法
  10. 最适合物联网应用的开源数据库