??xml version="1.0" encoding="utf-8" standalone="yes"?>一本久久a久久精品综合夜夜,久久强奷乱码老熟女网站,久久久久免费精品国产http://m.shnenglu.com/Pigwen/articles/2522.htmlPIGWORLDPIGWORLDMon, 09 Jan 2006 14:58:00 GMThttp://m.shnenglu.com/Pigwen/articles/2522.htmlhttp://m.shnenglu.com/Pigwen/comments/2522.htmlhttp://m.shnenglu.com/Pigwen/articles/2522.html#Feedback1http://m.shnenglu.com/Pigwen/comments/commentRss/2522.htmlhttp://m.shnenglu.com/Pigwen/services/trackbacks/2522.html1.7 错误处理

?/SPAN>UNIXpȝ函数发生错误Ӟ通常q回一个负|q且整数errno被设|ؓ(f)一个可以给出额外信息的倹{例如,open函数或者返回一个非负的文g描述W(当一切正常时Q,或者生一个错误。一?/SPAN>open的错误能产生15个可能的errno|例如文g不存在,权限问题Q等{。一些函Cq回负|而是使用?fn)惯?gu)来表C错误。例如,多数函数q回一个对象的指针Q而返回一?/SPAN>nullQ空Q指针来表示一个错误?/SPAN>

头文?/SPAN><errno.h>定义?jin)标识?/SPAN>errno?/SPAN>errno的每个可能的帔R倹{这之中的每个常量值都以字W?/SPAN>E开头。在UNIXpȝ手册W二节的W一,名ؓ(f)intro(2)的页面中Q同样列Z(jin)q之中所有的错误帔R。例如,如果errno{于帔REACCESQ这显CZ(jin)一个权限错误,比如没有_的权限来打开所h的文件?/SPAN>

?/SPAN>Linux中,错误帔R被列丑֜手册errno(3)中?/SPAN>

POSIX?/SPAN>ISO C?/SPAN>errno扩展定义为可变的整型左倹{它既可以是一个包含了(jin)错误代码的整敎ͼ也可以是一个函敎ͼ该函数返回指向错误代码的指针。以前的定义?/SPAN>

 

extern int errno;

 

然而在一个支持线E的环境中,q程地址I间在多个线E中׃nQ同时每个线E都需?/SPAN>errno的本地拷贝来防止U程间互相媄(jing)响。例如,Linux通过以下定义来支持多U程讉KerrnoQ?/SPAN>

 

extern int *_ _errno_location(void);

#define errno (*_ _errno_location())

 

errno有两条规则。第一Q如果不发生错误Q?/SPAN>errno的值决不会(x)被程序清除。因此,只有在函数的q回DC错误发生时Q才需要检?/SPAN>errno的倹{第二,M函数都不?x)?/SPAN>errno的D|ؓ(f)0Q同时在<errno.h>中也没有定义M帔Rgؓ(f)0?/SPAN>

标准C中定义了(jin)两个函数来帮助打印错误消息?/SPAN>

 

#include <string.h>

char *strerror(int errnum);

q回|(x)指向消息字符串的指针

 

该函数把errno的典型?/SPAN>errnum映射C个错误消息字W串Qƈq回一个指向字W串的指针?/SPAN>

perror函数在标准错误生ƈq回一个错误消息,该消息基?/SPAN>errno的当前倹{?/SPAN>

 

#include <stdio.h>

void perror(const char *msg);

 

它输?/SPAN>msg指向的字W串Q接着是一个分号和一个空|然后是与errno值对应的错误消息Q最后是一个新行?/SPAN>

例子

?/SPAN>1.8展示?jin)这两个函数的应用?/SPAN>

如果该程序被~译为文?/SPAN>a.outQ我们将看到

 

$ ./a.out

EACCES: Permission denied

./a.out: No such file or directory

 

注意我们把程序名?/SPAN>argv[0]作ؓ(f)参数传递给perrorQ?/SPAN>argv[0]的值是./a.out。这?/SPAN>UNIXpȝ的一个标准惯例。通过q样做,如果E序是作为管道的一部分执行Q就像在

 

prog1 < inputfile | prog2 | prog3 > outputfile

 

我们p够分清是三个E序中是哪个产生?jin)错误消息?BR>

 1#include "apue.h"
 2#include <errno.h>
 3
 4int
 5main(int argc, char *argv[])
 6{
 7    fprintf(stderr, "EACCES: %s\n", strerror(EACCES));
 8    errno = ENOENT;
 9    perror(argv[0]);
10    exit(0);
11}

?/SPAN> 1.8 strerror?/SPAN>perror的示?BR>

本书中的所有的例子都用附?/SPAN>B中的错误函数Q来代替直接调用strerror或?/SPAN>perror。附录中的错误函C用了(jin)ISO C的可变参数列表,可以只用单个C语句来处理错误情c(din)?/SPAN>

错误恢复

<errno.h>中定义的错误可以被分Zc:(x)致命的和非致命的。一个致命错误是不能够恢复的。最好的办法是在用户的屏q上打印一条错误消息,或者在一个日志文件中写入错误消息Q接着在退出。另一斚wQ非致命错误在某些时候能够更得体的处理。多数非致命错误是自然的临时错误Q例如当pȝ的活动程序较?yu)时Q(pȝQ资源短~的错误可能׃?x)发生?/SPAN>

资源相关的非致命错误包括EAGAINQ?/SPAN>ENFILEQ?/SPAN>ENOBUFSQ?/SPAN>ENOLCKQ?/SPAN>ENOSPCQ?/SPAN>ENOSRQ?/SPAN>EWOULDBLOCKQ当ENOMEMQ?/SPAN>EBUSH表示一个共享资源正在被使用Ӟ它们也可以被作ؓ(f)非致命错误。某些时候,?/SPAN>EINTR中断?jin)一个缓慢的pȝ调用Ӟ它也可以被看作非致命错误Q详?/SPAN>10.5节)(j)?/SPAN>

