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)
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.
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).
The pipe size is advertized by ulimit is still 4kB on Kernel 3.2 (the one I have here).
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With