Slave processes

Occasionally, child programs both accept data from and return data to their callers through pipes. Unlike simple shellouts, 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 dead trivial, or (b) the slave process has been designed to speak an application protocol along the lines we discussed in Chapter 5 (Textuality). We'll return to this issue, and ways to cope with it, in Chapter 8 (Minilanguages).

One common case where 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. [30]



[30] The friend who suggested this case study comments: “Yes, you can get away with this technique...if there are just a few easily-recognizable nuggets of information coming back from the slave process, and you have tongs and a radiation suit.”