FIFOs

Paul Falstad pfalstad at phoenix.Princeton.EDU
Mon Jan 21 16:11:13 AEST 1991


byron at archone.tamu.edu (Byron Rakitzis) wrote:
>I'm presently writing a shell to run on many systems, and one of
>the features of the shell is a new kind of redirection. This redirection
>has been implemented in an AT&T shell using /dev/fd, but I'm wondering
>if I can implement it with FIFOs or not.

zsh implements this with FIFOs, so yes, it can be done.

>The syntax of this redirection is:
>cat <{foo}
>
>What it does is to supply cat a single argument, the name of a FIFO,
>and the output of "foo" is redirected into this FIFO. The problem
>is this: cat will block indefinitely on reading from the FIFO if it
>tries to read more bytes than have been written to the FIFO by foo.
>Presumably this is because cat does not have O_NDELAY set for files
>that it opens.

Why is this a problem?  When 'foo' exits, closing its half of the pipe,
cat's read() will return with the last part of "foo"'s output.  When cat
tries to read again, it will get an EOF.  Setting O_NDELAY would not
be good, since if cat runs faster than foo (which it probably does) it
will eventually try to read before foo is ready to write, and therefore
get an EOF and exit.

>My question is this: is there anything "foo" can do so that when "foo"
>is finished writing, cat's read() returns without blocking? Setting
>the O_NDELAY flag from cat's side is not allowed, since cat has to
>treat the FIFO as any other argument. However, the shell does have
>some control over how "foo" writes to the FIFO.

Not that I know of.  But you wouldn't want it to anyway, as I said.

When writing zsh, I had lots of problems with the mail shell not closing
pipe file descriptors at the right times; "cat" would block even though
"foo" had exited, because the main shell had a copy of the writing end
of the pipe still open while waiting for cat, and a deadlock resulted.
Make sure you close the file descriptors for pipes as soon as possible.

--
Paul Falstad, pfalstad at phoenix.princeton.edu PLink:HYPNOS GEnie:P.FALSTAD
In the heat of composition I find that I have inadvertently allowed
myself to assume the form of a large centipede.  I am accordingly
dictating the rest to my secretary.



More information about the Comp.unix.programmer mailing list