资源相关的非致命错误的典型恢复动作就是gq一?x)儿再试。这个技巧也能应用在其它循环中。例如,如果错误表示|络q接没有工作Q那么程序可能会(x)延迟一?x)儿再重新徏立连接。一些程序用指数增长的法Q每ơ等待更长的旉?/SPAN>

最后,由应用程序开发者来军_哪些错误是可以恢复的。如果一个合理的{略能够被用来恢复错误,通过避免异常退出,我们可以增强我们程序的健壮性?/SPAN>



PIGWORLD 2006-01-09 22:58 发表评论
]]>
1.6 E序和进E?/title><link>http://m.shnenglu.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://m.shnenglu.com/Pigwen/articles/2459.html</guid><wfw:comment>http://m.shnenglu.com/Pigwen/comments/2459.html</wfw:comment><comments>http://m.shnenglu.com/Pigwen/articles/2459.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://m.shnenglu.com/Pigwen/comments/commentRss/2459.html</wfw:commentRss><trackback:ping>http://m.shnenglu.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'">E序和进E?/SPAN></H1> <H2 style="MARGIN: 13pt 0cm"><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">E序</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'">E序是存在于盘上目录中的可执行文g?/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'">函数中的L一个,都可以将一个程序读入内存中q由内核执行Q感觉这句没有翻译好Q原句是Q?/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'">Q。我们将?/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'">节中介绍q些函数?/SPAN></P> <H2 style="MARGIN: 13pt 0cm"><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">q程和进E?/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'">一个正在执行中的程序实例被UCؓ(f)q程Q?/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'">Q,该词语(q程Q几乎会(x)出现在本书中的每一c(din)一些操作系l用dQ?/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'">Q来U呼一个正在运行中的程序?/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'">pȝ保每一个进E都拥有一个唯一的数字标识符Q称E?/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'">。进E?/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'">L非负整数?/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'">中的E序打印出它的进E?/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'">q执行它Q我们会(x)看到</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'">函数来获得进E?/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> <SPAN style="COLOR: #000000">#include </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> <SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">3</SPAN> <SPAN style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #008080">4</SPAN> <SPAN style="COLOR: #000000">main(</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">)<BR></SPAN><SPAN style="COLOR: #008080">5</SPAN> <SPAN style="COLOR: #000000">{<BR></SPAN><SPAN style="COLOR: #008080">6</SPAN> <SPAN style="COLOR: #000000">    printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">hello world from process ID %d\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">, getpid());<BR></SPAN><SPAN style="COLOR: #008080">7</SPAN> <SPAN style="COLOR: #000000">    exit(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);<BR></SPAN><SPAN style="COLOR: #008080">8</SPAN> <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">打印q程</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">q程控制</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'">q程控制主要使用三个函数Q?/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'">Q?/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'">个变体,我们通常把它们统UCؓ(f)</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'">函数。)(j)</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'">使用一个简单的E序Q图</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'">Q来展示</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'">pȝ的进E控制特性,该程序从标准输入d命o(h)q且执行q些命o(h)。这是一个类?/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'">E序的本质(译得不好,原句是:(x)</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'">Q。在q个</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'">行的E序中有许多Ҏ(gu)值得思考?/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'">         </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'">Q一ơ从标准输入中读取一行。当输入文gl止W(通常?/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'">Q作Z行的W一个字W时Q?/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'">q回</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'">指针Q同时终止@环,接着l止q程。在</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'">章中Q我们描q所有特D的l端字符Q文件终止符Q退格字W,整行擦除字符{等Q,q且介绍怎样改变它们?/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'">         </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'">q回的每一行都l止于一个换行符和跟在换行符后面的一?/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'">字节Q我们用标准的</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'">来计字W串的长度,接着把换行符替换Z?/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'">l束的参敎ͼ而不是以换行W结束的参数?/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'">         </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'">函数来徏立一个进E,q个q程是一个调用者的拯。我们把调用者称为父q程Q把新徏立的q程UCؓ(f)子进E。那?/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'">函数q回子进E的非负q程</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'">l父q程Q同时返?/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'">l子q程。因?/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'">创徏?jin)一个新q程Q我们说</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'">被调用一ơ就q回两次Q一ơ返回给父进E,一ơ返回给子进E?/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'">         </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">在子q程中,调用</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'">来执行从标准输入d的命令。这把子进E替换ؓ(f)一个新的程序文件?/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'">函数的组合,是一些操作系l所谓的产生一个新q程。(译得不好,原句是:(x)</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'">Q在</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'">pȝ中,q两部分被分Z(jin)独立的函数。第八章中将?x)介l更多这斚w的内宏V?/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'">         </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">因ؓ(f)子进E调?/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'">来执行新的程序文Ӟ父进E就?x)等待直到子q程l束。这些工作由</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'">参数代表?jin)子q程的进E?/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'">Q该参数用来标识出需要等待的q程?/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'">函数也可以得到子q程的终止状态。在q个单程序中的的</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'">变量׃表了(jin)子进E的l止状态,在程序中我们没有使用q个|但是我们可以通过(g)查这个值来定子进E的l止状态?/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'">         </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">q个E序最Ҏ(gu)的限制在于我们不能像我们所执行的命令传递参数。例如,不能列D特定的目录。我们只可以在工作目录执?/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'">命o(h)。ؓ(f)?jin)能够传递参敎ͼ我们需要分析输入行Q按某种?fn)惯Q可能的情况是用空格或者制表符Q区别出参数Q接着把每一个参C为独立的参数传递给</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'">函数。尽如此,q个E序仍然?/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'">pȝq程控制函数q行?jin)有用说明?/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'">q行q个E序Q我们得C面的输出。注意我们的E序有一个不同的提示W,使用癑ֈP</SPAN><SPAN lang=EN-US>%</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">Q来区别?/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'">的提C符?/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">    </SPAN>:0<SPAN style="mso-spacerun: yes">        </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">    </SPAN>pts/0<SPAN style="mso-spacerun: yes">      </SPAN>Jul 26 22:54<SPAN style="mso-spacerun: yes">  </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">    </SPAN>pts/1<SPAN style="mso-spacerun: yes">      </SPAN>Jul 26 22:54<SPAN style="mso-spacerun: yes">  </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">    </SPAN>pts/2<SPAN style="mso-spacerun: yes">      </SPAN>Jul 26 22:54<SPAN style="mso-spacerun: yes">  </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">                           </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'">输入文gl止W?/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">                              </SPAN><I style="mso-bidi-font-style: normal"><SPAN style="mso-spacerun: yes"> </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'">提示W?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> </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 </SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">apue.h</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"><BR>#include </SPAN><SPAN style="COLOR: #000000"><</SPAN><SPAN style="COLOR: #000000">sys</SPAN><SPAN style="COLOR: #000000">/</SPAN><SPAN style="COLOR: #000000">wait.h</SPAN><SPAN style="COLOR: #000000">></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>    </SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">    buf[MAXLINE];   </SPAN><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000"> from apue.h </SPAN><SPAN style="COLOR: #008000">*/</SPAN><SPAN style="COLOR: #000000"><BR>    pid_t   pid;<BR>    </SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">     status;<BR><BR>    printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">%% </SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);  </SPAN><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000"> print prompt (printf requires %% to print %) </SPAN><SPAN style="COLOR: #008000">*/</SPAN><SPAN style="COLOR: #000000"><BR>    </SPAN><SPAN style="COLOR: #0000ff">while</SPAN><SPAN style="COLOR: #000000"> (fgets(buf, MAXLINE, stdin) </SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000"> NULL) {<BR>        </SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000"> (buf[strlen(buf) </SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">] </SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">\n</SPAN><SPAN style="COLOR: #000000">'</SPAN><SPAN style="COLOR: #000000">)<BR>            buf[strlen(buf) </SPAN><SPAN style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">] </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">; </SPAN><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000"> replace newline with null </SPAN><SPAN style="COLOR: #008000">*/</SPAN><SPAN style="COLOR: #000000"><BR><BR>        </SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000"> ((pid </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> fork()) </SPAN><SPAN style="COLOR: #000000"><</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">) {<BR>            err_sys(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">fork error</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR>        } </SPAN><SPAN style="COLOR: #0000ff">else</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000"> (pid </SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">) {      </SPAN><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000"> child </SPAN><SPAN style="COLOR: #008000">*/</SPAN><SPAN style="COLOR: #000000"><BR>            execlp(buf, buf, (</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);<BR>            err_ret(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">couldn't execute: %s</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">, buf);<BR>            exit(</SPAN><SPAN style="COLOR: #000000">127</SPAN><SPAN style="COLOR: #000000">);<BR>        }<BR><BR>        </SPAN><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000"> parent </SPAN><SPAN style="COLOR: #008000">*/</SPAN><SPAN style="COLOR: #000000"><BR>        </SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000"> ((pid </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> waitpid(pid, </SPAN><SPAN style="COLOR: #000000">&</SPAN><SPAN style="COLOR: #000000">status, </SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)) </SPAN><SPAN style="COLOR: #000000"><</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">)<BR>            err_sys(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">waitpid error</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR>        printf(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">%% </SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR>    }<BR>    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> </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'">W号</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'">用代表一个控制字W。控制字W是一U特D的的字W,可以通过同时按下控制键(在你的计机上通常?/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'">键)(j)和另一个键来生它?/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'">Q或者说</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'">Q是默认的文件终止符。在</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'">的时候会(x)看到更多的控制字W?/SPAN></P> <H2 style="MARGIN: 13pt 0cm"><SPAN style="FONT-FAMILY: 黑体; mso-ascii-font-family: Arial">U程和线E?/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'">通常Q一个进E只有一个线E(原句是:(x)</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'">不知道怎么译Q留l大虄译)(j)。当有多于一个的U程来控制不同部分时Q一些问题就变得很容易解冟뀂另外,多线E在多处理器pȝ上能够获得^衡性(又一句翻译得不爽Q?/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'">Q?/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'">一个进E中的所有线E共享同一个地址I间Q文件描q符Q栈和进E相关的属性。因够访问相同的内存Q线E必d步访问它们自q׃n数据Q以避免冲突?/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'">和进E一PU程qE?/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'">标识。尽如此,U程</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'">对于q程</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'">来说是本地的。也是_(d)一个进E中的线E?/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'">在另一个进E中是没有意义的。当我们在进E中操作U程的时候,使用U程</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'">来指出特定的U程?/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'">控制U程的函数和控制q程的函数是一L(fng)。在q程模型建立很久以后Q线E才被加入到</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'">pȝ中,然而,U程模型和进E模型之间有一些复杂的交互Q在</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'">章将?x)看到这些?/SPAN></P><img src ="http://m.shnenglu.com/Pigwen/aggbug/2459.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/Pigwen/" target="_blank">PIGWORLD</a> 2006-01-06 00:25 <a href="http://m.shnenglu.com/Pigwen/articles/2459.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>1.5 输入和输?/title><link>http://m.shnenglu.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://m.shnenglu.com/Pigwen/articles/2408.html</guid><wfw:comment>http://m.shnenglu.com/Pigwen/comments/2408.html</wfw:comment><comments>http://m.shnenglu.com/Pigwen/articles/2408.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/Pigwen/comments/commentRss/2408.html</wfw:commentRss><trackback:ping>http://m.shnenglu.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>文g描述W?/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'">文g描述W通常是小的且非负的整敎ͼ内核使用它来标识能被特定q程讉K的文件。只要打开或创Z(jin)新文Ӟ内核p回一个文件描q符Q我们用它来读写文件?/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>标准输入Q标准输出和标准错误</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'">通常Q一旦一个新的程序运行,所有的</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'">׃ؓ(f)其打开三个描述W:(x)标准输入Q标准输出和标准错误。如果不做什么特别的事,像下面q样一条简单的命o(h)Q?/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'">那么所有的三个描述W都q接到终端。多?/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'">提供?jin)一U方式来重定向三个描q符到文件。例如:(x)</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 > 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'">命o(h)Qƈ把它的标准输出重定向到名?/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'">函数Q?/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'">函数Q?/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'">函数Q?/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'">函数提供。这些函数都需要文件描q符才能工作?/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'">中的E序可以拷?/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'">pȝ中的常规文g?/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><unistd.h></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">头文Ӟ以及(qing)两个帔R</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'">标准Q下章中会(x)详细介绍Q的一部分。在该头文g中有许多</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'">pȝ服务的函数的原型Q例如我们调用的</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'">帔R</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><unistd.h></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中定义,它们指定?jin)标准输入和标准输出的文件描q符。通常Q?/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'">的值分别ؓ(f)</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'">Q然而,在便备开发中Q我们用新的名字来表示它们?/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'">节中?x)详l讨?/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'">帔RQ将看到它的不同值是如何影响一个程序的效率的。尽如此,无论</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'">Z|?/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'">中的E序都会(x)拯常规文g?/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'">函数q回d的字节数Q这个D用来作ؓ(f)要写入的字节数。当到达输入文g的末时Q?/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'">函数q回</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'">Q程序停止。如果读取时发生错误Q?/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'">函数q回</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'">。多数系l函数在发生错误时返?/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'">如果像下面这h标准名字Q?/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'">Q编译我们的E序</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 > 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'">标准输入是l端Q而标准输?gu)重定向到文g</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'">Q同时标准错误也是终端。如果这个输出文件不存在Q那?/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'">默认创建它。除非输入终止符Q通常?/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'">Q,E序拷贝输入到标准输出?/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'">如果我们q行</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 < infile > 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'">那么文g</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> </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: 宋体">W三章中详l描q无~冲<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'">函数提供?jin)缓冲接口。用标?/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'">帔R。用标?/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'">函数的另一个优Ҏ(gu)其仅仅处理输入的行(</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'">应用E序中的通常事gQ。例?/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'">函数Q它d整个一行。另一斚wQ?/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'">函数d指定的字节数。就像将</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'">节中看到的那P标准</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'">。如果在E序中调?/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'">函数Q可以通过包含</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><stdio.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><stdio.h></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">头文件中包含?jin)所有标?/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'">中的E序?x)?/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'">节中详细讨论。该E序像前面的E序那样调用</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'">函数。该E序拯标准输入到标准输出,同时也能够拷贝Q何常规文件?/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'">函数一ơ读取一个字W,该字W被</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'">函数写入。在最后一个输入的字节被读取后Q?/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'">q回帔R</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'">Q?/SPAN><SPAN lang=EN-US><stdio.h></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中定义)(j)。标?/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'">帔R</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><stdio.h></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">头文件中定义Q它们分别表C标准输入和标准输出?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> </o:p></SPAN></P><img src ="http://m.shnenglu.com/Pigwen/aggbug/2408.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/Pigwen/" target="_blank">PIGWORLD</a> 2006-01-03 22:04 <a href="http://m.shnenglu.com/Pigwen/articles/2408.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>1.4 文g与目?/title><link>http://m.shnenglu.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://m.shnenglu.com/Pigwen/articles/2407.html</guid><wfw:comment>http://m.shnenglu.com/Pigwen/comments/2407.html</wfw:comment><comments>http://m.shnenglu.com/Pigwen/articles/2407.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/Pigwen/comments/commentRss/2407.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/Pigwen/services/trackbacks/2407.html</trackback:ping><description><![CDATA[<div id="gkssu4i" 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">文g与目?/SPAN></H2> <H3><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">文gpȝ</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'">文gpȝ是按层次安排目录和文件的。v始目录被UCؓ(f)根(</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'">Q,它的名字是一个字W?/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'">目录是一个包含目录项的文件。在逻辑上,可以认ؓ(f)每个目录w包含一个文件名Q同时还包含说明该文件属性的信息。文件属性是Q文件类型,文g长度Q文件所有者,文g的许可权Q例如,其他用户能否能访问该文gQ?/SPAN><SPAN lang=EN-US>,</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">文g最后的修改旉{?/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 id="iyusg4q" 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'">函数q回一个包含所有文件属性的信息l构。第</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'">章将详细说明文g的各U属性?/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'">目录事实上存储在磁盘上Q对此区别于目录的逻辑看法。多?/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'">文gpȝ的实现ƈ不在目录中存储该目录项自己的属性,因ؓ(f)当文件有很多连接的时候,很难做到同步保存该文件的属性。当我们在第四章讨论连接后Q对此将有清晰的认识?/SPAN></P> <H3><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">文g?/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'">目录中的各个名字被称为文件名。唯一两个不能出现在文件名中的字符是反斜杠Q?/SPAN><SPAN lang=EN-US>/</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">Q和I字W(</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'">Q。反斜杠用来把文件名从\径名中区别出来,I字W用来结束一个\径名。因此,把文件名中可用的字符限制在一般打印字W的子集中是一个好?fn)惯。(我们限制可用字符q因为,如果我们在文件名中用了(jin)</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'">的特D字W,我们必M?/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'">的引h制来引用文g名,q将D问题复杂化。)(j)</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'">被反斜杆区分开Qƈ且可选的以反斜杆开头一个或多个文g名的序列Q构成了(jin)路径名。以反斜杆v头的路径名称为绝对\径;否则Q称为相对\径。相对\径涉?qing)到的文件相对于当前目录。文件系l的根(</SPAN><SPAN lang=EN-US>/</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">Q的名字是绝对\径的特例Q它没有文g名?/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'">列出一个目录中所有文件的名字q不困难。图</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 id="iemsceu" 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'">Q?/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'">Q命令实现的本质?/SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 17.95pt; mso-outline-level: 5; mso-char-indent-count: 1.71"><span id="iu8wqq6" 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'">Q?/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'">Q符h</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'">pȝ手册的一般表C方法,用来引用特定的项。它引用W一节中的</SPAN><span id="eiqw6ik" 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'">V小节号通常用数?/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'">表示Q同时每一节中的所有项都是按字母顺序排列的。本书中Q我们假定你有一份你?/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'">pȝ手册的拷贝?/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'">pȝ把所?/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'">E序员手?/SPAN></I><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中。随着|的增加,势变ؓ(f)把各节安排在不同的手册中Q例如一份ؓ(f)用户准备的,一份ؓ(f)E序员准备的Q一份ؓ(f)pȝ理员准备的?/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'">pȝ使用大写字母在已有的节中进一步细分手册。D例来_(d)所?/SPAN><SPAN lang=EN-US>AT&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 id="cmqi2s4" 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'">Q?/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'">Q。其它系l用字母来代替数字表C小节号Q例如用</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'">表示命o(h)?/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'">今天Q许多手册用?sh)子形式q行发行。如果你的手册是联机手册Q查看手册中</SPAN><span id="wmacu4u" 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'">命o(h)的方式或许是q样的:(x)</SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><span id="kaqg28m" class=GramE><SPAN lang=EN-US style="COLOR: red">man<SPAN style="mso-spacerun: yes">  </SPAN>1</SPAN></SPAN><SPAN lang=EN-US style="COLOR: red"><SPAN style="mso-spacerun: yes">  </SPAN><span id="ukqs2y2" 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 id="6kimeky" class=GramE><SPAN lang=EN-US style="COLOR: red">man<SPAN style="mso-spacerun: yes">  </SPAN>?/SPAN></SPAN><SPAN lang=EN-US style="COLOR: red">s1<SPAN style="mso-spacerun: yes">  </SPAN><span id="ma66cs8" 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 id="6qskoce" 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'">Q我们按照以下方式把它编译成默认的可执行文g</SPAN><span id="mae6mmy" 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'">Q?/SPAN></P> <P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><span id="owa28uy" class=GramE><SPAN lang=EN-US>cc <SPAN style="mso-spacerun: yes"> </SPAN><span id="46sw2wm" 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'">Q?/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'">Q是早期?/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'">~译器的pȝ上,</SPAN><span id="iwy4i84" 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'">Q?/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'">Q是</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 id="gaciam8" 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'">相连接(译者注Q原句是</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'">Q?/SPAN><SPAN lang=EN-US>cc is often linked to <span id="gu66msg" class=SpellE>gcc</SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。没译好,期待哪位大虾指教Q?/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'">一些输Z子如下:(x)</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 id="ces2q8w" class=GramE><SPAN lang=EN-US style="COLOR: red">$<B style="mso-bidi-font-weight: normal"> ./</B></SPAN></SPAN><span id="yyag4qq" 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 id="iceas8m" 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 id="meemwiw" class=SpellE><span id="wkasg6c" 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 id="icqis6c" class=SpellE><span id="ekysk26" 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 id="64s48ui" class=SpellE><span id="yya6mgu" 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 id="uu4sim6" 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 id="4gkeum6" 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 id="goqgy6y" class=SpellE><span id="quwa6gs" 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 id="accg6si" class=SpellE><span id="6quaese" 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 id="8a6sky4" class=SpellE><span id="gacc4oc" 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 id="6uqgm86" 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'">省略其它未显C的?/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 id="aaeagi4" class=SpellE><span id="q64w6e4" 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 id="i22g28o" class=GramE><SPAN lang=EN-US style="COLOR: red">$ <B style="mso-bidi-font-weight: normal">./</B></SPAN></SPAN><span id="ggumq6k" 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 id="66gocoq" class=SpellE>var/spool/cron</SPAN></SPAN></B></P> <P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><span id="maq8cqs" class=GramE><SPAN lang=EN-US style="COLOR: red">can’t</SPAN></SPAN><SPAN lang=EN-US style="COLOR: red"> open /<span id="oa686uq" 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 id="aq8yeii" class=GramE><SPAN lang=EN-US style="COLOR: red">$ <B style="mso-bidi-font-weight: normal">./</B></SPAN></SPAN><span id="gacuwis" 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 id="ayymoay" class=SpellE>tty</SPAN></SPAN></B></P> <P class=MsoNormal style="TEXT-INDENT: 21.75pt; mso-outline-level: 5"><span id="6iw4kku" class=GramE><SPAN lang=EN-US style="COLOR: red">can’t</SPAN></SPAN><SPAN lang=EN-US style="COLOR: red"> open /dev/<span id="wu6wqqo" 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'">像上面那P我们?x)以如下的方式展C行的命o(h)和该命o(h)的输出:(x)输入的字W以<B style="mso-bidi-font-weight: normal"><SPAN style="COLOR: red">U色加粗字体昄</SPAN></B>Q而程序的输出?SPAN style="COLOR: red">U色字体</SPAN>昄。如果对于输出需要加注释Q我们会(x)?I style="mso-bidi-font-style: normal"><SPAN style="COLOR: red">U色斜体字体</SPAN></I>昄注释。输入行前面的美元符h</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'">打印出来的提C符。我们始l用美元符号作?/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'">提示W?/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'">注意文g名ƈ没有按照字母序输出。?/SPAN><span id="gwkocyw" 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'">命o(h)?x)在打印名字之前现对名字排序?/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'">行的E序中,许多l节应当被考虑Q?/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'">         </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">首先Q我们包含了(jin)一个我们自q写的头文Ӟ(x)</SPAN><span id="is8c4kg" 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'">。几乎本书中所有的E序都包含这个头文g。该头文件包含了(jin)一些标准系l头文gQƈ且定义了(jin)本书例子中用的数值常量和函数原型。该头文件的清单在附?/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'">         </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'">函数的声明用了(jin)</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'">标准的风根{(下章中我们会(x)谈到更多</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'">标准。)(j)</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'">         </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">我们从命令行得到参数</SPAN><span id="emeg4y6" 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'">作ؓ(f)对象目录的名字。在W七章,我们看?/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'">函数是怎样被调用的Q同时看到命令行参数和环境变量是如何传递给E序的?/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'">         </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'">pȝ的目录项格式不尽相同Q我们?/SPAN><span id="oqsim6c" 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'">函数Q?/SPAN><span id="6e8okik" 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 id="k8cuyii" 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'">         </SPAN></SPAN></SPAN><span id="u8cuy4u" 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'">函数q回</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'">l构的指针,q传递该指针l?/SPAN><span id="6w64cqu" 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'">l构的细节。接着在@环中调用</SPAN><span id="y8wo84u" 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'">函数Q用来读取每个目录项?/SPAN><span id="ckocsqy" 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'">函数q回一?/SPAN><span id="m8aekww" 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'">l构的指针,否则Q在无目录项可读时返?/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 id="uquuyie" 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'">l构中每个目录项的名字(</SPAN><span id="mmg6wya" 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'">Q。用这个名字,我们可以调?/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'">函数Q?/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'">节介l)(j)来确定文件的所有属性?/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'">         </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">我们调用两个我们自己~写的函数来处理错误Q?/SPAN><span id="amqgw6q" 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 id="gcimcmi" 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'">的输Z我们可以看到Q?/SPAN><span id="8gmyoeg" 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'">函数打印Z(jin)丰富的信息来描述遇到的错误(?/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'">”)(j)。附?/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'">中列Z(jin)q两个错误处理函数。在</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'">         </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">当程序完成时Q以参数</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'">函数l束一个程序。通常Q参?/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'">之间的参数意味着发生?jin)错误?/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'">节中Q我们将?x)学习(fn)一个程序(比如</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'">或者我们自己写的程序)(j)如何获得另一个正在执行中的程序的</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">列DZ个目录中的所有文?/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'">每个q程都有一个工作目录,有时又称为当前工作目录。所有的相对路径都从该目录开始。一个进E能够用</SPAN><span id="qsi644a" 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'">例如Q相对\?/SPAN><SPAN lang=EN-US>doc/memo/<span id="iu8oqs6" 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'">目录中的文g或目?/SPAN><span id="kgmeuui" 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'">Q?/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'">一定是目录Q但是我们不能确?/SPAN><span id="k8kqu8q" 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 id="ymmqie6" 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'">是一个绝对\径,它指Z(jin)</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'">目录中的文g或目?/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'">Q?/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 id="m6gk6w6" 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 id="4qskm68" 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'">Q目录中?/SPAN></P> <H3><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">起始Q?/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'">Q目?/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'">当我们登录时Q工作目录被讑֮为我们的起始目录。我们的起始目录是从密码文gQ?/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'">节介l)(j)中我们的d<span id="cssiy8e" class=GramE>得?/SPAN>的?/SPAN></P></DIV><img src ="http://m.shnenglu.com/Pigwen/aggbug/2407.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/Pigwen/" target="_blank">PIGWORLD</a> 2006-01-03 22:00 <a href="http://m.shnenglu.com/Pigwen/articles/2407.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>1.3 dhttp://m.shnenglu.com/Pigwen/articles/2406.htmlPIGWORLDPIGWORLDTue, 03 Jan 2006 13:56:00 GMThttp://m.shnenglu.com/Pigwen/articles/2406.htmlhttp://m.shnenglu.com/Pigwen/comments/2406.htmlhttp://m.shnenglu.com/Pigwen/articles/2406.html#Feedback0http://m.shnenglu.com/Pigwen/comments/commentRss/2406.htmlhttp://m.shnenglu.com/Pigwen/services/trackbacks/2406.html

