📄 _chapter 1.htm
字号:
<li><span style="FONT-WEIGHT: normal">
<p class="docList">Handling signals.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">Setting up programs for execution.</span></li>
</ol>
</span>
<p class="docText">Each of these topics is discussed in detail as it pertains to
a particular shell.</p>
<h3 class="docSection1Title" id="ch01lev1sec2">1.2 System Startup and the Login Shell</h3>
<p class="docText">When you start up your system, the first process is called
<span class="docEmphasis">init.</span> Each process has a process identification
number associated with it, called the <span class="docEmphasis">PID</span>.
Since <span class="docEmphasis">init</span> is the first process, its PID is 1.
The <span class="docEmphasis">init</span> process initializes the system and
then starts another process to open terminal lines and set up the standard input
(<span class="docEmphasis">stdin</span>), standard output (<span class="docEmphasis">stdout</span>),
and standard error (<span class="docEmphasis">stderr</span>), which are all
associated with the terminal. The standard input normally comes from the
keyboard; the standard output and standard error go to the screen. At this
point, a login prompt would appear on your terminal.</p>
<p class="docText">After you type your login name, you will be prompted for a
password. The <span class="docEmphasis">/bin/login</span> program then verifies
your identity by checking the first field in the <span class="docEmphasis">
passwd</span> file. If your username is there, the next step is to run the
password you typed through an encryption program to determine if it is indeed
the correct password. Once your password is verified, the
<span class="docEmphasis">login</span> program sets up an initial environment
consisting of variables that define the working environment that will be passed
on to the shell. The <span class="docEmphasis">HOME,</span>
<span class="docEmphasis">SHELL,</span> <span class="docEmphasis">USER,</span>
and <span class="docEmphasis">LOGNAME</span> variables are assigned values
extracted from information in the <span class="docEmphasis">passwd</span> file.
The <span class="docEmphasis">HOME</span> variable is assigned your home
directory; the <span class="docEmphasis">SHELL</span> variable is assigned the
name of the login shell, which is the last entry in the
<span class="docEmphasis">passwd</span> file. The <span class="docEmphasis">USER</span>
and/or <span class="docEmphasis">LOGNAME</span> variables are assigned your
login name. A <span class="docEmphasis">search path</span> variable is set so
that commonly used utilities may be found in specified directories. When
<span class="docEmphasis">login</span> has finished, it will execute the program
found in the last entry of the <span class="docEmphasis">passwd</span> file.
Normally, this program is a shell. If the last entry in the
<span class="docEmphasis">passwd</span> file is <span class="docEmphasis">/bin/csh,</span>
the C shell program is executed. If the last entry in the
<span class="docEmphasis">passwd</span> file is <span class="docEmphasis">/bin/sh</span>
or is null, the Bourne shell starts up. If the last entry is
<span class="docEmphasis">/bin/ksh,</span> the Korn shell is executed. This
shell is called the <span class="docEmphasis">login shell.</span></p>
<p class="docText">After the shell starts up, it checks for any systemwide
initialization files set up by the system administrator and then checks your
home directory to see if there are any shell-specific initialization files
there. If any of these files exist, they are executed. The initialization files
are used to further customize the user environment. After the commands in those
files have been executed, a prompt appears on the screen. The shell is now
waiting for your input.</p>
<h4 class="docSection2Title" id="ch01lev2sec6">1.2.1 Parsing the Command Line</h4>
<p class="docText">When you type a command at the prompt, the shell reads a line
of input and parses the command line, breaking the line into words, called
tokens. Tokens are separated by spaces and tabs and the command line is
terminated by a newline.<span id="ENB1-1"><a class="docLink" href="#EN1-1"><sup>[1]</sup></a></span>
The shell then checks to see whether the first word is a built-in command or an
executable program located somewhere out on disk. If it is built-in, the shell
will execute the command internally. Otherwise, the shell will search the
directories listed in the path variable to find out where the program resides.
If the command is found, the shell will fork a new process and then execute the
program. The shell will sleep (or wait) until the program finishes execution and
then, if necessary, will report the status of the exiting program. A prompt will
appear and the whole process will start again. The order of processing the
command line is as follows:</p>
<span style="FONT-WEIGHT: bold">
<ol class="docList" type="1">
<li><span style="FONT-WEIGHT: normal">
<p class="docList">History substitution is performed (if applicable).</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">Command line is broken up into tokens, or words.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">History is updated (if applicable).</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">Quotes are processed.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">Alias substitution and functions are defined (if
applicable).</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">Redirection, background, and pipes are set up.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">Variable substitution (<span class="docEmphasis">$user,</span>
<span class="docEmphasis">$name,</span> etc.) is performed.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">Command substitution (echo for <span class="docEmphasis">
today is 'date'</span>) is performed.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">Filename substitution, called <span class="docEmphasis">
globbing</span> (<span class="docEmphasis">cat abc.??,</span>
<span class="docEmphasis">rm *.c,</span> etc.) is performed.</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">Program execution.</span></li>
</ol>
</span>
<h4 class="docSection2Title" id="ch01lev2sec7">1.2.2 Types of Commands</h4>
<p class="docText">When a command is executed, it is an alias, a function, a
built-in command, or an executable program on disk. Aliases are abbreviations
(nicknames) for existing commands and apply to the C, TC, Bash, and Korn shells.
Functions apply to the Bourne (introduced with AT&T System V, Release 2.0),
Bash, and Korn shells. They are groups of commands organized as separate
routines. Aliases and functions are defined within the shell's memory. Built-in
commands are internal routines in the shell, and executable programs reside on
disk. The shell uses the path variable to locate the executable programs on disk
and forks a child process before the command can be executed. This takes time.
When the shell is ready to execute the command, it evaluates command types in
the following order:<span id="ENB1-2"><a class="docLink" href="#EN1-2"><sup>[2]</sup></a></span></p>
<span style="FONT-WEIGHT: bold">
<ol class="docList" type="1">
<li><span style="FONT-WEIGHT: normal">
<p class="docList">Aliases</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">Keywords</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">Functions (<span class="docEmphasis">bash</span>)</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">Built-in commands</span></li>
<li><span style="FONT-WEIGHT: normal">
<p class="docList">Executable programs</span></li>
</ol>
</span>
<p class="docText">If, for example, the command is <span class="docEmphasis">xyz</span>
the shell will check to see if <span class="docEmphasis">xyz</span> is an alias.
If not, is it a built-in command or a function? If neither of those, it must be
an executable command residing on the disk. The shell then must search the path
for the command.</p>
<h3 class="docSection1Title" id="ch01lev1sec3">1.3 Processes and the Shell</h3>
<p class="docText">A process is a program in execution and can be identified by
its unique PID (process identification) number. The kernel controls and manages
processes. A process consists of the executable program, its data and stack,
program and stack pointer, registers, and all the information needed for the
program to run. When you start the shell, it is a process. The shell belongs to
a process group identified by the group's PID. Only one process group has
control of the terminal at a time and is said to be running in the foreground.
When you log on, your shell is in control of the terminal and waits for you to
type a command at the prompt.</p>
<p class="docText">The shell can spawn other processes. In fact, when you enter
a command at the prompt or from a shell script, the shell has the responsibility
of finding the command either in its internal code (built-in) or out on the disk
and then arranging for the command to be executed. This is done with calls to
the kernel, called <span class="docEmphasis">system calls.</span> A system call
is a request for kernel services and is the only way a process can access the
system's hardware. There are a number of system calls that allow processes to be
created, executed, and terminated. (The shell provides other services from the
kernel when it performs redirection and piping, command substitution, and the
execution of user commands.)</p>
<p class="docText">The system calls used by the shell to cause new processes to
run are discussed in the following sections. See
<a class="docLink" href="#ch01fig02">Figure 1.2</a>.</p>
<center>
<h5 id="ch01fig02" class="docFigureTitle">Figure 1.2. The shell and command execution.</h5>
<p class="docText">
<img alt="graphics/01fig02.gif" src="01fig02.gif" border="0" width="500" height="695"></p>
</center>
<h4 class="docSection2Title" id="ch01lev2sec8">1.3.1 What Processes Are Running?</h4>
<p class="docText"><span class="docEmphStrong">The <span class="docEmphasis">ps</span>
Command.</span> The <span class="docEmphasis">ps</span> command with its many
options displays a list of the processes currently running in a number of
formats. <a class="docLink" href="#ch01list01">Example 1.1</a> shows all
processes that are running by users on a Linux system. (See
<a class="docLink" href="Appendix A.htm">Appendix A</a> for
<span class="docEmphasis">ps</span> and its options.)</p>
<h5 id="ch01list01" class="docExampleTitle">Example 1.1 </h5>
<pre>$ <span class="docEmphStrong">ps au</span> <span class="docEmphasis">(BSD/Linux ps) (use ps -ef for SVR4)</span>
USER PID %CPU %MEM SIZE RSS TTY STAT START TIME COMMAND
ellie 456 0.0 1.3 1268 840 1 S 13:23 0:00 -bash
ellie 476 0.0 1.0 1200 648 1 S 13:23 0:00 sh /usr/X11R6/bin/sta
ellie 478 0.0 1.0 2028 676 1 S 13:23 0:00 xinit /home/ellie/.xi
ellie 480 0.0 1.6 1852 1068 1 S 13:23 0:00 fvwm2
ellie 483 0.0 1.3 1660 856 1 S 13:23 0:00 /usr/X11R6/lib/X11/fv
ellie 484 0.0 1.3 1696 868 1 S 13:23 0:00 /usr/X11R6/lib/X11/fv
ellie 487 0.0 2.0 2348 1304 1 S 13:23 0:00 xclock -bg #c0c0c0 -p
ellie 488 0.0 1.1 1620 724 1 S 13:23 0:00 /usr/X11R6/lib/X11/fv
ellie 489 0.0 2.0 2364 1344 1 S 13:23 0:00 xload -nolabel -bg gr
ellie 495 0.0 1.3 1272 848 p0 S 13:24 0:00 -bash
ellie 797 0.0 0.7 852 484 p0 R 14:03 0:00 ps au
root 457 0.0 0.4 724 296 2 S 13:23 0:00 /sbin/mingetty tty2
root 458 0.0 0.4 724 296 3 S 13:23 0:00 /sbin/mingetty tty3
root 459 0.0 0.4 724 296 4 S 13:23 0:00 /sbin/mingetty tty4
root 460 0.0 0.4 724 296 5 S 13:23 0:00 /sbin/mingetty tty5
root 461 0.0 0.4 724 296 6 S 13:23 0:00 /sbin/mingetty tty6
root 479 0.0 4.5 12092 2896 1 S 13:23 0:01 X :0
root 494 0.0 2.5 2768 1632 1 S 13:24 0:00 nxterm -ls -sb -fn
</pre>
<h4 class="docSection2Title" id="ch01lev2sec9">1.3.2 Creating Processes</h4>
<p class="docText"><span class="docEmphStrong">The <span class="docEmphasis">
fork</span> System Call. </span>A process is created in UNIX with the
<span class="docEmphasis">fork</span> system call. The <span class="docEmphasis">
fork</span> system call creates a duplicate of the calling process. The new
process is called the <span class="docEmphasis">child</span> and the process
that created it is called the <span class="docEmphasis">parent.</span> The child
process starts running right after the call to <span class="docEmphasis">fork,</span>
and both processes initially share the CPU. The child process has a copy of the
parent's environment, open files, real and user identifications,
<span class="docEmphasis">umask,</span> current working directory, and signals.</p>
<p class="docText">When you type a command, the shell parses the command line
and determines whether the first word is a built-in command or an executable
command that resides out on the disk. If the command is built-in, the shell
handles it, but if on the disk, the shell invokes the <span class="docEmphasis">
fork</span> system call to make a copy of itself (<a class="docLink" href="#ch01fig03">Figure
1.3</a>). Its child will search the path to find the command, as well as set up
the file descriptors for redirection, pipes, command substitution, and
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -