LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Data queued in a named pipe. (https://www.linuxquestions.org/questions/programming-9/data-queued-in-a-named-pipe-916778/)

abhishekbatra 12-02-2011 09:44 AM

Data queued in a named pipe.
 
Is there any way to find out how much data is queued in/written to a POSIX named pipe?

Thanks,
Abhishek

Nominal Animal 12-03-2011 09:29 AM

Other than reading the pipe, not that I know of.

When using sockets (for example, a socket pair created using socketpair(2)), one can use recv(descriptor,buffer,buffersize,MSG_NOWAIT|MSG_PEEK) to read but not consume already buffered data from the socket. It will not work with named pipes, however; only with sockets.

abhishekbatra 12-03-2011 09:51 AM

Thank you for the helpful reply. For the problem I had at hand I only needed to know when the pipe is empty, so the reader can stop reading. After your conformation and going through the manpage again I guess a call to read(int fildes, void *buf, size_t nbyte) is enough for that.

But just out of curiosity and for possible utility, is there a way to peek into the underlying kernel data structures to find out the fifo size, if anyone knows that?

Nominal Animal 12-03-2011 12:51 PM

Quote:

Originally Posted by abhishekbatra (Post 4541036)
Thank you for the helpful reply. For the problem I had at hand I only needed to know when the pipe is empty, so the reader can stop reading. After your conformation and going through the manpage again I guess a call to read(int fildes, void *buf, size_t nbyte) is enough for that.

You may wish to open the pipe nonblocking (or set it nonblocking using fcntl(descriptor,F_SETFL,O_NONBLOCK)), so the call will return immediately if there is no data to read from the pipe.

Quote:

Originally Posted by abhishekbatra (Post 4541036)
But just out of curiosity and for possible utility, is there a way to peek into the underlying kernel data structures to find out the fifo size, if anyone knows that?

Only if using kernel 2.6.35 or later. Then, you have fcntl(descriptor,F_GETPIPE_SZ) to find out the capacity of the pipe (if the descriptor refers to a pipe), and fcntl(descriptor,F_GETPIPE_SZ,size) to set the pipe capacity. If setting a pipe capacity that is not a multiple of page size (sysconf(_SC_PAGESIZE)), the kernel will round it upwards to the next multiple of page size.

An userspace application may adjust the pipe capacity freely between one page (sysconf(_SC_PAGESIZE) bytes) and /proc/sys/fs/pipe-size-max bytes.


All times are GMT -5. The time now is 03:36 PM.