﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-PIGWORLD-文章分类-《UNIX环境高级编程（第二版）》翻译</title><link>http://www.cppblog.com/Pigwen/category/710.html</link><description>学无止境</description><language>zh-cn</language><lastBuildDate>Tue, 20 May 2008 06:49:31 GMT</lastBuildDate><pubDate>Tue, 20 May 2008 06:49:31 GMT</pubDate><ttl>60</ttl><item><title>1.7 错误处理</title><link>http://www.cppblog.com/Pigwen/articles/2522.html</link><dc:creator>PIGWORLD</dc:creator><author>PIGWORLD</author><pubDate>Mon, 09 Jan 2006 14:58:00 GMT</pubDate><guid>http://www.cppblog.com/Pigwen/articles/2522.html</guid><wfw:comment>http://www.cppblog.com/Pigwen/comments/2522.html</wfw:comment><comments>http://www.cppblog.com/Pigwen/articles/2522.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/Pigwen/comments/commentRss/2522.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Pigwen/services/trackbacks/2522.html</trackback:ping><description><![CDATA[<H1 style="MARGIN: 17pt 0cm 16.5pt"><SPAN lang=EN-US>1.7 </SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">错误处理</SPAN></H1>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">当</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系统函数发生错误时，通常返回一个负值，并且整数</SPAN><SPAN lang=EN-US style="COLOR: red">errno</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">被设置为一个可以给出额外信息的值。例如，</SPAN><SPAN lang=EN-US style="COLOR: red">open</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数或者返回一个非负的文件描述符（当一切正常时），或者产生一个错误。一个</SPAN><SPAN lang=EN-US style="COLOR: red">open</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的错误能产生</SPAN><SPAN lang=EN-US>15</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">个可能的</SPAN><SPAN lang=EN-US style="COLOR: red">errno</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">值，例如文件不存在，权限问题，等等。一些函数不返回负值，而是使用习惯方法来表示错误。例如，多数函数返回一个对象的指针，而返回一个</SPAN><SPAN lang=EN-US>null</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">（空）指针来表示一个错误。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">头文件</SPAN><SPAN lang=EN-US style="COLOR: red">&lt;errno.h&gt;</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">定义了标识符</SPAN><SPAN lang=EN-US style="COLOR: red">errno</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US style="COLOR: red">errno</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的每个可能的常量值。这之中的每个常量值都以字符</SPAN><SPAN lang=EN-US style="COLOR: red">E</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">开头。在</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系统手册第二节的第一页，名为</SPAN><SPAN lang=EN-US style="COLOR: red">intro</SPAN><SPAN lang=EN-US>(2)</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的页面中，同样列出了这之中所有的错误常量。例如，如果</SPAN><SPAN lang=EN-US style="COLOR: red">errno</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">等于常量</SPAN><SPAN lang=EN-US style="COLOR: red">EACCES</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，这就显示了一个权限错误，比如没有足够的权限来打开所请求的文件。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在</SPAN><SPAN lang=EN-US>Linux</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中，错误常量被列举在手册</SPAN><SPAN lang=EN-US style="COLOR: red">errno</SPAN><SPAN lang=EN-US>(3)</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71"><SPAN lang=EN-US>POSIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US>ISO C</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">把</SPAN><SPAN lang=EN-US style="COLOR: red">errno</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">扩展定义为可变的整型左值。它既可以是一个包含了错误代码的整数，也可以是一个函数，该函数返回指向错误代码的指针。以前的定义是</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71"><SPAN lang=EN-US><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71"><SPAN lang=EN-US style="COLOR: red">extern int errno;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71"><SPAN lang=EN-US style="COLOR: red"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">然而在一个支持线程的环境中，进程地址空间在多个线程中共享，同时每个线程都需要</SPAN><SPAN lang=EN-US style="COLOR: red">errno</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的本地拷贝来防止线程间互相影响。例如，</SPAN><SPAN lang=EN-US>Linux</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">通过以下定义来支持多线程访问</SPAN><SPAN lang=EN-US style="COLOR: red">errno</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">：</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71"><SPAN lang=EN-US><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; TEXT-ALIGN: left; mso-char-indent-count: 1.71" align=left><SPAN lang=EN-US style="COLOR: red">extern int *_ _errno_location(void);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71"><SPAN lang=EN-US style="COLOR: red">#define errno (*_ _errno_location())<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71"><SPAN lang=EN-US style="COLOR: red"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71"><SPAN lang=EN-US style="COLOR: red">errno</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">有两条规则。第一，如果不发生错误，</SPAN><SPAN lang=EN-US style="COLOR: red">errno</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的值决不会被程序清除。因此，只有在函数的返回值表示错误发生时，才需要检查</SPAN><SPAN lang=EN-US style="COLOR: red">errno</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的值。第二，任何函数都不会把</SPAN><SPAN lang=EN-US style="COLOR: red">errno</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的值设置为</SPAN><SPAN lang=EN-US>0</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，同时在</SPAN><SPAN lang=EN-US style="COLOR: red">&lt;errno.h&gt;</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中也没有定义任何常量值为</SPAN><SPAN lang=EN-US>0</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">标准</SPAN><SPAN lang=EN-US>C</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中定义了两个函数来帮助打印错误消息。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71"><SPAN lang=EN-US><o:p>&nbsp;</o:p></SPAN></P>
<TABLE class=MsoTableGrid style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; MARGIN: auto auto auto 23.4pt; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse; mso-border-alt: solid windowtext .5pt; mso-yfti-tbllook: 480; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-border-insideh: .5pt solid windowtext; mso-border-insidev: .5pt solid windowtext" cellSpacing=0 cellPadding=0 border=1>
<TBODY>
<TR style="mso-yfti-irow: 0; mso-yfti-firstrow: yes; mso-yfti-lastrow: yes">
<TD style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 1pt solid; WIDTH: 402.7pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt" vAlign=top width=537>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="COLOR: red">#include &lt;string.h&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="COLOR: red">char *strerror(int errnum);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: right" align=right><SPAN style="COLOR: red; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">返回值：指向消息字符串的指针</SPAN><SPAN lang=EN-US style="COLOR: red"><o:p></o:p></SPAN></P></TD></TR></TBODY></TABLE>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71"><SPAN lang=EN-US><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">该函数把</SPAN><SPAN lang=EN-US style="COLOR: red">errno</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的典型值</SPAN><SPAN lang=EN-US>errnum</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">映射到一个错误消息字符串，并返回一个指向字符串的指针。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71"><SPAN lang=EN-US style="COLOR: red">perror</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数在标准错误产生并返回一个错误消息，该消息基于</SPAN><SPAN lang=EN-US style="COLOR: red">errno</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的当前值。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71"><SPAN lang=EN-US><o:p>&nbsp;</o:p></SPAN></P>
<TABLE class=MsoTableGrid style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; MARGIN: auto auto auto 23.4pt; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse; mso-border-alt: solid windowtext .5pt; mso-yfti-tbllook: 480; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-border-insideh: .5pt solid windowtext; mso-border-insidev: .5pt solid windowtext" cellSpacing=0 cellPadding=0 border=1>
<TBODY>
<TR style="mso-yfti-irow: 0; mso-yfti-firstrow: yes; mso-yfti-lastrow: yes">
<TD style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: windowtext 1pt solid; WIDTH: 402.7pt; PADDING-TOP: 0cm; BORDER-BOTTOM: windowtext 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid windowtext .5pt" vAlign=top width=537>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="COLOR: red">#include &lt;stdio.h&gt;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="COLOR: red">void perror(const char *msg);<o:p></o:p></SPAN></P></TD></TR></TBODY></TABLE>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71"><SPAN lang=EN-US><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">它输出</SPAN><SPAN lang=EN-US>msg</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">指向的字符串，接着是一个分号和一个空格，然后是与</SPAN><SPAN lang=EN-US style="COLOR: red">errno</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">值对应的错误消息，最后是一个新行。</SPAN></P>
<H3 style="MARGIN: 13pt 0cm"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><FONT size=5>例子</FONT></SPAN></H3>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">图</SPAN><SPAN lang=EN-US>1.8</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">展示了这两个函数的应用。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">如果该程序被编译为文件</SPAN><SPAN lang=EN-US>a.out</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，我们将看到</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN lang=EN-US><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 35.9pt; mso-char-indent-count: 3.42; mso-outline-level: 5"><SPAN lang=EN-US style="COLOR: red">$<B style="mso-bidi-font-weight: normal"> ./a.out</B><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 35.9pt; mso-char-indent-count: 3.42; mso-outline-level: 5"><SPAN lang=EN-US style="COLOR: red">EACCES: Permission denied<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 35.9pt; mso-char-indent-count: 3.42; mso-outline-level: 5"><SPAN lang=EN-US style="COLOR: red">./a.out: No such file or directory<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN lang=EN-US><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">注意我们把程序名字</SPAN><SPAN lang=EN-US style="COLOR: red">argv[0]</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">作为参数传递给</SPAN><SPAN lang=EN-US style="COLOR: red">perror</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，</SPAN><SPAN lang=EN-US style="COLOR: red">argv[0]</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的值是</SPAN><SPAN lang=EN-US style="COLOR: red">./a.out</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。这是</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系统的一个标准惯例。通过这样做，如果程序是作为管道的一部分执行，就像在</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN lang=EN-US><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 35.9pt; mso-char-indent-count: 3.42; mso-outline-level: 5"><SPAN lang=EN-US style="COLOR: red">prog1 &lt; inputfile | prog2 | prog3 &gt; outputfile<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 35.9pt; mso-char-indent-count: 3.42; mso-outline-level: 5"><SPAN lang=EN-US style="COLOR: red"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">我们就能够分清是三个程序中是哪个产生了错误消息。<BR><BR></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center; mso-outline-level: 5" align=center><SPAN lang=EN-US><?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><v:shapetype id=_x0000_t75 stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></v:path><o:lock aspectratio="t" v:ext="edit"></o:lock></v:shapetype></SPAN></P>
<P class=MsoCaption style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align=center><FONT size=2><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial"></P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #008080">&nbsp;1</SPAN><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">apue.h</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;2</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">errno.h</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;3</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;4</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">&nbsp;5</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top>main(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;argc,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">argv[])<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;6</SPAN><SPAN style="COLOR: #000000"><IMG id=Codehighlighter1_71_183_Open_Image onclick="this.style.display='none'; Codehighlighter1_71_183_Open_Text.style.display='none'; Codehighlighter1_71_183_Closed_Image.style.display='inline'; Codehighlighter1_71_183_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_71_183_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_71_183_Closed_Text.style.display='none'; Codehighlighter1_71_183_Open_Image.style.display='inline'; Codehighlighter1_71_183_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_71_183_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.cppblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_71_183_Open_Text><SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;7</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">EACCES:&nbsp;%s\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;strerror(EACCES));<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;8</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;errno&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;ENOENT;<BR></SPAN><SPAN style="COLOR: #008080">&nbsp;9</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;perror(argv[</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">]);<BR></SPAN><SPAN style="COLOR: #008080">10</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;exit(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">11</SPAN><SPAN style="COLOR: #000000"><IMG src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV>
<P class=MsoCaption style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align=center>图</SPAN><SPAN lang=EN-US><FONT face=Arial> 1.8 <SPAN style="COLOR: red">strerror</SPAN></FONT></SPAN><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">和</SPAN><SPAN lang=EN-US style="COLOR: red"><FONT face=Arial>perror</FONT></SPAN><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">的示范<BR></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">本书中的所有的例子都使用附录</SPAN><SPAN lang=EN-US>B</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的错误函数，来代替直接调用</SPAN><SPAN lang=EN-US style="COLOR: red">strerror</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">或者</SPAN><SPAN lang=EN-US style="COLOR: red">perror</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。附录中的错误函数使用了</SPAN><SPAN lang=EN-US>ISO C</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的可变参数列表，可以只用单个</SPAN><SPAN lang=EN-US>C</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">语句来处理错误情况。</SPAN></P>
<H2 style="MARGIN: 13pt 0cm"><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">错误恢复</SPAN></H2>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN lang=EN-US style="COLOR: red">&lt;errno.h&gt;</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中定义的错误可以被分为两类：致命的和非致命的。一个致命错误是不能够恢复的。最好的办法是在用户的屏幕上打印一条错误消息，或者在一个日志文件中写入错误消息，接着在退出。另一方面，非致命错误在某些时候能够更得体的处理。多数非致命错误是自然的临时错误，例如当系统的活动程序较少时，（系统）资源短缺的错误可能就不会发生。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">资源相关的非致命错误包括</SPAN><SPAN lang=EN-US style="COLOR: red">EAGAIN</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，</SPAN><SPAN lang=EN-US style="COLOR: red">ENFILE</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，</SPAN><SPAN lang=EN-US style="COLOR: red">ENOBUFS</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，</SPAN><SPAN lang=EN-US style="COLOR: red">ENOLCK</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，</SPAN><SPAN lang=EN-US style="COLOR: red">ENOSPC</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，</SPAN><SPAN lang=EN-US style="COLOR: red">ENOSR</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，</SPAN><SPAN lang=EN-US style="COLOR: red">EWOULDBLOCK</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，当</SPAN><SPAN lang=EN-US style="COLOR: red">ENOMEM</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，</SPAN><SPAN lang=EN-US style="COLOR: red">EBUSH</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">表示一个共享资源正在被使用时，它们也可以被作为非致命错误。某些时候，当</SPAN><SPAN lang=EN-US style="COLOR: red">EINT</SPAN><SPAN lang=EN-US>R</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中断了一个缓慢的系统调用时，它也可以被看作非致命错误（详见</SPAN><SPAN lang=EN-US>10.5</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">节）。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">资源相关的非致命错误的典型恢复动作就是延迟一会儿再试。这个技巧也能应用在其它循环中。例如，如果错误表示网络连接没有工作，那么程序可能会延迟一会儿再重新建立连接。一些程序使用指数增长的算法，每次等待更长的时间。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">最后，由应用程序开发者来决定哪些错误是可以恢复的。如果一个合理的策略能够被用来恢复错误，通过避免异常退出，我们就可以增强我们程序的健壮性。</SPAN></P><img src ="http://www.cppblog.com/Pigwen/aggbug/2522.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Pigwen/" target="_blank">PIGWORLD</a> 2006-01-09 22:58 <a href="http://www.cppblog.com/Pigwen/articles/2522.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>1.6 程序和进程</title><link>http://www.cppblog.com/Pigwen/articles/2459.html</link><dc:creator>PIGWORLD</dc:creator><author>PIGWORLD</author><pubDate>Thu, 05 Jan 2006 16:25:00 GMT</pubDate><guid>http://www.cppblog.com/Pigwen/articles/2459.html</guid><wfw:comment>http://www.cppblog.com/Pigwen/comments/2459.html</wfw:comment><comments>http://www.cppblog.com/Pigwen/articles/2459.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/Pigwen/comments/commentRss/2459.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Pigwen/services/trackbacks/2459.html</trackback:ping><description><![CDATA[<H1 style="MARGIN: 17pt 0cm 16.5pt"><SPAN lang=EN-US>1.6 </SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">程序和进程</SPAN></H1>
<H2 style="MARGIN: 13pt 0cm"><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">程序</SPAN></H2>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">程序是存在于磁盘上目录中的可执行文件。</SPAN><SPAN lang=EN-US>6</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">个</SPAN><SPAN lang=EN-US>exec</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数中的任意一个，都可以将一个程序读入内存中并由内核执行（感觉这句没有翻译好，原句是：</SPAN><SPAN lang=EN-US>A program is read into memory and is executed by the kernel as a result of one of the six </SPAN><TT><SPAN lang=EN-US style="FONT-SIZE: 12pt"><FONT face=宋体>exec</FONT></SPAN></TT><SPAN lang=EN-US> functions.</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）。我们将在</SPAN><SPAN lang=EN-US>8.10</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">节中介绍这些函数。</SPAN></P>
<H2 style="MARGIN: 13pt 0cm"><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">进程和进程</SPAN><SPAN lang=EN-US><FONT face=Arial>ID</FONT></SPAN></H2>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">一个正在执行中的程序实例被称为进程（</SPAN><SPAN lang=EN-US>process</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">），该词语（进程）几乎会出现在本书中的每一页。一些操作系统用任务（</SPAN><SPAN lang=EN-US>task</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）来称呼一个正在运行中的程序。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系统确保每一个进程都拥有一个唯一的数字标识符，称为进程</SPAN><SPAN lang=EN-US>ID</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。进程</SPAN><SPAN lang=EN-US>ID</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">总是非负整数。</SPAN></P>
<H3 style="MARGIN: 13pt 0cm"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><FONT size=5>例子</FONT></SPAN></H3>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">图</SPAN><SPAN lang=EN-US>1.6</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的程序打印出它的进程</SPAN><SPAN lang=EN-US>ID</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">如果我们把程序编译成</SPAN><SPAN lang=EN-US>a.out</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">并执行它，我们会看到</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN lang=EN-US style="COLOR: red">$<B style="mso-bidi-font-weight: normal"> ./a.out<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></B></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN lang=EN-US style="COLOR: red">hello world from process ID 851<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN lang=EN-US style="COLOR: red">$<B style="mso-bidi-font-weight: normal"> ./a.out</B></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN lang=EN-US style="COLOR: red">hello world from process ID 854<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">该程序运行时调用</SPAN><SPAN lang=EN-US>getpid</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数来获得进程</SPAN><SPAN lang=EN-US>ID</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。<BR></P></SPAN><FONT size=2><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5" align=center></P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #008080">1</SPAN>&nbsp;<SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">apue.h</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">2</SPAN>&nbsp;<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">3</SPAN>&nbsp;<SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">4</SPAN>&nbsp;<SPAN style="COLOR: #000000">main(</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">)<BR></SPAN><SPAN style="COLOR: #008080">5</SPAN>&nbsp;<SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">6</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">hello&nbsp;world&nbsp;from&nbsp;process&nbsp;ID&nbsp;%d\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;getpid());<BR></SPAN><SPAN style="COLOR: #008080">7</SPAN>&nbsp;<SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;exit(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">8</SPAN>&nbsp;<SPAN style="COLOR: #000000">}</SPAN></DIV>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5" align=center>图</SPAN><SPAN lang=EN-US><FONT face=Arial> 1.6 </FONT></SPAN><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">打印进程</SPAN><SPAN lang=EN-US><FONT face=Arial>ID</FONT></SPAN></FONT></P>
<H2 style="MARGIN: 13pt 0cm"><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">进程控制</SPAN></H2>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">进程控制主要使用三个函数：</SPAN><SPAN lang=EN-US>fork</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，</SPAN><SPAN lang=EN-US>exec</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US>waitpid</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。（</SPAN><SPAN lang=EN-US>exec</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数有</SPAN><SPAN lang=EN-US>6</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">个变体，我们通常把它们统称为</SPAN><SPAN lang=EN-US>exec</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数。）</SPAN></P>
<H3 style="MARGIN: 13pt 0cm"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><FONT size=5>例子</FONT></SPAN></H3>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">使用一个简单的程序（图</SPAN><SPAN lang=EN-US>1.7</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）来展示</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系统的进程控制特性，该程序从标准输入读取命令并且执行这些命令。这是一个类似</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">程序的本质（翻译得不好，原句是：</SPAN><SPAN lang=EN-US>This is a bare-bones implementation of a shell-like program.</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）。在这个</SPAN><SPAN lang=EN-US>30</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">行的程序中有许多特性值得思考。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: -21pt; mso-outline-level: 6; mso-list: l0 level1 lfo1; tab-stops: list 21.0pt"><SPAN lang=EN-US style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN style="mso-list: Ignore">l<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">使用标准</SPAN><SPAN lang=EN-US>I/O</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数</SPAN><SPAN lang=EN-US>fgets</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，一次从标准输入中读取一行。当输入文件终止符（通常是</SPAN><SPAN lang=EN-US>Control-D</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）作为一行的第一个字符时，</SPAN><SPAN lang=EN-US>fgets</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">返回</SPAN><SPAN lang=EN-US>null</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">指针，同时终止循环，接着终止进程。在</SPAN><SPAN lang=EN-US>18</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">章中，我们描述所有特殊的终端字符（文件终止符，退格字符，整行擦除字符等等），并且介绍怎样改变它们。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: -21pt; mso-outline-level: 6; mso-list: l0 level1 lfo1; tab-stops: list 21.0pt"><SPAN lang=EN-US style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN style="mso-list: Ignore">l<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN lang=EN-US>fgets</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">返回的每一行都终止于一个换行符和跟在换行符后面的一个</SPAN><SPAN lang=EN-US>null</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">字节，我们使用标准的</SPAN><SPAN lang=EN-US>C</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数</SPAN><SPAN lang=EN-US>strlen</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">来计算字符串的长度，接着把换行符替换为一个</SPAN><SPAN lang=EN-US>null</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">字节。这样做是因为</SPAN><SPAN lang=EN-US>execlp</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数需要一个以</SPAN><SPAN lang=EN-US>null</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">结束的参数，而不是以换行符结束的参数。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: -21pt; mso-outline-level: 6; mso-list: l0 level1 lfo1; tab-stops: list 21.0pt"><SPAN lang=EN-US style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN style="mso-list: Ignore">l<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">调用</SPAN><SPAN lang=EN-US>fork</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数来建立一个进程，这个进程是一个调用者的拷贝。我们把调用者称为父进程，把新建立的进程称为子进程。那么</SPAN><SPAN lang=EN-US>fork</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数返回子进程的非负进程</SPAN><SPAN lang=EN-US>ID</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">给父进程，同时返回</SPAN><SPAN lang=EN-US>0</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">给子进程。因为</SPAN><SPAN lang=EN-US>fork</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">创建了一个新进程，我们说</SPAN><SPAN lang=EN-US>fork</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">被调用一次就返回两次，一次返回给父进程，一次返回给子进程。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: -21pt; mso-outline-level: 6; mso-list: l0 level1 lfo1; tab-stops: list 21.0pt"><SPAN lang=EN-US style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN style="mso-list: Ignore">l<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在子进程中，调用</SPAN><SPAN lang=EN-US>execlp</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">来执行从标准输入读取的命令。这就把子进程替换为一个新的程序文件。</SPAN><SPAN lang=EN-US>fork</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数后跟</SPAN><SPAN lang=EN-US>exec</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数的组合，就是一些操作系统所谓的产生一个新进程。（翻译得不好，原句是：</SPAN><SPAN lang=EN-US>This replaces the child process with the new program file. The combination of a </SPAN><TT><SPAN lang=EN-US style="FONT-SIZE: 12pt"><FONT face=宋体>fork</FONT></SPAN></TT><SPAN lang=EN-US>, followed by an </SPAN><TT><SPAN lang=EN-US style="FONT-SIZE: 12pt"><FONT face=宋体>exec</FONT></SPAN></TT><SPAN lang=EN-US>, is what some operating systems call spawning a new process.</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）在</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系统中，这两部分被分为了独立的函数。第八章中将会介绍更多这方面的内容。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: -21pt; mso-outline-level: 6; mso-list: l0 level1 lfo1; tab-stops: list 21.0pt"><SPAN lang=EN-US style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN style="mso-list: Ignore">l<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">因为子进程调用</SPAN><SPAN lang=EN-US>execlp</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">来执行新的程序文件，父进程就会等待直到子进程结束。这些工作由</SPAN><SPAN lang=EN-US>waitpid</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数完成。</SPAN><SPAN lang=EN-US>waitpid</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数中的</SPAN><SPAN lang=EN-US>pid</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">参数代表了子进程的进程</SPAN><SPAN lang=EN-US>ID</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，该参数用来标识出需要等待的进程。</SPAN><SPAN lang=EN-US>waitpid</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数也可以得到子进程的终止状态。在这个简单程序中的的</SPAN><SPAN lang=EN-US>status</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">变量就代表了子进程的终止状态，在程序中我们没有使用这个值，但是我们可以通过检查这个值来确定子进程的终止状态。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: -21pt; mso-outline-level: 6; mso-list: l0 level1 lfo1; tab-stops: list 21.0pt"><SPAN lang=EN-US style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN style="mso-list: Ignore">l<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这个程序最根本的限制在于我们不能像我们所执行的命令传递参数。例如，不能列举特定的目录。我们只可以在工作目录执行</SPAN><SPAN lang=EN-US>ls</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">命令。为了能够传递参数，我们需要分析输入行，按某种习惯（可能的情况是使用空格或者制表符）区别出参数，接着把每一个参数作为独立的参数传递给</SPAN><SPAN lang=EN-US>execlp</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数。尽管如此，这个程序仍然对</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系统进程控制函数进行了有用说明。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">运行这个程序，我们得到下面的输出。注意我们的程序有一个不同的提示符，使用百分号（</SPAN><SPAN lang=EN-US>%</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）来区别于</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的提示符。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 6"><SPAN lang=EN-US style="COLOR: red">$ <B style="mso-bidi-font-weight: normal">./a.out</B><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 6"><SPAN lang=EN-US style="COLOR: red">%<B style="mso-bidi-font-weight: normal"> date</B><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 6"><SPAN lang=EN-US style="COLOR: red">Sun Aug 1 03:04:47 EDT 2004<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 6"><SPAN lang=EN-US style="COLOR: red">%<B style="mso-bidi-font-weight: normal"> who</B><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 6"><SPAN lang=EN-US style="COLOR: red">sar<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>:0<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Jul 26 22:54<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 6"><SPAN lang=EN-US style="COLOR: red">sar<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>pts/0<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Jul 26 22:54<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>(:0)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 6"><SPAN lang=EN-US style="COLOR: red">sar<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>pts/1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Jul 26 22:54<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>(:0)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 6"><SPAN lang=EN-US style="COLOR: red">sar<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>pts/2<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Jul 26 22:54<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>(:0)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 6"><SPAN lang=EN-US style="COLOR: red">%<B style="mso-bidi-font-weight: normal"> pwd</B><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 6"><SPAN lang=EN-US style="COLOR: red">/home/sar/bk/apue/2e<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 6"><SPAN lang=EN-US style="COLOR: red">% <B style="mso-bidi-font-weight: normal">ls</B><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 6"><SPAN lang=EN-US style="COLOR: red">Makefile<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 6"><SPAN lang=EN-US style="COLOR: red">a.out<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 6"><SPAN lang=EN-US style="COLOR: red">shell1.c<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 6"><SPAN lang=EN-US style="COLOR: red">% <B style="mso-bidi-font-weight: normal">^D</B><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><I style="mso-bidi-font-style: normal"><SPAN style="COLOR: red; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">输入文件终止符</SPAN></I><SPAN lang=EN-US style="COLOR: red"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 6"><SPAN lang=EN-US style="COLOR: red">$<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><I style="mso-bidi-font-style: normal"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>shell</I></SPAN><I style="mso-bidi-font-style: normal"><SPAN style="COLOR: red; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">提示符<BR></SPAN></I><FONT size=2><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial"></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 6" align=center>&nbsp;</P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN style="COLOR: #000000">#include&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">apue.h</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"><BR>#include&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">sys</SPAN><SPAN style="COLOR: #000000">/</SPAN><SPAN style="COLOR: #000000">wait.h</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"><BR><BR></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"><BR>main(</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;buf[MAXLINE];&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">&nbsp;from&nbsp;apue.h&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;pid_t&nbsp;&nbsp;&nbsp;pid;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;status;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">%%&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">&nbsp;print&nbsp;prompt&nbsp;(printf&nbsp;requires&nbsp;%%&nbsp;to&nbsp;print&nbsp;%)&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">while</SPAN><SPAN style="COLOR: #000000">&nbsp;(fgets(buf,&nbsp;MAXLINE,&nbsp;stdin)&nbsp;</SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000">&nbsp;NULL)&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;(buf[strlen(buf)&nbsp;</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">]&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">\n</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buf[strlen(buf)&nbsp;</SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">]&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;&nbsp;</SPAN><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">&nbsp;replace&nbsp;newline&nbsp;with&nbsp;null&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN><SPAN style="COLOR: #000000"><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;((pid&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;fork())&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;err_sys(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">fork&nbsp;error</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</SPAN><SPAN style="COLOR: #0000ff">else</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;(pid&nbsp;</SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">&nbsp;child&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;execlp(buf,&nbsp;buf,&nbsp;(</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;err_ret(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">couldn't&nbsp;execute:&nbsp;%s</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,&nbsp;buf);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(</SPAN><SPAN style="COLOR: #000000">127</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000">&nbsp;parent&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN><SPAN style="COLOR: #000000"><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;((pid&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;waitpid(pid,&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">status,&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">))&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;err_sys(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">waitpid&nbsp;error</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">%%&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);<BR>}<BR><BR></SPAN></DIV>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 6" align=center>图</SPAN><SPAN lang=EN-US><FONT face=Arial> 1.7 </FONT></SPAN><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">从标准输入读取命令并执行它们</SPAN></FONT></P>
<P class=MsoCaption style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US><o:p><FONT face=Arial size=2>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">符号</SPAN><SPAN lang=EN-US>^D</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">用代表一个控制字符。控制字符是一种特殊的的字符，可以通过同时按下控制键（在你的计算机上通常为</SPAN><SPAN lang=EN-US>Control</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">键或</SPAN><SPAN lang=EN-US>Ctrl</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">键）和另一个键来产生它。</SPAN><SPAN lang=EN-US>Control-D</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，或者说</SPAN><SPAN lang=EN-US>^D</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，是默认的文件终止符。在</SPAN><SPAN lang=EN-US>18</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">章讨论终止</SPAN><SPAN lang=EN-US>I/O</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的时候会看到更多的控制字符。</SPAN></P>
<H2 style="MARGIN: 13pt 0cm"><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">线程和线程</SPAN><SPAN lang=EN-US><FONT face=Arial>ID</FONT></SPAN></H2>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">通常，一个进程只有一个线程（原句是：</SPAN><SPAN lang=EN-US>Usually, a process has only one thread of control one set of machine instructions executing at a time.</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">不知道怎么翻译，留给大虾翻译）。当有多于一个的线程来控制不同部分时，一些问题就变得很容易解决。另外，多线程在多处理器系统上能够获得平衡性（又一句翻译得不爽：</SPAN><SPAN lang=EN-US>Additionally, multiple threads of control can exploit the parallelism possible on multiprocessor systems.</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">一个进程中的所有线程共享同一个地址空间，文件描述符，栈和进程相关的属性。因为能够访问相同的内存，线程必须同步访问它们自己的共享数据，以避免冲突。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和进程一样，线程由线程</SPAN><SPAN lang=EN-US>ID</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">标识。尽管如此，线程</SPAN><SPAN lang=EN-US>ID</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">对于进程</SPAN><SPAN lang=EN-US>ID</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">来说是本地的。也就是说，一个进程中的线程</SPAN><SPAN lang=EN-US>ID</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在另一个进程中是没有意义的。当我们在进程中操作线程的时候，使用线程</SPAN><SPAN lang=EN-US>ID</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">来指出特定的线程。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-char-indent-count: 1.71; mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">控制线程的函数和控制进程的函数是一样的。在进程模型建立很久以后，线程才被加入到</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系统中，然而，线程模型和进程模型之间有一些复杂的交互，在</SPAN><SPAN lang=EN-US>12</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">章将会看到这些。</SPAN></P><img src ="http://www.cppblog.com/Pigwen/aggbug/2459.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Pigwen/" target="_blank">PIGWORLD</a> 2006-01-06 00:25 <a href="http://www.cppblog.com/Pigwen/articles/2459.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>1.5 输入和输出</title><link>http://www.cppblog.com/Pigwen/articles/2408.html</link><dc:creator>PIGWORLD</dc:creator><author>PIGWORLD</author><pubDate>Tue, 03 Jan 2006 14:04:00 GMT</pubDate><guid>http://www.cppblog.com/Pigwen/articles/2408.html</guid><wfw:comment>http://www.cppblog.com/Pigwen/comments/2408.html</wfw:comment><comments>http://www.cppblog.com/Pigwen/articles/2408.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Pigwen/comments/commentRss/2408.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Pigwen/services/trackbacks/2408.html</trackback:ping><description><![CDATA[<H2 style="MARGIN: 13pt 0cm" align=center><SPAN lang=EN-US><FONT face=Arial>1.5 </FONT></SPAN><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">输入和输出</SPAN></H2>
<H3 style="MARGIN: 13pt 0cm"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><FONT size=5>文件描述符</FONT></SPAN></H3>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">文件描述符通常是小的且非负的整数，内核使用它来标识能被特定进程访问的文件。只要打开或创建了新文件，内核就返回一个文件描述符，我们使用它来读写文件。</SPAN></P>
<H3 style="MARGIN: 13pt 0cm"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><FONT size=5>标准输入，标准输出和标准错误</FONT></SPAN></H3>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">通常，一旦一个新的程序运行，所有的</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">就为其打开三个描述符：标准输入，标准输出和标准错误。如果不做什么特别的事，就像下面这样一条简单的命令：</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN lang=EN-US style="COLOR: red">ls<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">那么所有的三个描述符都连接到终端。多数</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">提供了一种方式来重定向三个描述符到文件。例如：</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN lang=EN-US style="COLOR: red">ls &gt; file.list<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">执行</SPAN><SPAN lang=EN-US>ls</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">命令，并把它的标准输出重定向到名为</SPAN><SPAN lang=EN-US>file.list</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的文件。</SPAN></P>
<H3 style="MARGIN: 13pt 0cm"><FONT size=5><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">无缓冲</SPAN><SPAN lang=EN-US>I/O</SPAN></FONT></H3>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">无缓冲</SPAN><SPAN lang=EN-US>I/O</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">由</SPAN><SPAN lang=EN-US>open</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数，</SPAN><SPAN lang=EN-US>read</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数，</SPAN><SPAN lang=EN-US>write</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数，</SPAN><SPAN lang=EN-US>lseek</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数和</SPAN><SPAN lang=EN-US>close</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数提供。这些函数都需要文件描述符才能工作。</SPAN></P>
<H4 style="MARGIN: 14pt 0cm 14.5pt"><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial"><FONT size=5>例子</FONT></SPAN></H4>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">如果我们想要从标准输入读取，从标准输出写入，那么图</SPAN><SPAN lang=EN-US>1.4</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的程序就可以拷贝</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系统中的常规文件。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN lang=EN-US>apue.h</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中包含的</SPAN><SPAN lang=EN-US>&lt;unistd.h&gt;</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">头文件，以及两个常量</SPAN><SPAN lang=EN-US>STDIN_FILENO</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US>STDOUT_FILENO</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">都是</SPAN><SPAN lang=EN-US>POSIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">标准（下章中将会详细介绍）的一部分。在该头文件中有许多</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系统服务的函数的原型，例如我们调用的</SPAN><SPAN lang=EN-US>read</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US>write</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">常量</SPAN><SPAN lang=EN-US>STDIN_FILENO</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US>STDOUT_FILENO</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在</SPAN><SPAN lang=EN-US>&lt;unistd.h&gt;</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中定义，它们指定了标准输入和标准输出的文件描述符。通常，</SPAN><SPAN lang=EN-US>STDIN_FILENO</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US>STDOUT_FILENO</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的值分别为</SPAN><SPAN lang=EN-US>0</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US>1</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，然而，在便携设备开发中，我们使用新的名字来表示它们。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN lang=EN-US>3.9</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">节中会详细讨论</SPAN><SPAN lang=EN-US>BUFFSIZE</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">常量，将看到它的不同值是如何影响一个程序的效率的。尽管如此，无论</SPAN><SPAN lang=EN-US>BUFFSIZE</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">为何值，图</SPAN><SPAN lang=EN-US>1.4</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的程序都会拷贝常规文件。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN lang=EN-US>read</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数返回读取的字节数，这个值被用来作为要写入的字节数。当到达输入文件的末尾时，</SPAN><SPAN lang=EN-US>read</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数返回</SPAN><SPAN lang=EN-US>0</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，程序停止。如果读取时发生错误，</SPAN><SPAN lang=EN-US>read</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数返回</SPAN><SPAN lang=EN-US>-1</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。多数系统函数在发生错误时返回</SPAN><SPAN lang=EN-US>1</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">如果像下面这样按标准名字（</SPAN><SPAN lang=EN-US>a.out</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）编译我们的程序</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN lang=EN-US style="COLOR: red">./a.out &gt; data<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">标准输入就是终端，而标准输出被重定向到文件</SPAN><SPAN lang=EN-US style="COLOR: black">data</SPAN><SPAN style="COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，同时标准错误也是终端。如果这个输出文件不存在，那么</SPAN><SPAN lang=EN-US style="COLOR: black">shell</SPAN><SPAN style="COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">将默认创建它。除非输入终止符（通常是</SPAN><SPAN lang=EN-US style="COLOR: black">Control-D</SPAN><SPAN style="COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">），程序将拷贝输入到标准输出。</SPAN><SPAN lang=EN-US style="COLOR: black"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">如果我们运行</SPAN><SPAN lang=EN-US style="COLOR: black"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN lang=EN-US style="COLOR: red">./a.out &lt; infile &gt; outfile<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">那么文件</SPAN><SPAN lang=EN-US>finfile</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">将被拷贝到文件</SPAN><SPAN lang=EN-US>outfile</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。<BR><BR></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71" align=center><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: "><IMG alt=http://pigworld.blogbus.com/files/1136135921.jpg hspace=0 src="http://pigworld.blogbus.com/files/1136135921.jpg" align=baseline border=0></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><BR></P></SPAN>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-outline-level: 5"><SPAN lang=EN-US><?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><v:shapetype id=_x0000_t75 path="m@4@5l@4@11@9@11@9@5xe" stroked="f" filled="f" o:spt="75" o:preferrelative="t" coordsize="21600,21600"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></v:path><o:lock v:ext="edit" aspectratio="t"></o:lock></v:shapetype></SPAN></P>
<P class=MsoCaption style="MARGIN: 0cm 0cm 0pt" align=center><FONT size=2><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">图</SPAN><SPAN lang=EN-US><FONT face=Arial> 1.4 </FONT></SPAN><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">列出目录中的所有文件</SPAN></FONT></P>
<P class=MsoCaption style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US><o:p><FONT face=Arial size=2>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体">第三章中将详细描述无缓冲<SPAN lang=EN-US>I/O</SPAN>函数。<SPAN lang=EN-US><o:p></o:p></SPAN></SPAN></P>
<H3 style="MARGIN: 13pt 0cm"><FONT size=5><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">标准</SPAN><SPAN lang=EN-US>I/O</SPAN></FONT></H3>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">标准</SPAN><SPAN lang=EN-US>I/O</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数为无缓冲</SPAN><SPAN lang=EN-US>I/O</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数提供了缓冲接口。使用标准</SPAN><SPAN lang=EN-US>I/O</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">可以防止在选择合适缓冲大小的时候出现错误，例如图</SPAN><SPAN lang=EN-US>1.4</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的</SPAN><SPAN lang=EN-US>BUFFSIZE</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">常量。使用标准</SPAN><SPAN lang=EN-US>I/O</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数的另一个优点是其仅仅处理输入的行（</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">应用程序中的通常事件）。例如</SPAN><SPAN lang=EN-US>fgets</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数，它读取整个一行。另一方面，</SPAN><SPAN lang=EN-US>read</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数读取指定的字节数。就像将</SPAN><SPAN lang=EN-US>5.4</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">节中看到的那样，标准</SPAN><SPAN lang=EN-US>I/O</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">库提供的函数让我们能够控制缓冲的使用风格。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">最常见的标准</SPAN><SPAN lang=EN-US>I/O</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数就是</SPAN><SPAN lang=EN-US>printf</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。如果在程序中调用</SPAN><SPAN lang=EN-US>printf</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数，可以通过包含</SPAN><SPAN lang=EN-US>apue.h</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">来包含</SPAN><SPAN lang=EN-US>&lt;stdio.h&gt;</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">头文件，</SPAN><SPAN lang=EN-US>&lt;stdio.h&gt;</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">头文件中包含了所有标准</SPAN><SPAN lang=EN-US>I/O</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数的原型。</SPAN></P>
<H4 style="MARGIN: 14pt 0cm 14.5pt"><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial"><FONT size=5>例子</FONT></SPAN></H4>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">图</SPAN><SPAN lang=EN-US>1.5</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的程序会在</SPAN><SPAN lang=EN-US>5.8</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">节中详细讨论。该程序像前面的程序那样调用</SPAN><SPAN lang=EN-US>read</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">何</SPAN><SPAN lang=EN-US>write</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数。该程序拷贝标准输入到标准输出，同时也能够拷贝任何常规文件。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN lang=EN-US>getc</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数一次读取一个字符，该字符被</SPAN><SPAN lang=EN-US>putc</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数写入。在最后一个输入的字节被读取后，</SPAN><SPAN lang=EN-US>getc</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">返回常量</SPAN><SPAN lang=EN-US>EOF</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">（</SPAN><SPAN lang=EN-US>&lt;stdio.h&gt;</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中定义）。标准</SPAN><SPAN lang=EN-US>I/O</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">常量</SPAN><SPAN lang=EN-US>stdin</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US>stdout</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">也在</SPAN><SPAN lang=EN-US>&lt;stdio.h&gt;</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">头文件中定义，它们分别表示标准输入和标准输出。<BR></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71" align=center><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial"><IMG alt=http://pigworld.blogbus.com/files/1136135878.jpg hspace=0 src="http://pigworld.blogbus.com/files/1136135878.jpg" align=baseline border=0></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><BR></P></SPAN>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; mso-outline-level: 5"><SPAN lang=EN-US></SPAN></P>
<P class=MsoCaption style="MARGIN: 0cm 0cm 0pt" align=center><FONT size=2><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">图</SPAN><SPAN lang=EN-US><FONT face=Arial> 1.5 </FONT></SPAN><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">使用标准</SPAN><SPAN lang=EN-US><FONT face=Arial>I/O</FONT></SPAN><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">拷贝标注输入到标注输出</SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US><o:p>&nbsp;</o:p></SPAN></P><img src ="http://www.cppblog.com/Pigwen/aggbug/2408.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Pigwen/" target="_blank">PIGWORLD</a> 2006-01-03 22:04 <a href="http://www.cppblog.com/Pigwen/articles/2408.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>1.4 文件与目录</title><link>http://www.cppblog.com/Pigwen/articles/2407.html</link><dc:creator>PIGWORLD</dc:creator><author>PIGWORLD</author><pubDate>Tue, 03 Jan 2006 14:00:00 GMT</pubDate><guid>http://www.cppblog.com/Pigwen/articles/2407.html</guid><wfw:comment>http://www.cppblog.com/Pigwen/comments/2407.html</wfw:comment><comments>http://www.cppblog.com/Pigwen/articles/2407.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Pigwen/comments/commentRss/2407.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Pigwen/services/trackbacks/2407.html</trackback:ping><description><![CDATA[<DIV class=Section1 style="LAYOUT-GRID:  15.6pt none">
<H2><SPAN lang=EN-US>1.4 </SPAN><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">文件与目录</SPAN></H2>
<H3><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">文件系统</SPAN></H3>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">文件系统是按层次安排目录和文件的。起始目录被称为根（</SPAN><SPAN lang=EN-US>root</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">），它的名字是一个字符</SPAN><SPAN lang=EN-US> / </SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">目录是一个包含目录项的文件。在逻辑上，可以认为每个目录项都包含一个文件名，同时还包含说明该文件属性的信息。文件属性是：文件类型，文件长度，文件所有者，文件的许可权（例如，其他用户能否能访问该文件）</SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">文件最后的修改时间等。</SPAN><SPAN lang=EN-US>start</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN class=SpellE><SPAN lang=EN-US>fstat</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数返回一个包含所有文件属性的信息结构。第</SPAN><SPAN lang=EN-US>4</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">章将详细说明文件的各种属性。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">目录项事实上存储在磁盘上，对此区别于目录项的逻辑看法。多数</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">文件系统的实现并不在目录项中存储该目录项自己的属性，因为当文件有很多硬连接的时候，很难做到同步保存该文件的属性。当我们在第四章讨论硬连接后，对此将有清晰的认识。</SPAN></P>
<H3><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">文件名</SPAN></H3>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">目录中的各个名字被称为文件名。唯一两个不能出现在文件名中的字符是反斜杠（</SPAN><SPAN lang=EN-US>/</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）和空字符（</SPAN><SPAN lang=EN-US>null</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）。反斜杠用来把文件名从路径名中区别出来，空字符用来结束一个路径名。因此，把文件名中可用的字符限制在一般打印字符的子集中是一个好习惯。（我们限制可用字符还因为，如果我们在文件名中使用了</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的特殊字符，我们就必须使用</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的引号机制来引用文件名，这将导致问题复杂化。）</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">被反斜杆区分开，并且可选的以反斜杆开头一个或多个文件名的序列，构成了路径名。以反斜杆起头的路径名称为绝对路径；否则，称为相对路径。相对路径涉及到的文件相对于当前目录。文件系统的根（</SPAN><SPAN lang=EN-US>/</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）的名字是绝对路径的特例，它没有文件名。</SPAN></P>
<H4><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">例子</SPAN></H4>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">列出一个目录中所有文件的名字并不困难。图</SPAN><SPAN lang=EN-US>1.3</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">展示了</SPAN><SPAN class=SpellE><SPAN lang=EN-US>ls</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">（</SPAN><SPAN lang=EN-US>1</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）命令实现的本质。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN class=SpellE><SPAN lang=EN-US>ls</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">（</SPAN><SPAN lang=EN-US>1</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）符号是</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系统手册的一般表示方法，用来引用特定的项。它引用第一小节中的</SPAN><SPAN class=SpellE><SPAN lang=EN-US>ls</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">项。小节号通常用数字</SPAN><SPAN lang=EN-US>1</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">到</SPAN><SPAN lang=EN-US>8</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">表示，同时每一小节中的所有项都是按字母顺序排列的。本书中，我们假定你有一份你的</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系统手册的拷贝。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 6; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">历史上，</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系统把所有</SPAN><SPAN lang=EN-US>8</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">个小节集中在</SPAN><I style="mso-bidi-font-style: normal"><SPAN lang=EN-US>UNIX</SPAN></I><I style="mso-bidi-font-style: normal"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">程序员手册</SPAN></I><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中。随着页数的增加，趋势变为把各节安排在不同的手册中：例如一份为用户准备的，一份为程序员准备的，一份为系统管理员准备的。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 6; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">一些</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系统使用大写字母在已有的小节中进一步细分手册。举例来说，所有</SPAN><SPAN lang=EN-US>AT&amp;T</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的标准输出</SPAN><SPAN lang=EN-US>/</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">输入</SPAN><SPAN lang=EN-US>(I/O)</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数在</SPAN><SPAN lang=EN-US>3S</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">小节中，比如</SPAN><SPAN class=SpellE><SPAN lang=EN-US>fopen</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">（</SPAN><SPAN lang=EN-US>3S</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）。其它系统用字母来代替数字表示小节号，例如用</SPAN><SPAN lang=EN-US>C</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">表示命令。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">今天，许多手册用电子形式进行发行。如果你的手册是联机手册，查看手册中</SPAN><SPAN class=SpellE><SPAN lang=EN-US>ls</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">命令的方式或许是这样的：</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN class=GramE><SPAN lang=EN-US style="COLOR: red">man<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>1</SPAN></SPAN><SPAN lang=EN-US style="COLOR: red"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN class=SpellE>ls</SPAN><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P class=MsoNormal style="mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">或者</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN class=GramE><SPAN lang=EN-US style="COLOR: red">man<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>–</SPAN></SPAN><SPAN lang=EN-US style="COLOR: red">s1<SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN class=SpellE>ls</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN lang=EN-US><o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">图</SPAN><SPAN lang=EN-US>1.3</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是一个程序，其打印出目录中各个文件的名字。如果源码文件的名字是</SPAN><SPAN class=SpellE><SPAN lang=EN-US>myls.c</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，我们按照以下方式把它编译成默认的可执行文件</SPAN><SPAN class=SpellE><SPAN lang=EN-US>a.out</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">：</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN class=GramE><SPAN lang=EN-US>cc <SPAN style="mso-spacerun: yes">&nbsp;</SPAN><SPAN class=SpellE>myls.c</SPAN></SPAN></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN lang=EN-US>cc</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">（</SPAN><SPAN lang=EN-US>1</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）是早期的</SPAN><SPAN lang=EN-US>C</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">编译器。在支持</SPAN><SPAN lang=EN-US>GNU C</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">编译器的系统上，</SPAN><SPAN class=SpellE><SPAN lang=EN-US>gcc</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">（</SPAN><SPAN lang=EN-US>1</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）是</SPAN><SPAN lang=EN-US>C</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">编译器。这里，</SPAN><SPAN lang=EN-US>cc</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">通常与</SPAN><SPAN class=SpellE><SPAN lang=EN-US>gcc</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">相连接（译者注：原句是</SPAN><SPAN lang=EN-US>Here</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，</SPAN><SPAN lang=EN-US>cc is often linked to <SPAN class=SpellE>gcc</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。没翻译好，期待哪位大虾指教）。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">一些输出例子如下：</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN lang=EN-US><o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN class=GramE><SPAN lang=EN-US style="COLOR: red">$<B style="mso-bidi-font-weight: normal"> ./</B></SPAN></SPAN><SPAN class=SpellE><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="COLOR: red">a.out</SPAN></B></SPAN><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="COLOR: red"> /dev<o:p></o:p></SPAN></B></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN lang=EN-US style="COLOR: red">.<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN lang=EN-US style="COLOR: red">..<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN class=GramE><SPAN lang=EN-US style="COLOR: red">console</SPAN></SPAN><SPAN lang=EN-US style="COLOR: red"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN class=SpellE><SPAN class=GramE><SPAN lang=EN-US style="COLOR: red">tty</SPAN></SPAN></SPAN><SPAN lang=EN-US style="COLOR: red"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN class=SpellE><SPAN class=GramE><SPAN lang=EN-US style="COLOR: red">mem</SPAN></SPAN></SPAN><SPAN lang=EN-US style="COLOR: red"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN class=SpellE><SPAN class=GramE><SPAN lang=EN-US style="COLOR: red">kmem</SPAN></SPAN></SPAN><SPAN lang=EN-US style="COLOR: red"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN class=GramE><SPAN lang=EN-US style="COLOR: red">null</SPAN></SPAN><SPAN lang=EN-US style="COLOR: red"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN class=GramE><SPAN lang=EN-US style="COLOR: red">mouse</SPAN></SPAN><SPAN lang=EN-US style="COLOR: red"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN class=SpellE><SPAN class=GramE><SPAN lang=EN-US style="COLOR: red">stdin</SPAN></SPAN></SPAN><SPAN lang=EN-US style="COLOR: red"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN class=SpellE><SPAN class=GramE><SPAN lang=EN-US style="COLOR: red">stdout</SPAN></SPAN></SPAN><SPAN lang=EN-US style="COLOR: red"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN class=SpellE><SPAN class=GramE><SPAN lang=EN-US style="COLOR: red">stderr</SPAN></SPAN></SPAN><SPAN lang=EN-US style="COLOR: red"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN class=GramE><SPAN lang=EN-US style="COLOR: red">zero</SPAN></SPAN><SPAN lang=EN-US style="COLOR: red"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><I style="mso-bidi-font-style: normal"><SPAN style="COLOR: red; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">省略其它未显示的行</SPAN><SPAN lang=EN-US style="COLOR: red"><o:p></o:p></SPAN></I></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN class=SpellE><SPAN class=GramE><SPAN lang=EN-US style="COLOR: red">cdrom</SPAN></SPAN></SPAN><SPAN lang=EN-US style="COLOR: red"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN class=GramE><SPAN lang=EN-US style="COLOR: red">$ <B style="mso-bidi-font-weight: normal">./</B></SPAN></SPAN><SPAN class=SpellE><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="COLOR: red">a.out</SPAN></B></SPAN><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="COLOR: red"> /<SPAN class=SpellE>var/spool/cron</SPAN></SPAN></B></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN class=GramE><SPAN lang=EN-US style="COLOR: red">can’t</SPAN></SPAN><SPAN lang=EN-US style="COLOR: red"> open /<SPAN class=SpellE>var/spool/cron</SPAN>: Permission denied<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN class=GramE><SPAN lang=EN-US style="COLOR: red">$ <B style="mso-bidi-font-weight: normal">./</B></SPAN></SPAN><SPAN class=SpellE><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="COLOR: red">a.out</SPAN></B></SPAN><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="COLOR: red"> .dev/<SPAN class=SpellE>tty</SPAN></SPAN></B></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN class=GramE><SPAN lang=EN-US style="COLOR: red">can’t</SPAN></SPAN><SPAN lang=EN-US style="COLOR: red"> open /dev/<SPAN class=SpellE>tty</SPAN>: Not a directory<o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN lang=EN-US><o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">像上面那样，我们会以如下的方式展示运行的命令和该命令的输出：输入的字符以<B style="mso-bidi-font-weight: normal"><SPAN style="COLOR: red">红色加粗字体显示</SPAN></B>，而程序的输出以<SPAN style="COLOR: red">红色字体</SPAN>显示。如果对于输出需要加注释，我们会以<I style="mso-bidi-font-style: normal"><SPAN style="COLOR: red">红色斜体字体</SPAN></I>显示注释。输入行前面的美元符号是</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">打印出来的提示符。我们始终使用美元符号作为</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">提示符。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">注意文件名并没有按照字母顺序输出。而</SPAN><SPAN class=SpellE><SPAN lang=EN-US>ls</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">命令会在打印名字之前现对名字排序。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在这个</SPAN><SPAN lang=EN-US>20</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">行的程序中，许多细节应当被考虑：</SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 42.75pt; TEXT-INDENT: -21pt; mso-outline-level: 6; mso-list: l0 level1 lfo1; tab-stops: list 42.75pt"><SPAN lang=EN-US style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN style="mso-list: Ignore">l<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">首先，我们包含了一个我们自己编写的头文件：</SPAN><SPAN class=SpellE><SPAN lang=EN-US>apue.h</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。几乎本书中所有的程序都包含这个头文件。该头文件包含了一些标准系统头文件，并且定义了本书例子中使用的数值常量和函数原型。该头文件的清单在附录</SPAN><SPAN lang=EN-US>B</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中。</SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 42.75pt; TEXT-INDENT: -21pt; mso-outline-level: 6; mso-list: l0 level1 lfo1; tab-stops: list 42.75pt"><SPAN lang=EN-US style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN style="mso-list: Ignore">l<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN lang=EN-US>main</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数的声明使用了</SPAN><SPAN lang=EN-US>ISO C</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">标准的风格。（下章中我们会谈到更多</SPAN><SPAN lang=EN-US>ISO C</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">标准。）</SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 42.75pt; TEXT-INDENT: -21pt; mso-outline-level: 6; mso-list: l0 level1 lfo1; tab-stops: list 42.75pt"><SPAN lang=EN-US style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN style="mso-list: Ignore">l<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">我们从命令行得到参数</SPAN><SPAN class=SpellE><SPAN lang=EN-US>argv</SPAN></SPAN><SPAN lang=EN-US>[1]</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">作为对象目录的名字。在第七章，我们将看到</SPAN><SPAN lang=EN-US>main</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数是怎样被调用的，同时看到命令行参数和环境变量是如何传递给程序的。</SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 42.75pt; TEXT-INDENT: -21pt; mso-outline-level: 6; mso-list: l0 level1 lfo1; tab-stops: list 42.75pt"><SPAN lang=EN-US style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN style="mso-list: Ignore">l<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">由于不同</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系统的目录项格式不尽相同，我们使用</SPAN><SPAN class=SpellE><SPAN lang=EN-US>opendir</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数，</SPAN><SPAN class=SpellE><SPAN lang=EN-US>readdir</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数和</SPAN><SPAN class=SpellE><SPAN lang=EN-US>closedir</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数来操作目录。</SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 42.75pt; TEXT-INDENT: -21pt; mso-outline-level: 6; mso-list: l0 level1 lfo1; tab-stops: list 42.75pt"><SPAN lang=EN-US style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN style="mso-list: Ignore">l<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN class=SpellE><SPAN lang=EN-US>opendir</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数返回</SPAN><SPAN lang=EN-US>DIR</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">结构的指针，并传递该指针给</SPAN><SPAN class=SpellE><SPAN lang=EN-US>readdir</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数。不必关心</SPAN><SPAN lang=EN-US>DIR</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">结构的细节。接着在循环中调用</SPAN><SPAN class=SpellE><SPAN lang=EN-US>readdir</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数，用来读取每个目录项。</SPAN><SPAN class=SpellE><SPAN lang=EN-US>readdir</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数返回一个</SPAN><SPAN class=SpellE><SPAN lang=EN-US>dirent</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">结构的指针，否则，在无目录项可读时返回</SPAN><SPAN lang=EN-US>null</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">指针。我们只需要检查</SPAN><SPAN class=SpellE><SPAN lang=EN-US>dirent</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">结构中每个目录项的名字（</SPAN><SPAN class=SpellE><SPAN lang=EN-US>d_name</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）。使用这个名字，我们就可以调用</SPAN><SPAN lang=EN-US>stat</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数（</SPAN><SPAN lang=EN-US>4.2</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">节介绍）来确定文件的所有属性。</SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 42.75pt; TEXT-INDENT: -21pt; mso-outline-level: 6; mso-list: l0 level1 lfo1; tab-stops: list 42.75pt"><SPAN lang=EN-US style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN style="mso-list: Ignore">l<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">我们调用两个我们自己编写的函数来处理错误：</SPAN><SPAN class=SpellE><SPAN lang=EN-US>err_sys</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN class=SpellE><SPAN lang=EN-US>err_quit</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。从图</SPAN><SPAN lang=EN-US>1.3</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的输出中我们可以看到，</SPAN><SPAN class=SpellE><SPAN lang=EN-US>err_sys</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数打印出了丰富的信息来描述遇到的错误（“</SPAN><SPAN lang=EN-US>Permission denied</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">”和“</SPAN><SPAN lang=EN-US>Not a directory</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">”）。附录</SPAN><SPAN lang=EN-US>B</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中列出了这两个错误处理函数。在</SPAN><SPAN lang=EN-US>1.7</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">节中我们将更详细的谈到错误处理。</SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 42.75pt; TEXT-INDENT: -21pt; mso-outline-level: 6; mso-list: l0 level1 lfo1; tab-stops: list 42.75pt"><SPAN lang=EN-US style="FONT-FAMILY: Wingdings; mso-fareast-font-family: Wingdings; mso-bidi-font-family: Wingdings"><SPAN style="mso-list: Ignore">l<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">当程序完成时，以参数</SPAN><SPAN lang=EN-US>0</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">调用</SPAN><SPAN lang=EN-US>exit</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数。</SPAN><SPAN lang=EN-US>exit</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数结束一个程序。通常，参数</SPAN><SPAN lang=EN-US>0</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">意味者正常结束，而</SPAN><SPAN lang=EN-US>1</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">到</SPAN><SPAN lang=EN-US>255</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">之间的参数意味着发生了错误。</SPAN><SPAN lang=EN-US>8.5</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">节中，我们将会学习一个程序（比如</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">或者我们自己写的程序）如何获得另一个正在执行中的程序的</SPAN><SPAN lang=EN-US>exit</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">状态。</SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 21.75pt; mso-outline-level: 6"><SPAN lang=EN-US><o:p></P>
<P class=MsoNormal style="MARGIN-LEFT: 21.75pt; mso-outline-level: 6" align=center><IMG alt=http://pigworld.blogbus.com/files/1136135551.jpg hspace=0 src="http://pigworld.blogbus.com/files/1136135551.jpg" align=baseline border=0></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN-LEFT: 21.75pt; TEXT-ALIGN: center; mso-outline-level: 6" align=center><SPAN lang=EN-US><?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><v:shapetype id=_x0000_t75 coordsize="21600,21600" o:preferrelative="t" o:spt="75" filled="f" stroked="f" path=" m@4@5 l@4@11@9@11@9@5 xe"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0 "></v:f><v:f eqn="sum @0 1 0 "></v:f><v:f eqn="sum 0 0 @1 "></v:f><v:f eqn="prod @2 1 2 "></v:f><v:f eqn="prod @3 21600 pixelWidth "></v:f><v:f eqn="prod @3 21600 pixelHeight "></v:f><v:f eqn="sum @0 0 1 "></v:f><v:f eqn="prod @6 1 2 "></v:f><v:f eqn="prod @7 21600 pixelWidth "></v:f><v:f eqn="sum @8 21600 0 "></v:f><v:f eqn="prod @7 21600 pixelHeight "></v:f><v:f eqn="sum @10 21600 0 "></v:f></v:formulas><v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"></v:path><o:lock aspectratio="t" v:ext="edit"></o:lock></v:shapetype></SPAN></P>
<P class=MsoCaption style="TEXT-ALIGN: center" align=center><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">图</SPAN><SPAN lang=EN-US> 1.3 </SPAN><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">列举出一个目录中的所有文件</SPAN></P>
<P class=MsoCaption style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US><o:p></o:p></SPAN></P>
<H3><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">工作目录</SPAN></H3>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">每个进程都有一个工作目录，有时又称为当前工作目录。所有的相对路径都从该目录开始。一个进程能够用</SPAN><SPAN class=SpellE><SPAN lang=EN-US>chdir</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数改变它的工作目录。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">例如，相对路径</SPAN><SPAN lang=EN-US>doc/memo/<SPAN class=SpellE>joe</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">指出</SPAN><SPAN lang=EN-US>memo</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">目录中的文件或目录</SPAN><SPAN class=SpellE><SPAN lang=EN-US>joe</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，</SPAN><SPAN lang=EN-US>memo</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">目录又在</SPAN><SPAN lang=EN-US>doc</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">目录中，而</SPAN><SPAN lang=EN-US>doc</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">一定是工作目录下的一个目录。查看这个相对路径，我们知道</SPAN><SPAN lang=EN-US>doc</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US>memo</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">一定是目录，但是我们不能确定</SPAN><SPAN class=SpellE><SPAN lang=EN-US>joe</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是一个文件还是一个目录。路径</SPAN><SPAN lang=EN-US>/<SPAN class=SpellE>usr</SPAN>/lib/lint</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是一个绝对路径，它指出了</SPAN><SPAN lang=EN-US>lib</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">目录中的文件或目录</SPAN><SPAN lang=EN-US>lint</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，</SPAN><SPAN lang=EN-US>lib</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">目录在</SPAN><SPAN class=SpellE><SPAN lang=EN-US>usr</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">目录中，而</SPAN><SPAN class=SpellE><SPAN lang=EN-US>usr</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">目录又在根（</SPAN><SPAN lang=EN-US>root</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）目录中。</SPAN></P>
<H3><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">起始（</SPAN><SPAN lang=EN-US>Home</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）目录</SPAN></H3>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">当我们登录时，工作目录被设定为我们的起始目录。我们的起始目录是从密码文件（</SPAN><SPAN lang=EN-US>1.3</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">节介绍）中我们的登录<SPAN class=GramE>项得到</SPAN>的。</SPAN></P></DIV><img src ="http://www.cppblog.com/Pigwen/aggbug/2407.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Pigwen/" target="_blank">PIGWORLD</a> 2006-01-03 22:00 <a href="http://www.cppblog.com/Pigwen/articles/2407.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>1.3 登录</title><link>http://www.cppblog.com/Pigwen/articles/2406.html</link><dc:creator>PIGWORLD</dc:creator><author>PIGWORLD</author><pubDate>Tue, 03 Jan 2006 13:56:00 GMT</pubDate><guid>http://www.cppblog.com/Pigwen/articles/2406.html</guid><wfw:comment>http://www.cppblog.com/Pigwen/comments/2406.html</wfw:comment><comments>http://www.cppblog.com/Pigwen/articles/2406.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Pigwen/comments/commentRss/2406.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Pigwen/services/trackbacks/2406.html</trackback:ping><description><![CDATA[<DIV class=Section1 style="LAYOUT-GRID:  15.6pt none">
<H2><SPAN lang=EN-US>1.3 </SPAN><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">登录</SPAN></H2>
<H3><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">登录名</SPAN></H3>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">当我们登录</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系统时，我们输入我们的登录名，然后是口令。系统接着就在密码文件中扫描我们的登录名，密码文件通常位于</SPAN><SPAN lang=EN-US>/etc/<SPAN class=SpellE>passwd</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。如果我们在密码文件中查看我们的登录项，将会看到它是由</SPAN><SPAN lang=EN-US>7</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">个冒号分离的部分组成的：登录名，加密口令，用户的数字</SPAN><SPAN lang=EN-US>ID</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">（</SPAN><SPAN lang=EN-US>205</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">），组的数字</SPAN><SPAN lang=EN-US>ID</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">（</SPAN><SPAN lang=EN-US>105</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">），一个注释段，起始目录（</SPAN><SPAN lang=EN-US>/home/<SPAN class=SpellE>sar</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">），还有</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">（注：以后直接用</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">代表解释器）程序（</SPAN><SPAN lang=EN-US>/bin/<SPAN class=SpellE>ksh</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）。</SPAN></P>
<P class=MsoNormal style="mso-outline-level: 5"><SPAN lang=EN-US><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN class=GramE>sar:</SPAN>x:205:105:Stephen <SPAN class=SpellE>Rago:/home/sar:/bin/ksh</SPAN></SPAN></P>
<P class=MsoNormal style="mso-outline-level: 5"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">所有新的系统已经把加密口令移到另一个文件中了。在第六章，我们将查看这些文件和一些函数来访问他们。</SPAN></P>
<H3><SPAN lang=EN-US>Shell</SPAN></H3>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">一旦我们登录后，一些特定的系统信息就显示出来，接着我们就可以在</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中输入命令。（一些系统在你登录后，会启动窗口管理程序，但是一般来说，你会以在其中一个窗口中运行</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">来结束。）</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是一个命令行解释器，它读入用户的输入并且执行命令。用户通常从终端（一个交互式的</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">），或者某些时候从一个文件（被称为</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">教本）向</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">输入。通常使用的</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">总结在图</SPAN><SPAN lang=EN-US>1.2</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN lang=EN-US><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<TABLE class=MsoTableList5 style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BORDER-LEFT: medium none; WIDTH: 462.9pt; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse; mso-border-alt: solid black .75pt; mso-yfti-tbllook: 480; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-border-insideh: .75pt solid black" cellSpacing=0 cellPadding=0 width=617 border=1>
<TBODY>
<TR style="mso-yfti-irow: -1; mso-yfti-firstrow: yes">
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 92.3pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1.5pt solid; mso-border-top-alt: solid black .75pt; mso-border-left-alt: solid black .75pt; mso-border-bottom-alt: solid black 1.5pt" vAlign=top width=123>
<P class=MsoNormal style="TEXT-ALIGN: center; mso-yfti-cnfc: 5" align=center><B><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">名字</SPAN><SPAN lang=EN-US><o:p></o:p></SPAN></B></P></TD>
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 62.05pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1.5pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black 1.5pt" vAlign=top width=83>
<P class=MsoNormal style="TEXT-ALIGN: center; mso-yfti-cnfc: 1" align=center><B><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">路径</SPAN><SPAN lang=EN-US><o:p></o:p></SPAN></B></P></TD>
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 87.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1.5pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black 1.5pt" vAlign=top width=116>
<P class=MsoNormal style="TEXT-ALIGN: center; mso-yfti-cnfc: 1" align=center><B><SPAN lang=EN-US>FreeBSD <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:chsdate w:st="on" Year="1899" Month="12" Day="30" IsLunarDate="False" IsROCDate="False">5.2.1</st1:chsdate><o:p></o:p></SPAN></B></P></TD>
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 77.3pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1.5pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black 1.5pt" vAlign=top width=103>
<P class=MsoNormal style="TEXT-ALIGN: center; mso-yfti-cnfc: 1" align=center><B><SPAN lang=EN-US>Linux <st1:chsdate w:st="on" Year="1899" Month="12" Day="30" IsLunarDate="False" IsROCDate="False">2.4.22</st1:chsdate><o:p></o:p></SPAN></B></P></TD>
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 81pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1.5pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black 1.5pt" vAlign=top width=108>
<P class=MsoNormal style="TEXT-ALIGN: center; mso-yfti-cnfc: 1" align=center><B><SPAN lang=EN-US>Mac OS X 10.3<o:p></o:p></SPAN></B></P></TD>
<TD style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 63pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1.5pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black 1.5pt; mso-border-right-alt: solid black .75pt" vAlign=top width=84>
<P class=MsoNormal style="TEXT-ALIGN: center; mso-yfti-cnfc: 1" align=center><B><SPAN lang=EN-US>Solaris 9<o:p></o:p></SPAN></B></P></TD></TR>
<TR style="mso-yfti-irow: 0">
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 92.3pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-left-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt" vAlign=top width=123>
<P class=MsoNormal style="TEXT-ALIGN: center; mso-yfti-cnfc: 4" align=center><B><SPAN lang=EN-US>Bourne shell<o:p></o:p></SPAN></B></P></TD>
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 62.05pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt" vAlign=top width=83>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US>/bin/<SPAN class=SpellE>sh</SPAN></SPAN></P></TD>
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 87.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt" vAlign=top width=116>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US>*</SPAN></P></TD>
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 77.3pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt" vAlign=top width=103>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US>link to bash</SPAN></P></TD>
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 81pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt" vAlign=top width=108>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US>Link to bash</SPAN></P></TD>
<TD style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 63pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt; mso-border-right-alt: solid black .75pt" vAlign=top width=84>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US>*</SPAN></P></TD></TR>
<TR style="mso-yfti-irow: 1">
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 92.3pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-left-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt" vAlign=top width=123>
<P class=MsoNormal style="TEXT-ALIGN: center; mso-yfti-cnfc: 4" align=center><B><SPAN lang=EN-US>Bourne-again shell<o:p></o:p></SPAN></B></P></TD>
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 62.05pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt" vAlign=top width=83>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US>/bin/bash</SPAN></P></TD>
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 87.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt" vAlign=top width=116>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US>Optional</SPAN></P></TD>
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 77.3pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt" vAlign=top width=103>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US>*</SPAN></P></TD>
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 81pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt" vAlign=top width=108>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US>*</SPAN></P></TD>
<TD style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 63pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt; mso-border-right-alt: solid black .75pt" vAlign=top width=84>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US>*</SPAN></P></TD></TR>
<TR style="mso-yfti-irow: 2">
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 92.3pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-left-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt" vAlign=top width=123>
<P class=MsoNormal style="TEXT-ALIGN: center; mso-yfti-cnfc: 4" align=center><B><SPAN lang=EN-US>C shell<o:p></o:p></SPAN></B></P></TD>
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 62.05pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt" vAlign=top width=83>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US>/bin/<SPAN class=SpellE>csh</SPAN></SPAN></P></TD>
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 87.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt" vAlign=top width=116>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US>link to <SPAN class=SpellE>tcsh</SPAN></SPAN></P></TD>
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 77.3pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt" vAlign=top width=103>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US>Link to <SPAN class=SpellE>tcsh</SPAN></SPAN></P></TD>
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 81pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt" vAlign=top width=108>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US>link to <SPAN class=SpellE>tcsh</SPAN></SPAN></P></TD>
<TD style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 63pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt; mso-border-right-alt: solid black .75pt" vAlign=top width=84>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US>*</SPAN></P></TD></TR>
<TR style="mso-yfti-irow: 3">
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 92.3pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-left-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt" vAlign=top width=123>
<P class=MsoNormal style="TEXT-ALIGN: center; mso-yfti-cnfc: 4" align=center><SPAN class=SpellE><B><SPAN lang=EN-US>Korn</SPAN></B></SPAN><B><SPAN lang=EN-US> shell<o:p></o:p></SPAN></B></P></TD>
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 62.05pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt" vAlign=top width=83>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US>/bin/<SPAN class=SpellE>ksh</SPAN></SPAN></P></TD>
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 87.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt" vAlign=top width=116>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US><o:p>&nbsp;</o:p></SPAN></P></TD>
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 77.3pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt" vAlign=top width=103>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US><o:p>&nbsp;</o:p></SPAN></P></TD>
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 81pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt" vAlign=top width=108>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US><o:p>&nbsp;</o:p></SPAN></P></TD>
<TD style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 63pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt; mso-border-right-alt: solid black .75pt" vAlign=top width=84>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US>*</SPAN></P></TD></TR>
<TR style="mso-yfti-irow: 4; mso-yfti-lastrow: yes">
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 92.3pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-left-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt" vAlign=top width=123>
<P class=MsoNormal style="TEXT-ALIGN: center; mso-yfti-cnfc: 4" align=center><B><SPAN lang=EN-US>TENEX C shell<o:p></o:p></SPAN></B></P></TD>
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 62.05pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt" vAlign=top width=83>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US>/bin</SPAN></P></TD>
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 87.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt" vAlign=top width=116>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US>*</SPAN></P></TD>
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 77.3pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt" vAlign=top width=103>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US>*</SPAN></P></TD>
<TD style="BORDER-RIGHT: medium none; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 81pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt" vAlign=top width=108>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US>*</SPAN></P></TD>
<TD style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 63pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; mso-border-top-alt: solid black .75pt; mso-border-bottom-alt: solid black .75pt; mso-border-right-alt: solid black .75pt" vAlign=top width=84>
<P class=MsoNormal style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US>*</SPAN></P></TD></TR></TBODY></TABLE>
<P class=MsoCaption style="TEXT-ALIGN: center" align=center><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">图</SPAN><SPAN lang=EN-US> 1.2 UNIX</SPAN><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">系统常用</SPAN><SPAN lang=EN-US>shell</SPAN></P>
<P class=MsoNormal><SPAN lang=EN-US><o:p></o:p></SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系统从密码文件中登录项的最后一个字段中了解到应该执行哪一个</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN lang=EN-US>Bourne shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，由</SPAN><SPAN lang=EN-US>Steve Bourne</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在贝尔实验室开发，自</SPAN><SPAN lang=EN-US>Version 7</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">以来一直在使用，并且被目前几乎所有的</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系统所支持。</SPAN><SPAN lang=EN-US>The control-flow constructs of the Bourne shell are reminiscent of <SPAN class=SpellE>Algol</SPAN> 68</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN lang=EN-US>C shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是由</SPAN><SPAN lang=EN-US>Bill Joy</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在伯克利开发的，被所有的</SPAN><SPAN lang=EN-US>BSD</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">发行版支持。另外，</SPAN><SPAN lang=EN-US>C shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">也被</SPAN><SPAN lang=EN-US>AT&amp;T</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的</SPAN><SPAN lang=EN-US>System V/386 Release 3.2</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US>System V Release 4</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">（</SPAN><SPAN lang=EN-US>SVR4</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）所支持。（我们将在下一章中更详细介绍这两个版本</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系统的不同。）</SPAN><SPAN lang=EN-US>C shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">构建于第六版的</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，而不是</SPAN><SPAN lang=EN-US>Bourne shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。它的</SPAN><SPAN lang=EN-US>control flow</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">更像</SPAN><SPAN lang=EN-US>C</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">语言，它也支持一些其它的，</SPAN><SPAN lang=EN-US>Bourne shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">所不支持的特性：</SPAN><SPAN lang=EN-US>job control, a history mechanism, and command line editing</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN class=SpellE><SPAN lang=EN-US>Korn</SPAN></SPAN><SPAN lang=EN-US> shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">被认为是对</SPAN><SPAN lang=EN-US>Bourne shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的成功者，它最先被</SPAN><SPAN lang=EN-US>SVR4</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">所支持。</SPAN><SPAN class=SpellE><SPAN lang=EN-US>Korn</SPAN></SPAN><SPAN lang=EN-US> shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是由</SPAN><SPAN lang=EN-US>David <SPAN class=SpellE>Korn</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在贝尔实验室开发的，运行于大多数的</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系统上。在</SPAN><SPAN lang=EN-US>SVR4</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">之前，</SPAN><SPAN class=SpellE><SPAN lang=EN-US>Korn</SPAN></SPAN><SPAN lang=EN-US> shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是一个收费组件，所以它并没有其它两个</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">流行。它也和</SPAN><SPAN lang=EN-US>Bourne shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">兼容，同时包含了使得</SPAN><SPAN lang=EN-US>C shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">流行的那些特性：</SPAN><SPAN lang=EN-US>job control, command line editing, and so on</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN lang=EN-US>Bourne-again shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是由所有</SPAN><SPAN lang=EN-US>Linux</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系统所支持的</SPAN><SPAN lang=EN-US>GNU shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。它被设计为与</SPAN><SPAN lang=EN-US>POSIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">一致，同时仍然与</SPAN><SPAN lang=EN-US>Bourne shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">兼容。它同时支持</SPAN><SPAN lang=EN-US>C shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN class=SpellE><SPAN lang=EN-US>Korn</SPAN></SPAN><SPAN lang=EN-US> shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的特性。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN lang=EN-US>TENEX C shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是</SPAN><SPAN lang=EN-US>C shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的增强版本。它从</SPAN><SPAN lang=EN-US>TENEX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">操作系统（</SPAN><SPAN lang=EN-US>1972</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">年开发于</SPAN><SPAN lang=EN-US>Bolt <SPAN class=SpellE>Beranek</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US>Newman</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）借用了许多特性，比如命令补全。</SPAN><SPAN lang=EN-US>TENEX C shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">对于</SPAN><SPAN lang=EN-US>C shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">增加了许多特性，其通常被作为</SPAN><SPAN lang=EN-US>C shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的替代品。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 6; mso-char-indent-count: 1.71"><SPAN lang=EN-US>Linux</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">使用</SPAN><SPAN lang=EN-US>Bourne-again shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">作为默认</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。事实上，</SPAN><SPAN lang=EN-US>/bin/<SPAN class=SpellE>sh</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">就是</SPAN><SPAN lang=EN-US>/bin/bash</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的连接。</SPAN><SPAN lang=EN-US>FreeBSD</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US>Mac OS X</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的默认</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是</SPAN><SPAN lang=EN-US>TENEN C shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，由于</SPAN><SPAN lang=EN-US>C shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的程序设计语言被广泛的认为很难使用，所有他们使用</SPAN><SPAN lang=EN-US>Bourne shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">作为他们的系统（</SPAN><SPAN lang=EN-US>administrative</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">脚本。</SPAN><SPAN lang=EN-US>Solaris</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">有继承于</SPAN><SPAN lang=EN-US>BSD</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US>System V</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的，在图</SPAN><SPAN lang=EN-US>1.2</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中所列出的所有</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。大多数这些</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的免费部分能够在因特网上得到。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 6; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">通过本书，我们将展示互动的</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">例子来执行我们所开发的程序。这些例子使用</SPAN><SPAN lang=EN-US>Bourne shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，</SPAN><SPAN class=SpellE><SPAN lang=EN-US>Korn</SPAN></SPAN><SPAN lang=EN-US> shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</SPAN><SPAN lang=EN-US>Bourne-again shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的共同特性。</SPAN></P></DIV><img src ="http://www.cppblog.com/Pigwen/aggbug/2406.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Pigwen/" target="_blank">PIGWORLD</a> 2006-01-03 21:56 <a href="http://www.cppblog.com/Pigwen/articles/2406.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>1.2 UNIX结构</title><link>http://www.cppblog.com/Pigwen/articles/2363.html</link><dc:creator>PIGWORLD</dc:creator><author>PIGWORLD</author><pubDate>Mon, 02 Jan 2006 14:55:00 GMT</pubDate><guid>http://www.cppblog.com/Pigwen/articles/2363.html</guid><wfw:comment>http://www.cppblog.com/Pigwen/comments/2363.html</wfw:comment><comments>http://www.cppblog.com/Pigwen/articles/2363.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/Pigwen/comments/commentRss/2363.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Pigwen/services/trackbacks/2363.html</trackback:ping><description><![CDATA[<DIV class=Section1 style="LAYOUT-GRID:  15.6pt none">
<H2><SPAN lang=EN-US>1.2 UNIX</SPAN><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">结构</SPAN></H2>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">&nbsp;严格的来说，操作系统被定义为操控计算机硬件和为程序提供运行环境的软件。一般来说，我们<SPAN class=GramE>称改软件</SPAN>为内核（</SPAN><SPAN lang=EN-US>kernel</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">），因为其相对较小并居于运行环境的核心。图</SPAN><SPAN lang=EN-US>1.1</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">展示了</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系统的架构。</SPAN></P>
<H5 style="TEXT-ALIGN: center" align=center><SPAN lang=EN-US><?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><v:shapetype id=_x0000_t75 path=" m@4@5 l@4@11@9@11@9@5 xe" stroked="f" filled="f" o:spt="75" o:preferrelative="t" coordsize="21600,21600"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0 "></v:f><v:f eqn="sum @0 1 0 "></v:f><v:f eqn="sum 0 0 @1 "></v:f><v:f eqn="prod @2 1 2 "></v:f><v:f eqn="prod @3 21600 pixelWidth "></v:f><v:f eqn="prod @3 21600 pixelHeight "></v:f><v:f eqn="sum @0 0 1 "></v:f><v:f eqn="prod @6 1 2 "></v:f><v:f eqn="prod @7 21600 pixelWidth "></v:f><v:f eqn="sum @8 21600 0 "></v:f><v:f eqn="prod @7 21600 pixelHeight "></v:f><v:f eqn="sum @10 21600 0 "></v:f></v:formulas><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></v:path><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:lock v:ext="edit" aspectratio="t"></o:lock></v:shapetype></SPAN></H5>
<P class=MsoCaption style="TEXT-ALIGN: center" align=center><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: "><IMG alt=http://pigworld.blogbus.com/files/1136135093.jpg hspace=0 src="http://pigworld.blogbus.com/files/1136135093.jpg" align=baseline border=0></SPAN><BR>图</SPAN> <SPAN lang=EN-US><SPAN style="mso-no-proof: yes">1</SPAN></SPAN><SPAN lang=EN-US>.1 UNIX</SPAN><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">操作系统的结构</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">&nbsp;内核的接口是一个软件层，被称为系统调用【</SPAN><SPAN lang=EN-US>system calls</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">】（图</SPAN><SPAN lang=EN-US>1.1</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中阴影部分）。常用的函数库就被构建在系统调用接口之上，但是应用程序可以任意地调用他们二者。（我们将在</SPAN><SPAN lang=EN-US>1.11</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">节更多的介绍系统调用和常用函数库。）解释器（</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">）是一个特殊的应用程序，其用来为其它程序的运行提供接口。<BR></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">&nbsp;&nbsp; &nbsp;更一般来说，一个操作系统就是内核和所有其它使得计算机易用并有个性的软件的组合。这些其它的软件包括系统工具，应用程序，解释器（</SPAN><SPAN lang=EN-US>shell</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">），常用函数库，等等。<BR>&nbsp;</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">&nbsp;&nbsp; 举例来说，</SPAN><SPAN lang=EN-US>Linux</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">就是被</SPAN><SPAN lang=EN-US>GNU</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">操作系统使用的内核。一些人称此为</SPAN><SPAN lang=EN-US>GNU/Linux</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">操作系统，然而其通常的被简称为</SPAN><SPAN lang=EN-US>Linux</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。尽管这种称法严格来说并不正确，然而这种称法却更容易理解，赋予了词组操作系统双重含义。（另一个优点是这样更简洁。）</SPAN></P></DIV><img src ="http://www.cppblog.com/Pigwen/aggbug/2363.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Pigwen/" target="_blank">PIGWORLD</a> 2006-01-02 22:55 <a href="http://www.cppblog.com/Pigwen/articles/2363.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>1.1 简介</title><link>http://www.cppblog.com/Pigwen/articles/2362.html</link><dc:creator>PIGWORLD</dc:creator><author>PIGWORLD</author><pubDate>Mon, 02 Jan 2006 14:51:00 GMT</pubDate><guid>http://www.cppblog.com/Pigwen/articles/2362.html</guid><wfw:comment>http://www.cppblog.com/Pigwen/comments/2362.html</wfw:comment><comments>http://www.cppblog.com/Pigwen/articles/2362.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Pigwen/comments/commentRss/2362.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Pigwen/services/trackbacks/2362.html</trackback:ping><description><![CDATA[<DIV class=Section1 style="LAYOUT-GRID:  15.6pt none">
<H2><SPAN lang=EN-US>1.1 </SPAN><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">简介</SPAN></H2>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">所有操作系统为程序的运行提供服务。典型的服务包括执行一个新的程序，打开文件，阅读文件，申请内存，获得当前时间，等等。本文集中于描述不同版本</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">操作系统提供的服务。</SPAN></P>
<P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">以严格的步进方式描述</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系统，不对任何<SPAN class=GramE>未学习</SPAN>过的词汇进行超前引用几乎是不可能的（或许也是乏味的）。本章从一个程序员的角度对</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系统进行快速浏览。并对书中引用的一些术语和概念进行简要的说明并给出实例。在以后各章中，将对这些概念作更详细的说明。对于不熟悉</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的程序设计人员，本书简要介绍了</SPAN><SPAN lang=EN-US>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">提供的各种服务。</SPAN></P></DIV><img src ="http://www.cppblog.com/Pigwen/aggbug/2362.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Pigwen/" target="_blank">PIGWORLD</a> 2006-01-02 22:51 <a href="http://www.cppblog.com/Pigwen/articles/2362.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>