Alex Rivera | Logout

Set pipe buffer size

Asked 2011-03-07T10:50:39.810
19

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)

Edit
Report

1 Answer

21

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).

answered 2013-01-17T02:14:42.107

Your Answer