Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Chapter 15 Exercises 587


To use this technique with XSI IPC, recall that theipc_permstructureassociated
with each message queue, semaphore, and shared memory segment identifies the
creator of the IPC structure(thecuidandcgidfields). As with the example using
FIFOs, the server should requirethe client to create the IPC structureand have the client
set the access permissions to user-read and user-write only.The times associated with
the IPC structureshould also be verified by the server to be recent (since these IPC
structures hang around until explicitly deleted).
We’ll see in Section 17.3 that a far better way of doing this authentication is for the
kernel to provide the effective user ID and effective group ID of the client. This is done
by the socket subsystem when file descriptors arepassed between processes.

15.12 Summary


We’ve detailed numerous forms of interprocess communication: pipes, named pipes
(FIFOs), the three forms of IPC commonly called XSI IPC (message queues, semaphores,
and shared memory), and an alternative semaphoremechanism provided by POSIX.
Semaphores arereally a synchronization primitive, not true IPC, and areoften used to
synchronize access to a shared resource, such as a shared memory segment.With pipes,
we looked at the implementation of thepopenfunction, at coprocesses, and at the
pitfalls that can be encountered with the standardI/O library’s buffering.
After comparing the timing of message queues versus full-duplex pipes, and
semaphores versus recordlocking, we can make the following recommendations: learn
pipes and FIFOs, since these two basic techniques can still be used effectively in
numerous applications. Avoid using message queues and semaphores in any new
applications. Full-duplex pipes and recordlocking should be considered instead, as
they arefar simpler.Shared memory still has its use, although the same functionality
can be provided through the use of themmapfunction (Section 14.8).
In the next chapter, we will look at network IPC, which can allow processes to
communicate across machine boundaries.

Exercises


15.1 In the program shown in Figure15.6, remove thecloseright beforethewaitpidat the
end of the parent code. Explain what happens.
15.2 In the program in Figure15.6, remove thewaitpidat the end of the parent code. Explain
what happens.
15.3 What happens if the argument topopenis a nonexistent command? Write a small
program to test this.
15.4 In the program shown in Figure15.18, remove the signal handler,execute the program,
and then terminate the child. After entering a line of input, how can you tell that the
parent was terminated bySIGPIPE?
15.5 In the program in Figure15.18, use the standardI/O library for reading and writing the
pipes instead ofreadandwrite.
Free download pdf