Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

736 Pseudo Terminals Chapter 19


pty slowout > file.out &
theptyprocess is stopped immediately when the child tries to read from its standard
input (the terminal). The reason is that the job is a background job and gets job-control
stopped when it tries to access the terminal. If we redirect standardinput so thatpty
doesn’t try to read from the terminal, as in
pty slowout < /dev/null > file.out &
theptyprogram stops immediately because it reads an end of file on its standardinput
and terminates. The solution for this problem is the-ioption, which says to ignorean
end of file on the standardinput:
pty -i slowout < /dev/null > file.out &
This flag causes theptychild in Figure19.12 to exit when the end of file is encountered,
but the child doesn’t tell the parent to terminate. Instead, the parent continues copying
the PTY slave output to standardoutput (the filefile.outin the example).

scriptProgram


Using theptyprogram, we can implement thescript( 1 )program as the following
shell script:
#!/bin/sh
pty "${SHELL:-/bin/sh}" | tee typescript
Once we run this shell script, we can execute thepscommand to see all the process
relationships. Figure19.14 details these relationships.

login
shell sh tee

pty
parent

pty
child ksh ps

typescript
file

line
discipline

TTY masterPTY

line
discipline

PTY
slave

user

pipe

Figure 19.14 Arrangement of processes forscriptshell script
Free download pdf