How to Change the Size of a Named Pipe on Linux

Is it possible to change the size of a named pipe on Linux?

With recent kernels (>= 2.6.35), you can change the size of a pipe with

fcntl(fd, F_SETPIPE_SZ, size)

where size is a long. The maximum size is in /proc/sys/fs/pipe-max-size.

named pipe blocks on writing before its maximum size

It's because of the way 4KB pages are filled with written data in the pipe implementation in the Linux kernel. More specifically, the kernel appends written data to a page only if the data fits entirely in the page, otherwise puts the data into another page with enough free bytes.

If you write 3 bytes at a time, the pipe pages won't be filled at their full capacity, because the page size (4096) is not a multiple of 3: the nearest multiple is 4095, so each page will end up with 1 "wasted" byte. Multiplying 4095 by 16, which is the total number of pages, you get 65520.

In your second use case, when you write 65520 bytes all at once, you are filling 15 pages entirely (61440 bytes), plus you are putting the remaining 4080 bytes in the last page, which will have 16 bytes still available for subsequent writes: that's why your second write() call with 3 bytes succeeds without blocking.

For full details on the Linux pipe implementation, see https://elixir.bootlin.com/linux/latest/source/fs/pipe.c.

Increasing the maximum pipe size in linux

Changing the value in /proc/sys/fs/pipe-max-size, seems to do the trick :)... Thanks Joachim for your pointer in the right direction.

My current linux has the max value as 1048576

Increase pipe internal buffer size on CentOS

On the kernel of CentoS 5, a pipe is fixed at 64 KB

Pipe buffer size is 4k or 64k?

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



Related Topics



Leave a reply



Submit