Information Technology Reference
In-Depth Information
file as input. The shell reads the file as a list of commands to invoke the
pre-processor, parser, code generator and assembler in turn, exactly as if
it was reading text input from the keyboard. When the last command
completes, the interpreter exits to inform the kernel that the program is
done.
A program can send its output to a file. By changing the stdout
le descriptor in the child, the shell can redirect the child's output to a
le. In the standard UNIX shell, this is signied with a \greater than"
symbol. Thus, \ls > tmp" lists the contents of the current directory into
the le \tmp". After the fork and before the exec, the shell can replace
the stdout file descriptor for the child using dup2 . Because the parent has
been cloned, changing stdout for the child has no effect on the parent.
A program can read its output from a file. Likewise, by using dup2
to change the stdin file descriptor, the shell can cause the child to read its
input from a le. In the standard UNIX shell, this is signied with a \less
than" symbol. Thus, \zork < solution" plays the game \zork" with a list
of instructions stored in the le \solution."
The output of one program can be the input to another program.
The shell can use a pipe to connect two programs together, so that the
output of one is the input of another. This is called a producer-consumer
Denition:
producer-consumer
relationship. For example, in the C-compiler, the output of the prepro-
cessor is sent to the parser, and the output of the parser is sent to the
code-generator and then to the assembler. In the standard UNIX shell, a
pipe connecting two programs is signied by a \|" symbol, as in: \cpp
le.c | cparse | cgen | as > le.o". In this case the shell creates four
separate child processes, each connected by pipes to its predecessor and
successor. Each of the phases can run in parallel, with the parent waiting
for all of them to finish.
Exercises
5. Suppose you were the instructor of a very large introductory programming
class. Explain (in English) how you would use UNIX system calls to
automate testing of submitted homework assignments.
6. What happens if you run \exec csh" in a UNIX shell? Why?
7. What happens if you run \exec ls" in a UNIX shell? Why?
Search WWH ::




Custom Search