[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] The correct way and most succinct way to check if a pipe
From: |
Bob Proulx |
Subject: |
Re: [Help-bash] The correct way and most succinct way to check if a pipe is empty? |
Date: |
Sun, 15 Mar 2015 18:30:27 -0600 |
User-agent: |
Mutt/1.5.23 (2014-03-12) |
Peng Yu wrote:
> There are a number of discussions about how to check a pipe is empty.
> But some of the solutions are not correct and some are too long. Does
> anybody know the best way to check if a pipe is empty? Thanks.
This will be kernel specific. I know that different systems do it
differently. But on at least some the size of the pipe file returns
the number of characters buffered in the pipe. In others the the size
of the pipe is never accessible. You can test this on your system.
If stat(2) shows a size while there is data waiting then you have a
system that shows this information to you. If not then you don't.
The only reliable way I know to avoid blocking when reading from a
pipe is to use select(2) upon one opened for blocking read. The
select call will return an array of file descriptors indicating if
their is data to be read. (And on BSD 4.2 derived kernels such as
HP-UX select returns if read would block. Which is always true if the
file descriptor was opened non-blocking. But not very useful.)
Bob