1.3 d

d?/SPAN>

当我们登?/SPAN>UNIXpȝӞ我们输入我们的登录名Q然后是口o(h)。系l接着在密码文g中扫描我们的d名,密码文g通常位于/etc/passwd。如果我们在密码文g中查看我们的d,会(x)看到它是?/SPAN>7个冒号分ȝ部分l成的:(x)d名,加密口o(h)Q用L(fng)数字IDQ?/SPAN>205Q,l的数字IDQ?/SPAN>105Q,一个注释段Qv始目录(/home/sarQ,q有shellQ注Q以后直接用shell代表解释器)(j)E序Q?/SPAN>/bin/kshQ?/SPAN>

    sar:x:205:105:Stephen Rago:/home/sar:/bin/ksh

所有新的系l已l把加密口o(h)Ud另一个文件中?jin)。在W六章,我们查看这些文件和一些函数来讉K他们?/SPAN>

Shell

一旦我们登录后Q一些特定的pȝ信息显C出来,接着我们可以在shell中输入命令。(一些系l在你登录后Q会(x)启动H口理E序Q但是一般来_(d)你会(x)以在其中一个窗口中q行shell来结束。)(j)shell是一个命令行解释器,它读入用L(fng)输入q且执行命o(h)。用户通常从终端(一个交互式?/SPAN>shellQ,或者某些时候从一个文Ӟ被称?/SPAN>shell教本Q向shell输入。通常使用?/SPAN>shellȝ在图1.2中?/SPAN>

名字

路径

FreeBSD 5.2.1

Linux 2.4.22

Mac OS X 10.3

Solaris 9

Bourne shell

/bin/sh

*

link to bash

Link to bash

*

Bourne-again shell

/bin/bash

Optional

*

*

*

C shell

/bin/csh

link to tcsh

Link to tcsh

link to tcsh

*

Korn shell

/bin/ksh

 

 

 

*

TENEX C shell

/bin

*

*

*

*

?/SPAN> 1.2 UNIXpȝ常用shell

pȝ从密码文件中d的最后一个字D中?jin)解到应该执行哪一?/SPAN>shell?/SPAN>

