Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 19.6 Using theptyProgram 737


In this example, we assume that theSHELL variable is the Korn shell (probably
/bin/ksh). As we mentioned earlier,scriptcopies only what is output by the new
shell (and any processes that it invokes), but since the line discipline module above the
PTY slave normally has echo enabled, most of what we type is also written to the
typescriptfile.

Running Coprocesses


In Figure15.18, the coprocess couldn’t use the standardI/O functions, because standard
input and standardoutput do not refer to a terminal, so the standardI/O functions treat
them as fully buffered. If we run the coprocess underptyby replacing the line
if (execl("./add2", "add2", (char *)0) < 0)
with
if (execl("./pty", "pty", "-e", "add2", (char *)0) < 0)
the program now works, even if the coprocess uses standardI/O.
Figure19.15 shows the arrangement of processes when we run the coprocess with a
pseudo terminal as its input and output. It is an expansion of Figure19.6, showing all
the process connections and data flow.The box labeled ‘‘driving program’’ is the
program from Figure15.18, with theexeclchanged as described previously.

driving program forkexec parentpty fork childpty (coprocess)add2

terminal
line discipline

terminal
device driver

terminal
line discipline

PTY master PTY slave

fork,exec

pipe2
pipe1

user at a
terminal

Figure 19.15 Running a coprocess with a pseudo terminal as its input and output

This example shows the need for the-e(no echo) option for theptyprogram. The
ptyprogram is not running interactively,because its standardinput is not connected to
aterminal. In Figure19.11, theinteractiveflag defaults to false, since the call to
Free download pdf