Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 15.5 FIFOs 555


input
file prog1 tee

FIFO prog3

prog2

Figure 15.21 Using a FIFO andteeto send a stream to two different processes

FIFO that the server creates. (By‘‘well-known,’’ we mean that the pathname of the
FIFO is known to all the clients that need to contact the server.) Figure15.22 shows this
arrangement.

client ... client

well-known
FIFO

server

readrequests

write

requests

write
requests

Figure 15.22 Clients sending requests to a server using a FIFO

Since thereare multiple writers for the FIFO, the requests sent by the clients to the
server need to be less thanPIPE_BUFbytes in size. This prevents any interleaving of
the clientwrites.
The problem in using FIFOs for this type of client–server communication is how to
send replies back from the server to each client. Asingle FIFO can’t be used, as the
clients would never know when to read their response versus responses for other
clients. One solution is for each client to send its process ID with the request. The
server then creates a unique FIFO for each client, using a pathname based on the client’s
process ID. For example, the server can create a FIFO with the name
/tmp/serv1.XXXXX,whereXXXXX is replaced with the client’s process ID. This
arrangement is shown in Figure15.23.
This arrangement works, although it is impossible for the server to tell whether a
client crashes. Aclient crash leaves the client-specific FIFO in the file system. The
Free download pdf