126
◾
Linux with Operating System Concepts
The fork system call duplicates the parent to initialize the child. The child then obtains
the same contents as is stored in memory for the parent including such values as the cur-
rent working directory, the terminal window in which the parent (and child)
was launched,
umask values, and so on. The only difference between the two is the PIDs of the processes
and the location in memory of the processes. For vfork, the two processes (parent and
child) actually share the same memory. The clone system call is similar to vfork. The two
processes share memory, but also share other resources.
With clone, you can create threads. The wait system call is like fork except that the par-
ent goes into a wait (sleep) mode until the child terminates. At that point, the parent can
resume execution.
The exec system call causes a process to start a program which replaces
the current pro-
cess. That is, the process invoking exec will be replaced by the new running process. The
new running process is given the parent process’ PID (in actuality, the new process merely
replaces the old). With exec, we actually have several different system calls available. They
vary in that the different forms launch the new process using a different collection of envi-
ronments. For instance, execl uses the same environment for the child as the parent except
that it uses the current process’ PATH variable to locate the executable.
As the parent process is
replaced with exec, it is common to instead pair up fork and
exec. First, the parent process issues a fork to create a new process with a new PID. This
new process is a duplicate of the parent. Now the child invokes exec to change the process
to one of a different program.
As an example, imagine that you have opened a terminal window which is running a
bash shell. From this shell, you invoke a program, say vi. The bash
shell first issues a fork
call to duplicate itself and then the child issues an exec to replace itself with the vi program.
We will explore function calls in more detail in Chapter 8.
Let us consider a more concrete example of parents and children. The first process to run
once the Linux kernel has initialized itself is init. After init initializes the operating system
(including the user interface), we have a child process, the GUI environment itself (let us
assume Gnome). To run some application, it is Gnome that spawns a child process through
fork and exec. If you are operating in a terminal window, then
the terminal window repre-
sents one process and spawns a shell process (for instance, bash). Commands that the user
enters from the command line interface are spawned by bash. Usually, a process run from
the command line executes and terminates before the command line becomes available to
the user.
Entering a command which contains a pipe actually spawns multiple processes. For
instance, from the command line, if you enter the command
ls
−
al | grep zappaf | wc
−
l
then bash spawns first an ls process
whose output is sent to grep, a second process, and that
output is redirected to
wc
−
l
, a third process.
Consider as a user you have logged into a Linux computer using the GUI. You open a
terminal window. Your default shell is bash, so the terminal window,
upon starting, runs
Managing Processes
◾
127
bash. From the bash shell, you launch a background process such as a script (we discuss
background processes in Section 4.2). Then you launch a foreground process, perhaps a
find operation. You have created a chain of processes. The init process spawns the GUI
which spawns a terminal window which spawns bash which spawns both a background
and a foreground process. The chain might look like this:
init
\--gnome
\--gnome_terminal
\--bash
\--./somescbash
--find
There are different types of processes that run in the system. As a user, we are mostly
interested in our own processes (which include threads). From a system’s
point of view,
there are also services. We will ignore services in this chapter but cover them in detail later
in the text.
4.2 FORMS OF PROCESS MANAGEMENT
Before we specifically address processes in Linux, let us first consider what the process is
and the different ways processes are run in operating systems. As defined in the previous
section, a process is a running program. Consider that a program is a piece of software. It
contains code and has memory space reserved for data storage. Most of this storage is dedi-
cated to variables that are declared and used in the program. The program exists in one of
two states, the original source code or the executable code. In either case,
the program is
a
Do'stlaringiz bilan baham: