管道缓冲区大小为4K或64K? [英] Pipe buffer size is 4k or 64k?

查看:653
本文介绍了管道缓冲区大小为4K或64K?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I read in multiple places that the default buffer size for a pipe is 4kB (for instance, here), and my ulimit -a tends to confirm that statement:

$ 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

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

But when I use a little program to test the buffer size (by writing into the pipe until the write() blocks), I see a limit of 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;
}

和它的输出:

$ ./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]

这有力地表明,管道缓冲区的大小实际上是64K!
这是怎么回事?这里

It strongly suggests that the pipe buffer size is actually 64k! What is happening here??

推荐答案

其他的答案告诉你,管道大小为64 KB。之所以PIPE_BUF是4KB是PIPE_BUF是它写的保证是原子的最大尺寸。见<一href=\"http://pubs.opengroup.org/onlinepubs/9699919799/functions/write.html\">http://pubs.opengroup.org/onlinepubs/9699919799/functions/write.html

The other answers tell you that the pipe size is 64 KB. The reason why PIPE_BUF is 4KB is that PIPE_BUF is the largest size for which writes are guaranteed to be atomic. See http://pubs.opengroup.org/onlinepubs/9699919799/functions/write.html

这篇关于管道缓冲区大小为4K或64K?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