|
Unix Programming - Taxonomy of Unix IPC Methods - Slave Processes
Slave Processes
Occasionally, child programs both accept data from and return
data to their callers through
pipes connected to
standard input and output, interactively. Unlike simple shellouts and
what we have called ‘bolt-ons’ above, both master and
slave processes need to have internal state machines to handle a
protocol between them without deadlocking or racing. This is a
drastically more complex and more difficult-to-debug organization than
a simple shellout.
Unix's
popen(3)
call can set up either an input pipe or an output pipe for a shellout,
but not both for a slave process — this seems intended to
encourage simpler programming. And, in fact, interactive master-slave
communication is tricky enough that it is normally only used when
either (a) the implied protocol is utterly trivial, or (b) the slave
process has been designed to speak an application protocol along the
lines we discussed in Chapter5. We'll return to this issue, and ways
to cope with it, in Chapter8.
When writing a master/slave pair, it is good practice for the
master to support a command-line switch or environment variable that
allows callers to set their own slave command. Among other things,
this is useful for debugging; you will often find it handy during
development to invoke the real slave process from within a harness
that monitors and logs transactions between slave and master.
If you find that master/slave interactions in your program are
becoming nontrivial, it may be time to think about going the rest of
the way to a more peer-to-peer organization, using techniques like
sockets or shared memory.
One common case in which the implied protocol really is trivial is
progress meters. The
scp(1)
secure-copy command calls
ssh(1)
as a slave process, intercepting enough information from ssh's
standard output to reformat the reports as an ASCII animation of a
progress bar.[73]
[an error occurred while processing this directive]
|