Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a read-only open of a named pipe block?

I've noticed a couple of oddities when dealing with named pipes (FIFOs) under various flavors of UNIX (Linux, FreeBSD and MacOS X) using Python. The first, and perhaps most annoying is that attempts to open an empty/idle FIFO read-only will block (unless I use os.O_NONBLOCK with the lower level os.open() call). However, if I open it for read/write then I get no blocking.

Examples:

f = open('./myfifo', 'r')               # Blocks unless data is already in the pipe f = os.open('./myfifo', os.O_RDONLY)    # ditto  # Contrast to: f = open('./myfifo', 'w+')                           # does NOT block f = os.open('./myfifo', os.O_RDWR)                   # ditto f = os.open('./myfifo', os.O_RDONLY|os.O_NONBLOCK)   # ditto 

I'm just curious why. Why does the open call block rather than some subsequent read operation?

Also I've noticed that a non-blocking file descriptor can exhibit to different behaviors in Python. In the case where I use os.open() with the os.O_NONBLOCK for the initial opening operation then an os.read() seems to return an empty string if data isn't ready on the file descriptor. However, if I use fcntl.fcnt(f.fileno(), fcntl.F_SETFL, fcntl.GETFL | os.O_NONBLOCK) then an os.read raises an exception (errno.EWOULDBLOCK)

Is there some other flag being set by the normal open() that's not set by my os.open() example? How are they different and why?

like image 376
Jim Dennis Avatar asked Apr 25 '11 19:04

Jim Dennis


People also ask

Is read on pipe blocking?

Read/Write Are Blocking - when a process reads from a named pipe that has no data in it, the reading process is blocked. It does not receive an end of file (EOF) value, like when reading from a file.

Can Your named pipes work in unblocked mode?

Named pipes can also be used in Un-Blocked mode i.e., the sender (receiver) process will work independently of the other process.

Are named pipes blocking?

Both reading and writing are by default blocking. This means that if a process tries to read from a named-pipe that does not have data, it will block. Similarly, if a process write into a named-pipe that has not yet been opened by another process, the writer will block.

What are named pipes explain logical view of reading and writing pipes?

A named pipe is a one-way or duplex pipe that provides communication between the pipe server and some pipe clients. A pipe is a section of memory that is used for interprocess communication. A named pipe can be described as first in, first out (FIFO); the inputs that enter first will be output first.


1 Answers

That's just the way it's defined. From the Open Group page for the open() function

O_NONBLOCK      When opening a FIFO with O_RDONLY or O_WRONLY set: If O_NONBLOCK is     set:          An open() for reading only will return without delay. An open()         for writing only will return an error if no process currently         has the file open for reading.      If O_NONBLOCK is clear:          An open() for reading only will block the calling thread until a         thread opens the file for writing. An open() for writing only         will block the calling thread until a thread opens the file for         reading. 
like image 188
Jim Garrison Avatar answered Sep 23 '22 14:09

Jim Garrison