c - How to avoid blocking read() from named pipe after it closes -


i have 2 processes created named pipe. writer process writes message using write(), , reader process reads message read(). noticed read() blocks when writer closes pipe. possible let writer process sends eof before closing pipe reader not blocked?

not... it's not possible send eof because eof nothing maps sent on channel. eof condition (yes, it's condition, not receive or send on channel) means there's no more data read(2) , it's got process making read(2) return 0 characters read. many reads in eof condition, return value of 0 meaning there's no more data.

by way, pipes block readers when there's no data available, but, writer still there send more data, there's no eof condition. design, pipes block readers when there's no data available (but writer still has pipe open) , block writers when there's no more space put data in fifo (this happens when there readers fifo open, none of them reading) see it's feature, not bug.

if want make read(2) non blocking , 0 when there's no data available, can open(2) o_nonblock flag (or later, fcntl(2) system call), make read(2) return available data (even if there's no data available) has drawback: cannot distinguish fifo no data available (it has not been written) actual eof (the writer has closed side) both return 0 result.

by way, don't use eof constant when talking end of file conditions, because adds confusion of definition. eof value returned getchar(3) on end of file condition, it's not char (if check definition of getchar(3) discover it's defined int , not char, add eof condition whole range of possible char values --- so, there 257 possible values returned getchar(3), 0-256 data, , eof end of file condition)

if want writers signal end of file have close(2) fifo , reopen it.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -