⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unx18.htm

📁 Linux Unix揭密.高质量电子书籍.对学习Linux有大帮助,欢迎下载学习.
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML>

<HEAD>

<TITLE>UNIX Unleashed unx18.htm</TITLE>

<LINK REL="ToC" HREF="index.htm">

<LINK REL="Next" HREF="unx19.htm">

<LINK REL="Previous" HREF="unxpt4au.htm"></HEAD>

<BODY TEXT="#000000" LINK="#0000FF" VLINK="#800080" bgcolor=white>

<P><A HREF="unxpt4au.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>

<A HREF="index.htm"><IMG SRC="blutoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>

<A HREF="unx19.htm"><IMG SRC="blunext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>

<A HREF="index.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Home"></A>

</P><UL>

<LI>

<A HREF="#I1">18 &#150; What Is a Process?</A></LI>

<UL>

<UL>

<UL>

<UL>

<LI>

<A HREF="#I2">By Rachel and Robert Sartin</A></LI></UL></UL>

<LI>

<A HREF="#I3">What Happens When You Execute a Command?</A></LI>

<UL>

<LI>

<A HREF="#I5">Forking a Process</A></LI>

<LI>

<A HREF="#I6">Running a Command</A></LI></UL>

<LI>

<A HREF="#I7">Looking at Process</A></LI>

<LI>

<A HREF="#I8">Visiting the Shell Again</A></LI>

<UL>

<LI>

<A HREF="#I9">Processing a Command</A></LI>

<UL>

<LI>

<A HREF="#I10">Checking the Aliases and Built-Ins</A></LI>

<LI>

<A HREF="#I11">Make a New Process with fork</A></LI>

<LI>

<A HREF="#I12">Start a New Command with exec</A></LI>

<LI>

<A HREF="#I13">An Example</A></LI></UL>

<LI>

<A HREF="#I14">Executing in the Background</A></LI>

<UL>

<LI>

<A HREF="#I15">An Example</A></LI></UL></UL>

<LI>

<A HREF="#I16">Kinks in Your Pipeline</A></LI>

<LI>

<A HREF="#I17">A Special Process Called Daemon</A></LI>

<UL>

<LI>

<A HREF="#I18">init</A></LI>

<LI>

<A HREF="#I19">inetd</A></LI>

<LI>

<A HREF="#I20">cron</A></LI></UL>

<LI>

<A HREF="#I21">Summary</A></LI></UL></UL></UL>



<H1 ALIGN="CENTER">

<CENTER><A ID="I1" NAME="I1">

<BR>

<FONT SIZE=5><B>18 &#150; What Is a Process?</B>

<BR></FONT></A></CENTER></H1>

<H5 ALIGN="CENTER">

<CENTER><A ID="I2" NAME="I2">

<FONT SIZE=3><B>By Rachel and Robert Sartin</B>

<BR></FONT></A></CENTER></H5>

<P>This chapter introduces the concept of processes and how you use UNIX to interact with them.

<BR></P>

<H3 ALIGN="CENTER">

<CENTER><A ID="I3" NAME="I3">

<FONT SIZE=4><B>What Happens When You Execute a Command?</B>

<BR></FONT></A></CENTER></H3>

<P><A ID="I4" NAME="I4"></A>When you execute a program on your UNIX system, the system creates a special environment for that program. This environment contains everything needed for the system to run the program as if no other program were running on the 

system.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I5" NAME="I5">

<FONT SIZE=3><B>Forking a Process</B>

<BR></FONT></A></CENTER></H4>

<P>Each process has process context, which is everything that is unique about the state of the program you are currently running. The process context includes then following:

<BR></P>

<UL>

<LI>The text (program instructions) being run

<BR>

<BR></LI>

<LI>The memory used by the program being run

<BR>

<BR></LI>

<LI>The current working directory

<BR>

<BR></LI>

<LI>The files that are open and positions in the files

<BR>

<BR></LI>

<LI>Resource limits

<BR>

<BR></LI>

<LI>Access control information

<BR>

<BR></LI>

<LI>Others&#151;various low-level information

<BR>

<BR></LI></UL>

<P>Every time you execute a program the UNIX system does a fork, which performs a series of operations to create a process context and then execute your program in that context. The steps include the following:

<BR></P>

<OL>

<LI>Allocate a slot in the process table, a list of currently running programs kept by UNIX. UNIX creates the illusion of multiple programs running simultaneously by switching quickly between active processes in the process table. This allocation can fail 

for a number of reasons, including these:

<BR>

<BR></LI>

<UL>

<LI>You have exceeded your per user process limit, the maximum number of processes your UNIX system will allow you to run.

<BR>

<BR></LI>

<LI>The system runs out of open process slots. The UNIX kernel stores information about currently running processes in a table of processes. When this table runs out of room for new entries, you are unable to fork a new process.

<BR>

<BR></LI>

<LI>UNIX has run out of memory and does not have room for the text and data of the new process.

<BR>

<BR></LI></UL>

