管道缓冲区大小是4k还是64k?

Gui*_*i13 47 c linux size buffer pipe

我在多个地方读到管道的默认缓冲区大小是4kB(例如,这里),我ulimit -a倾向于确认该语句:

$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 15923
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8 // 8 * 512B = 4kB
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 1024
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
Run Code Online (Sandbox Code Playgroud)

但是当我使用一个小程序来测试缓冲区大小时(通过写入管道直到write()块),我看到了64kB的限制!

看这个程序:

#include <stdio.h>
#include <unistd.h>
#include <limits.h>

int main(void)
{
    int tube[2];
    char c = 'c';
    int i;

    fprintf(stdout, "Tube Creation\n");
    fprintf(stdout, "Theoretical max size: %d\n", PIPE_BUF);
    if( pipe(tube) != 0)
    {
        perror("pipe");
        _exit(1);
    }
    fprintf(stdout, "Writing in pipe\n");
    for(i=0;; i++)
    {
        fprintf(stdout, "%d bytes written\n", i+1);
        if( write(tube[1], &c, 1) != 1)
        {
            perror("Write");
            _exit(1);
        }
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它的输出:

$ ./test_buf_pipe 
Tube Creation
Theoretical max size: 4096
Writing in pipe
1 bytes written
2 bytes written
3 bytes written
4 bytes written
[...]
65535 bytes written
[blocks here]
Run Code Online (Sandbox Code Playgroud)

它强烈建议管道缓冲区大小实际上是64k!这里发生了什么?

jan*_*neb 48

其他答案告诉您管道大小为64 KB.PIPE_BUF为4KB的原因是PIPE_BUF是保证写入是原子的最大大小.请参见http://pubs.opengroup.org/onlinepubs/9699919799/functions/write.html

  • 这是否意味着缓冲区可以任意增加?但是保证的原子写入仅限于 4K? (2认同)

Dig*_*oss 13

它现在是可编程的


从Linux 2.6.35开始,您可以使用fcntl(2)进行F_SETPIPE_SZ操作以将管道缓冲区设置为/proc/sys/fs/pipe-max-size.这默认为1 MB; 见proc(5).

  • 是的,但没有.'ulimit`广告的管道大小在内核3.2(我在这里的那个)上仍然是4kB.我的问题是关于PIPE_BUF,这是janneb回答的:PIPE_BUF是一个常量,是执行时将是原子的写入的最大大小(即,线程安全;)). (4认同)