Pipes and FIFOs 909
44.8 A Client-Server Application Using FIFOs
In this section, we present a simple client-server application that employs FIFOs for
IPC. The server provides the (trivial) service of assigning unique sequential numbers
to each client that requests them. In the course of discussing this application, we
introduce a few concepts and techniques in server design.
Application overview
In the example application, all clients send their requests to the server using a
single server FIFO. The header file (Listing 44-6) defines the well-known name
(/tmp/seqnum_sv) that the server uses for its FIFO. This name is fixed, so that all cli-
ents know how to contact the server. (In this example application, we create the
FIFOs in the /tmp directory, since this allows us to conveniently run the programs
without change on most systems. However, as noted in Section 38.7, creating files in
publicly writable directories such as /tmp can lead to various security vulnerabilities
and should be avoided in real-world applications.)
In client-server applications, we’ll repeatedly encounter the concept of a well-
known address or name used by a server to make its service visible to clients.
Using a well-known address is one solution to the problem of how clients can
know where to contact a server. Another possible solution is to provide some
kind of name server with which servers can register the names of their ser-
vices. Each client then contacts the name server to obtain the location of the
service it desires. This solution allows the location of servers to be flexible, at
the cost of some extra programming effort. Of course, clients and servers
then need to know where to contact the name server; typically, it resides at a
well-known address.
It is not, however, possible to use a single FIFO to send responses to all clients,
since multiple clients would race to read from the FIFO, and possibly read each
other’s response messages rather than their own. Therefore, each client creates a
unique FIFO that the server uses for delivering the response for that client, and the
server needs to know how to find each client’s FIFO. One possible way to do this is
for the client to generate its FIFO pathname, and then pass the pathname as part of
its request message. Alternatively, the client and server can agree on a convention
for constructing a client FIFO pathname, and, as part of its request, the client can
pass the server the information required to construct the pathname specific to this
client. This latter solution is used in our example. Each client’s FIFO name is built
from a template (CLIENT_FIFO_TEMPLATE) consisting of a pathname containing the client’s
process ID. The inclusion of the process ID provides an easy way of generating a
name unique to this client.
Figure 44-6 shows how this application uses FIFOs for communication between
the client and server processes of our application.
The header file (Listing 44-6) defines the formats for the request messages sent
from clients to the server, and for the response messages sent from the server to clients.