📄 349-351.html
字号:
<HTML>
<HEAD>
<TITLE>Special Edition Using Linux, Fourth Edition:Understanding Linux Shells</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0789717468//-->
<!--TITLE=Special Edition Using Linux, Fourth Edition//-->
<!--AUTHOR=Jack Tackett//-->
<!--AUTHOR=Jr.//-->
<!--AUTHOR=Steve Burnett//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Que//-->
<!--CHAPTER=18//-->
<!--PAGES=349-351//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="347-349.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="351-353.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading6"></A><FONT COLOR="#000077">Understanding Processes</FONT></H4>
<P>A running program in Linux is called a <BIG>process</BIG>. Because Linux is a multitasking system, many processes can run at the same time. To distinguish between processes, Linux assigns each new process a unique ID called a <BIG>process ID</BIG>.</P>
<P>The process ID is simply a number that uniquely identifies each running process. To see what process IDs are now associated with your process, use the <TT>ps</TT> command. To look at most of the process IDs now running on your system, issue the command with the flags <TT>-guax</TT>, and you see something like the following:</P>
<!-- CODE //-->
<PRE>
USER PID %CPU %MEM SIZE RSS TTY STAT START TIME COMMAND
jack 53 3.2 7.0 352 468 p 1 S 02:01 0:01 -bash
jack 65 0.0 3.5 80 240 p 1 R 02:01 0:00 ps -guax
root 1 0.8 3.1 44 208 con S 02:00 0:00 init
root 6 0.0 1.8 24 124 con S 02:00 0:00 bdflush (daemon)
root 7 0.0 1.9 24 128 con S 02:01 0:00 update (bdflush)
root 40 1.0 3.5 65 240 con S 02:01 0:00 /usr/sbin/syslogd
root 42 0.2 2.9 36 200 con S 02:01 0:00 /usr/sbin/klogd
root 44 0.5 3.2 68 216 con S 02:01 0:00 /usr/sbin/inetd
root 46 0.2 3.0 64 204 con S 02:01 0:00 /usr/sbin/lpd
root 52 0.1 2.0 32 140 con S 02:01 0:00 selection -t ms
root 58 0.2 2.4 37 164 p 6 S 02:01 0:00 /sbin/agetty 38400 tt
</PRE>
<!-- END CODE //-->
<P>The process ID is identified by the column titled <TT>PID</TT>. Also note the boldfaced line, which indicates the first process started by the system—<TT>init</TT>. The <TT>init</TT> process is also described later in this chapter.</P>
<P>When Linux is told to run a program (that is, to create a process), it does so by making an exact copy of the program making the request. In the simplest case, you request that a program be run by telling your shell; the shell makes a fork request to the Linux kernel.</P>
<P><FONT SIZE="+1"><B>Fork, init, and the exec Process</B></FONT></P>
<P>A <BIG>fork</BIG> is the process of cloning an existing process. Linux creates all new processes through the mechanism of forking. When a process is forked, an almost exact duplicate of an existing process (including its environment and any open files) is created; what keeps the duplicate from being exactly the same as its parent application is a flag that tells the forked process which is the parent and which is the child.</P>
<P>Because all processes are created in this fashion, all processes have a parent process and a parent-process ID. Every process running on a Linux system can trace its lineage back to <TT>init</TT>, the mother of all processes. <TT>init</TT> itself, process ID 1, is the only process run directly by the Linux kernel that you as a user have any contact with. Every process you create during a session has your login shell as an ancestor, and your login shell has <TT>init</TT> as its parent.</P>
<P>After a process successfully forks, the child process calls the <TT>exec</TT> routine to transform itself into the process you requested. The only thing that changes after an <TT>exec</TT> function is the identity of the running process; the environment of the new process is an exact copy of the environment of its parent.</P>
<P><FONT SIZE="+1"><B>Standard Input and Output</B></FONT></P>
<P>Every new process is created with three open “files.” Because Linux treats files and devices exactly the same, an open “file” can be a real file on a disk or a device such as your terminal. The three open files are defined as standard input (stdin), standard output (stdout), and standard error output (stderr). All Linux commands, as well as application programs, accept input from the standard input and place any output on the standard output. Any diagnostic messages are automatically placed on the standard error output.
</P>
<P>When you first log in, the standard input, output, and error files are attached to your terminal; any programs you run (processes you create) inherit your terminal as the three open files.</P>
<H3><A NAME="Heading7"></A><FONT COLOR="#000077">Understanding Shell Command Parsing</FONT></H3>
<P><BIG>Parsing</BIG> is the act of splitting the command line, or what you type, into its component parts for processing. In Linux, parsing constitutes a lot more than simply splitting the command line. The command string is first split into its component parts: the filenames expanded if you used any wild cards, shell variables expanded, I/O redirection set up, any command groupings or subshells set up, and command substitution performed. Only then can the command line, as you typed it, be executed.</P>
<P>If terms such as <BIG>wild cards</BIG> and <BIG>I/O redirection</BIG> are new to you, you can find explanations of them, in the order they’re performed, later in this chapter. You must first start, however, with the basic command syntax.</P>
<H4 ALIGN="LEFT"><A NAME="Heading8"></A><FONT COLOR="#000077">Using Commands, Flags, and Parameters</FONT></H4>
<P>To execute a Linux command, you merely type the name of the file. The command to list files is <TT>ls</TT>; you can find a file by that name in the /bin directory. If /bin is listed in your <TT>PATH</TT> variable (and it should be), your shell finds and executes /bin/ls.</P>
<P>Some Linux commands aren’t independent files. These commands are built into the shells themselves. For example, the <TT>cd</TT> (change directory) command is built into most shells and executed directly by the shell without looking up a file. Read the man pages for the shell you’re using to determine what commands are executed internally or externally. Some shells have a command file that contains commands executed directly by the shell.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="347-349.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="351-353.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -