Pipe buffer size 4k or 64k?

I read in several places that the default buffer size for a channel is 4kB (for example, here ), and my ulimit -a tends to confirm this 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 

But when I use a small program to check the size of the buffer (by writing to the write () blocks), I see a limit of 64 KB!

See this program:

 #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; } 

And his conclusion:

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

This suggests that the buffer size for the buffer is actually 64 KB! What's going on here?

+43
c linux pipe size buffer
Jan 07 2018-11-11T00:
source share
5 answers

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

+43
Jan 07 2018-11-11T00:
source share

Now it is programmed




On Linux 2.6.35, you can use fcntl (2) with the F_SETPIPE_SZ operation to set the pipe buffer to /proc/sys/fs/pipe-max-size . This is the default 1 MB; see proc (5).

+12
Dec 16 '12 at 23:05
source share

In my experience, a single write test gave a total size of 65536, but when I wrote 2700 at a time, I could only write 16 times, and then the next attempt to stalls. I believe that an β€œatomic” record should be within the same 4 KB block, and for each of my records it will go to the next full block to satisfy the request. So the pipe size used depends on the size of your record.

+3
May 7 '12 at 16:11
source share

It looks like the kernel uses up to 16 buffers that add up to 64k. See Link for an explanation of ulimit output and actual buffer size.

+2
Jan 07 2018-11-11T00:
source share

It is right. Starting with kernel 2.6.11, the pipe size on Linux is 64 KB. Why ulimit reports this as 4Kb, I'm not sure, but this is wrong.

0
Jan 07 2018-11-11T00:
source share



All Articles