Bourne shellQ由Steve Bourne在贝?dng)实验室开发,?/SPAN>Version 7以来一直在使用Qƈ且被目前几乎所有的UNIXpȝ所支持?/SPAN>The control-flow constructs of the Bourne shell are reminiscent of Algol 68?/SPAN>

C shell是由Bill Joy在伯克利开发的Q被所有的BSD发行版支持。另外,C shell也被AT&T?/SPAN>System V/386 Release 3.2?/SPAN>System V Release 4Q?/SPAN>SVR4Q所支持。(我们在下一章中更详l介l这两个版本UNIXpȝ的不同。)(j)C shell构徏于第六版?/SPAN>shellQ而不?/SPAN>Bourne shell。它?/SPAN>control flow更像C语言Q它也支持一些其它的Q?/SPAN>Bourne shell所不支持的Ҏ(gu):(x)job control, a history mechanism, and command line editing?/SPAN>

Korn shell被认为是?/SPAN>Bourne shell的成功者,它最先被SVR4所支持?/SPAN>Korn shell是由David Korn在贝?dng)实验室开发的Q运行于大多数的UNIXpȝ上。在SVR4之前Q?/SPAN>Korn shell是一个收费组Ӟ所以它q没有其它两?/SPAN>shell行。它也和Bourne shell兼容Q同时包含了(jin)使得C shell行的那些特性:(x)job control, command line editing, and so on?/SPAN>