<LI>Assign a unique process identifier (PID) to the process. This identifier can be used to examine and control the process later.

<BR>

<BR></LI>

<LI>Copy the context of the parent, the process that requested the spawning of the new process.

<BR>

<BR></LI>

<LI>Return the new PID to the parent process. This enables the parent process to examine or control the process directly.

<BR>

<BR></LI></OL>

<P>After the fork is complete, UNIX runs your program. One of the differences between UNIX and many other operating systems is that UNIX performs this two-step procedure to run a program. The first step is to create a new process that's just like the 
parent. The second is to execute a different program. This procedure allows interesting variations. (See the section &quot;A Special Process Called Daemon.&quot;)

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I6" NAME="I6">

<FONT SIZE=3><B>Running a Command</B>

<BR></FONT></A></CENTER></H4>

<P>When you enter ls to look at the contents of your current working directory, UNIX does a series of things to create an environment for ls and the run it:

<BR></P>

<OL>

<LI>The shell has UNIX perform a fork. This creates a new process that the shell will use to run the ls program.

<BR>

<BR></LI>

<LI>The shell has UNIX perform an exec of the ls program. This replaces the shell program and data with the program and data for ls and then starts running that new program.

<BR>

<BR></LI>

<LI>The ls program is loaded into the new process context, replacing the text and data of the shell.

<BR>

<BR></LI>

<LI>The ls program performs its task, listing the contents of the current directory.

<BR>

<BR></LI></OL>

<H3 ALIGN="CENTER">

<CENTER><A ID="I7" NAME="I7">

<FONT SIZE=4><B>Looking at Process</B>

<BR></FONT></A></CENTER></H3>

<P>Because processes are so important to getting things done, UNIX has several commands that enable you to examine processes and modify their state. The most frequently used command is ps, which prints out the process status for processes running on your 
system. Each system has a slightly different version of the ps command, but there are two main variants, the System V version and the Berkeley version, covered in this section. Different versions of ps do similar things, but have somewhat different output 

and are controlled using different options. The X/Open Portability Guide makes an attempt to standardize somewhat on output of the ps command. The ps command is covered in more detail in chapter 19, &quot;Administrative Processes.&quot;

<BR></P>

<P>On a System V or XPG-compliant system, you can examine all the processes you are running by entering ps -f and you will get output such as the following:

<BR></P>

<PRE>$ ps -f

     UID   PID  PPID  C    STIME TTY      TIME COMMAND

    root 14931   136  0 08:37:48 ttys0    0:00 rlogind

  sartin 14932 14931  0 08:37:50 ttys0    0:00 -sh

  sartin 15339 14932  7 16:32:29 ttys0    0:00 ps -f

$</PRE>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> After the first line, which is the header, each line of output tells about the status of a single process. The UID column tells the owner of the process. The PID column tells the process ID. The PPID 

tells the process ID of the parent process (the process that executed the fork). The STIME is the time the process began executing. The TIME is the amount of computer time the process has used. The COMMAND field tells what command line was executed.

<BR></NOTE>

<HR ALIGN=CENTER>

<P>Look at this example and you can see that root (the system administration user) is running rlogind as process 14931. This process is a special kind of administrative program, called a daemon (daemons are described in the section &quot;A Special Process 

Called Daemon&quot;). This particular daemon is responsible for managing a connection from rlogin, which is described in Chapter 8, &quot;Getting Around the Network.&quot; As you can see from the next line, there is a process called -sh, which is a Bourne 

shell. The shell has rlogind as its parent because the daemon did a fork to run the login shell. Similarly, there is a ps -f command that has the shell as its parent.

<BR></P>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="imp.gif" WIDTH = 68 HEIGHT = 35><B>TIP:</B> The leading hyphen on the -sh in the output of ps means that the shell is executing as a login shell, which does certain special processing that other instances of the shell do not. See the chapter on 
your shell for more information on login shells.

<BR></NOTE>

<HR ALIGN=CENTER>

<H3 ALIGN="CENTER">

<CENTER><A ID="I8" NAME="I8">

<FONT SIZE=4><B>Visiting the Shell Again</B>

<BR></FONT></A></CENTER></H3>

<P>Earlier in this chapter you learned that the shell creates a new process for each command you execute. This section covers in a bit more detail how the shell creates and manages processes.

<BR></P>

<H4 ALIGN="CENTER">

<CENTER><A ID="I9" NAME="I9">

<FONT SIZE=3><B>Processing a Command</B>

<BR></FONT></A></CENTER></H4>

<P>When you type a command to your shell user interface, the shell performs a series of tasks to process the command. Although the steps may seem a bit cumbersome at first, they create an environment that is highly flexible.

<BR></P>

<H5 ALIGN="CENTER">

<CENTER><A ID="I10" NAME="I10">

<FONT SIZE=3><B>Checking the Aliases and Built-Ins</B>

<BR></FONT></A></CENTER></H5>

<P>The first thing the shell does is alias and built-in processing to see if your command is one of the shell's internally implemented functions. Each shell implements a number of functions internally either because external implementation would be 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -