Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set pipe buffer size

Tags:

c

pipe

buffer

I have a C++ multithreaded application which uses posix pipes in order to perform inter thread communications efficiently (so I don't have to get crazy with deadlocks).

I've set the write operation non-blocking, so the writer will get an error if there is not enough space in the buffer to write.

if((pipe(pipe_des)) == -1)
    throw PipeException();

int flags = fcntl(pipe_des[1], F_GETFL, 0); // set write operation non-blocking
assert(flags != -1);
fcntl(pipe_des[1], F_SETFL, flags | O_NONBLOCK);

Now I'd wish to set the pipe buffer size to a custom value (one word in the specific case).

I've googled for it but I was not able to find anything useful. Is there a way (possibly posix compliant) to do it?

Thanks

Lorenzo

PS: I'm under linux (if it may be useful)

like image 968
Zeruel Avatar asked Mar 07 '11 10:03

Zeruel


People also ask

What is pipe buffer size in Linux?

Since Linux 2.6. 11, the pipe capacity is 16 pages (i.e., 65,536 bytes in a system with a page size of 4096 bytes). Since Linux 2.6. 35, the default pipe capacity is 16 pages, but the capacity can be queried and set using the fcntl(2) F_GETPIPE_SZ and F_SETPIPE_SZ operations.

What is pipe buffer?

PIPES Buffer (CAS 5625-37-6) with full chemical name as 1,4-Piperazinediethanesulfonic acid, is a zwitterionic biological buffer. It is a white powder solid at normal temperature. PIPES Buffer is not very soluble in water (1 g/L (100 °C).

What is pipe size in Ulimit?

The pipe size is advertized by ulimit is still 4kB on Kernel 3.2 (the one I have here).

What is PIPE_BUF?

PIPE_BUF is an implementation-specific constant that specifies the maximum number of bytes that are atomic when writing to a pipe. When writing to a pipe, write requests of PIPE_BUF or fewer bytes are not interleaved with data from other processes doing writes to the same pipe.


1 Answers

Since you mentioned you are on Linux and may not mind non-portability, you may be interested in the file descriptor manipulator F_SETPIPE_SZ, available since Linux 2.6.35.

int pipe_sz = fcntl(pipe_des[1], F_SETPIPE_SZ, sizeof(size_t));

You'll find that pipe_sz == getpagesize() after that call, since the buffer cannot be made smaller than the system page size. See fcntl(2).

like image 131
Jeremy Fishman Avatar answered Oct 05 '22 18:10

Jeremy Fishman