Bourne-again shell是由所?/SPAN>Linuxpȝ所支持?/SPAN>GNU shell。它被设计ؓ(f)?/SPAN>POSIX一_(d)同时仍然?/SPAN>Bourne shell兼容。它同时支持C shell?/SPAN>Korn shell的特性?/SPAN>

TENEX C shell?/SPAN>C shell的增强版本。它?/SPAN>TENEX操作pȝQ?/SPAN>1972q开发于Bolt Beranek?/SPAN>NewmanQ借用?jin)许多特性,比如命o(h)补全?/SPAN>TENEX C shell对于C shell增加?jin)许多特性,光常被作?/SPAN>C shell的替代品?/SPAN>

Linux使用Bourne-again shell作ؓ(f)默认shell。事实上Q?/SPAN>/bin/sh/bin/bash的连接?/SPAN>FreeBSD?/SPAN>Mac OS X的默?/SPAN>shell?/SPAN>TENEN C shellQ由?/SPAN>C shell的程序设计语a被广泛的认ؓ(f)很难使用Q所有他们?/SPAN>Bourne shell作ؓ(f)他们的系l(administrativeQ?/SPAN>shell脚本?/SPAN>Solaris有承于BSD?/SPAN>System V的,在图1.2中所列出的所?/SPAN>shell。大多数q些shell的免贚w分能够在因特|上得到?/SPAN>

通过本书Q我们将展示互动?/SPAN>shell例子来执行我们所开发的E序。这些例子?/SPAN>Bourne shellQ?/SPAN>Korn shell?/SPAN>Bourne-again shell的共同特性?/SPAN>



PIGWORLD 2006-01-03 21:56 发表评论
]]>
1.2 UNIXl构http://m.shnenglu.com/Pigwen/articles/2363.htmlPIGWORLDPIGWORLDMon, 02 Jan 2006 14:55:00 GMThttp://m.shnenglu.com/Pigwen/articles/2363.htmlhttp://m.shnenglu.com/Pigwen/comments/2363.htmlhttp://m.shnenglu.com/Pigwen/articles/2363.html#Feedback1http://m.shnenglu.com/Pigwen/comments/commentRss/2363.htmlhttp://m.shnenglu.com/Pigwen/services/trackbacks/2363.html

1.2 UNIXl构

 严格的来_(d)操作pȝ被定义ؓ(f)操控计算机硬件和为程序提供运行环境的软g。一般来_(d)我们U改软g为内核(kernelQ,因ؓ(f)其相对较?yu)ƈ居于q行环境的核?j)。图1.1展示?/SPAN>UNIXpȝ的架构?/SPAN>

http://pigworld.blogbus.com/files/1136135093.jpg
?/SPAN> 1.1 UNIX操作pȝ的结?/SPAN>

 内核的接口是一个Y件层Q被UCؓ(f)pȝ调用?/SPAN>system calls】(?/SPAN>1.1中阴影部分)(j)。常用的函数库就被构建在pȝ调用接口之上Q但是应用程序可以Q意地调用他们二者。(我们在1.11节更多的介绍pȝ调用和常用函数库。)(j)解释器(shellQ是一个特D的应用E序Q其用来为其它程序的q行提供接口?BR>    更一般来_(d)一个操作系l就是内核和所有其它得计机易用q有个性的软g的组合。这些其它的软g包括pȝ工具Q应用程序,解释器(shellQ,常用函数库,{等?BR>    举例来说Q?/SPAN>Linux是?/SPAN>GNU操作pȝ使用的内核。一些hU此?/SPAN>GNU/Linux操作pȝQ然而其通常的被UCؓ(f)Linux。尽这U称法严格来说ƈ不正,然而这U称法却更容易理解,赋予?jin)词l操作系l双重含义。(另一个优Ҏ(gu)q样更简z。)(j)



PIGWORLD 2006-01-02 22:55 发表评论
]]>
1.1 ?/title><link>http://m.shnenglu.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://m.shnenglu.com/Pigwen/articles/2362.html</guid><wfw:comment>http://m.shnenglu.com/Pigwen/comments/2362.html</wfw:comment><comments>http://m.shnenglu.com/Pigwen/articles/2362.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/Pigwen/comments/commentRss/2362.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/Pigwen/services/trackbacks/2362.html</trackback:ping><description><![CDATA[<div id="gauyyis" 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'">所有操作系lؓ(f)E序的运行提供服务。典型的服务包括执行一个新的程序,打开文gQ阅LӞ甌内存Q获得当前时_(d){等。本文集中于描述不同版本</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'">操作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>UNIX</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">pȝQ不对Q?SPAN class=GramE>未学?/SPAN>q的词汇q行前引用几乎是不可能的(或许也是乏味的)(j)。本章从一个程序员的角度对</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'">pȝq行快速浏览。ƈ对书中引用的一些术语和概念q行要的说明q给出实例。在以后各章中,对q些概念作更详细的说明。对于不熟?zhn)?/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'">的程序设计h员,本书要介l了(jin)</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'">提供的各U服务?/SPAN></P></DIV><img src ="http://m.shnenglu.com/Pigwen/aggbug/2362.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/Pigwen/" target="_blank">PIGWORLD</a> 2006-01-02 22:51 <a href="http://m.shnenglu.com/Pigwen/articles/2362.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <p>лǵվܻԴȤ</p> <a href="http://m.shnenglu.com/" title="精品视频久久久久">精品视频久久久久</a> <div class="friend-links"> </div> </div> </footer> <a href="http://www.moisky.com.cn" target="_blank">þŷƷ</a>| <a href="http://www.fhigmn.cn" target="_blank">þ91Ʒþ91ۺ</a>| <a href="http://www.ireboot.cn" target="_blank">97þþþ</a>| <a href="http://www.jhyjpj.cn" target="_blank">ۿƷþ</a>| <a href="http://www.popotang.cn" target="_blank">ŷþþþ9999</a>| <a href="http://www.swwpb.cn" target="_blank">ŷһþþ</a>| <a href="http://www.moonlong.cn" target="_blank">ĻþþƷ</a>| <a href="http://www.club-biz.cn" target="_blank">Ըߺþþþþþþ</a>| <a href="http://www.9yyg.cn" target="_blank">ۺþþ</a>| <a href="http://www.yichengsh.com.cn" target="_blank">Ʒþþþa</a>| <a href="http://www.52wysq.cn" target="_blank">þþþ޾Ʒվ</a>| <a href="http://www.hellorolex.cn" target="_blank">޹Ʒþһ</a>| <a href="http://www.vzas.cn" target="_blank">97Ʒ˾þô߽app</a>| <a href="http://www.jdongshop.cn" target="_blank">һþþƷ</a>| <a href="http://www.88354.com.cn" target="_blank">ھƷ˾þþþӰԺ԰</a>| <a href="http://www.gg32.cn" target="_blank">ާѡþþþƷ</a>| <a href="http://www.1985328.cn" target="_blank">޹Ʒþһ</a>| <a href="http://www.t421.cn" target="_blank">ŷһþ</a>| <a href="http://www.modellesson.cn" target="_blank">þþþùɫAVѹۿ</a>| <a href="http://www.ag202.cn" target="_blank">뾫Ʒþþɫ</a>| <a href="http://www.lingxiu98.com.cn" target="_blank">ھƷþþĻ</a>| <a href="http://www.a1dk.cn" target="_blank">99þþù</a>| <a href="http://www.pochai.cn" target="_blank">պþþþĻ </a>| <a href="http://www.qj119.cn" target="_blank">þþƷһ</a>| <a href="http://www.ctihf.com.cn" target="_blank">ѾþþƷѾѾ</a>| <a href="http://www.hotsw.com.cn" target="_blank">˾þþƷһ </a>| <a href="http://www.21gou.cn" target="_blank">þþһ</a>| <a href="http://www.oq21.cn" target="_blank">99þþƷһ </a>| <a href="http://www.wzyuan25.cn" target="_blank">ҰAVþһ</a>| <a href="http://www.wcjty.cn" target="_blank">ŷþþƷһcƬƬ</a>| <a href="http://www.floodlighting.cn" target="_blank">ȾþùþƷ</a>| <a href="http://www.oulihong.cn" target="_blank">þþһ</a>| <a href="http://www.pbkf.net.cn" target="_blank">þþþþþۺձ</a>| <a href="http://www.pynov.cn" target="_blank">ξþ99ƷþþþþС˵ </a>| <a href="http://www.qancen.cn" target="_blank">þþ㽶ۺϼձ</a>| <a href="http://www.todaytrends.cn" target="_blank">þ91Ʒ91þС</a>| <a href="http://www.shangxinju.cn" target="_blank">þþù99þùһ</a>| <a href="http://www.theboy.com.cn" target="_blank">þþƷƷ޾Ʒ</a>| <a href="http://www.6r9q.cn" target="_blank">þëƬһ</a>| <a href="http://www.ramar.com.cn" target="_blank">Ʒþþþþ</a>| <a href="http://www.0208958.cn" target="_blank">պƷþþվ</a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>