Using select on FIFO's

Leslie Mikesell les at chinet.chi.il.us
Wed Jun 5 08:52:10 AEST 1991


In article <2729 at root44.co.uk> gwc at root.co.uk (Geoff Clare) writes:

>Although this works on most if not all current UNIX systems, it's not
>required by POSIX, so applications using this method may not work
>on some future systems.  For maximum portability to current and
>future systems, I recommend using the following method to open both
>sides of a FIFO to do blocking I/O:
>
>	fd_rd = open("FIFO", O_RDONLY|O_NONBLOCK);
>	fd_wr = open("FIFO", O_WRONLY);
>	flags = fcntl(fd_rd, F_GETFL);
>	fcntl(fd_rd, F_SETFL, flags & ~O_NONBLOCK);

Is it required to work when the process opens with one fd for
reading and a different one for writing?  I do that in shell
scripts to make them block when no one else is writing to
the FIFO:
---simple shell script server---
setup code...
while read CMD P1 P2 P3 P4 P5 P6
do
 case "$CMD" in
  STOP)
    exit 0
     ;;
   whatever else...
  esac
done <FIFO 4>FIFO

I just start one of these in the background to schedule jobs and
other scripts periodically echo commands to it.  IMHO FIFO's would
be pretty useless if you couldn't get reasonable blocking to
happen from a shell script.  I also use the non-blocking mode
that happens after the first writer closes in some other scripts.
The setup is similar except for the 4>FIFO and a calling script
always writes a dummy command after starting the daemon script in
the background.  In this case the daemon program has something to
do in the body of the loop and only checks periodically for controlling
commands.  If nothing has been written since the last time around
the read returns EOF (nothing) and the loop continues.
Is this behaviour portable?

Les Mikesell
  les at chinet.chi.il.us



More information about the Comp.unix.programmer mailing list