Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

588 Interprocess Communication Chapter 15


15.6 The Rationale for POSIX.1 gives as one of the reasons for adding thewaitpidfunction
that most pre-POSIX.1 systems can’t handle the following:
if ((fp = popen("/bin/true", "r")) == NULL)
...
if ((rc = system("sleep 100")) == -1)
...
if (pclose(fp) == -1)
...
What happens in this code ifwaitpidisn’t available andwaitis used instead?
15.7 Explain howselectandpollhandle an input descriptor that is a pipe, when the pipe is
closed by the writer.Todetermine the answer,write two small test programs: one using
selectand one usingpoll.
Redo this exercise, looking at an output descriptor that is a pipe, when the read end is
closed.
15.8 What happens if thecmdstringexecuted bypopenwith atypeof"r"writes to its standard
error?
15.9 Sincepopeninvokes a shell to execute itscmdstring argument, what happens when
cmdstringterminates? (Hint: Draw all the processes involved.)
15.10 POSIX.1 specifically states thatopening a FIFO for read–write is undefined. Although
most UNIX systems allow this, show another method for opening a FIFO for both reading
and writing, without blocking.
15.11 Unless a file contains sensitive or confidential data, allowing other users to read the file
causes no harm. (It is usually considered antisocial, however, to go snooping around in
other people’s files.) But what happens if a malicious process reads a message from a
message queue that is being used by a server and several clients? What information does
the malicious process need to know to read the message queue?
15.12 Write a program that does the following. Execute a loop five times: create a message
queue, print the queue identifier,delete the message queue. Then execute the next loop
five times: create a message queue with a key ofIPC_PRIVATE,and place a message on
the queue. After the program terminates, look at the message queues usingipcs( 1 ).
Explain what is happening with the queue identifiers.
15.13 Describe how to build a linked list of data objects in a shared memory segment. What
would you store as the list pointers?
15.14 Draw a timeline of the program in Figure15.33 showing the value of the variableiin both
the parent and child, the value of the long integer in the shared memory region, and the
value returned by theupdatefunction. Assume that the child runs first after thefork.
15.15 Redo the program in Figure15.33 using the XSI shared memory functions from
Section 15.9 instead of the shared memory-mapped region.
15.16 Redo the program in Figure15.33 using the XSI semaphorefunctions from Section 15.8 to
alternate between the parent and the child.
15.17 Redo the program in Figure15.33 using advisory recordlocking to alternate between the
parent and the child.
15.18 Redo the program in Figure15.33 using the POSIX semaphorefunctions from Section 15.10
to alternate between the parent and the child.
Free download pdf