10.2. How New Processes are Created in Unix
Before we talk about process management in detail, let's talk about
what happens when you run a simple program, such as
from your shell prompt. Essentially,
you just asked the OS to start an entire sequence of events:
- The shell, using the PATH environment variable, searches
for a file called fgrep. It also looks for the file myfile
in your current directory.
- Assuming that fgrep is found in /usr/bin, the system checks
to see if it is executable. This is done by checking the permission
bits.
- Assuming that fgrep has the right permissions bits, the header
of the executable fgrep
is read. It contains information about the format and
structure of the program fgrep.
- The fork system call is made by the shell to create a new Unix
process. This new process is identical to the shell process (in
that the address space of the child process contains the shell's
text and data segments).
- The shell makes the exec system call to execute fgrep.
In the process, the text and data segments of fgrep replace
those of the shell's in the child process.
- The child fgrep process is run, results are displayed,
the child process quits, and control is returned back to the
parent shell.
Items 4 and 5 above constitute the basic mechanism (fork and exec)
by which new processes
are created in Unix.