📄 00000002.htm
字号:
<HTML><HEAD> <TITLE>BBS水木清华站∶精华区</TITLE></HEAD><BODY><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER>注: <BR> 以下文章转载自bbs.ee.nthu programming 板精华区 <BR>><I> ========================================================================= < </I><BR> <BR>发信人: <A HREF="mailto:czliao@csie.nctu">czliao@csie.nctu</A> (snOOPy), 信区: programming <BR>标 题: 问一下UNIX programming 的基本问题 <BR>发信站: 交大资工 News Server (Mon Nov 6 00:15:30 1995) <BR>转信站: star!news.ee.nthu!news.csie.nctu!czliao <BR> <BR> <BR> <BR> 底下有几个问题想请问一下懂UNIX programming 的人 <BR> while (fgets(buf, MAXLINE, stdin) != NULL) { <BR> buf[strlen(buf) - 1] = 0; /* replace newline with null *. <BR> <BR> if ( (pid = fork()) < 0) <BR> err_sys("fork error"); <BR> <BR> else if (pid == 0) { /* child */ <BR> execlp(buf, buf, (char *) 0); <BR> err_ret("couldn't execute: %s", buf); <BR> exit(127); <BR> } <BR> <BR> /*这是Advanced Programming in UNIX Environment 中的一个 <BR> 程式,我有点搞不懂的是,这其中所指的parent 和 child, <BR> 是不是由 fork()所产生的 process 且其值等於0的都是 <BR> child process 其值大於0的都是parent process ? <BR> 另外照这个程式看来,似乎要等parent 或 child 其中一个 process <BR> 完全执行完,才会执行下一个 process ,这样一来,好像就没 <BR> 有多工的情形了*/ <BR> <BR> /* parent */ <BR> if ( (pid = waitpid(pid, &status, 0)) < 0) <BR> err_sys("waitpid error"); <BR> printf("%% "); <BR> } <BR> <BR>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <BR>发信人: <A HREF="mailto:letitbe.bbs@cis.nctu">letitbe.bbs@cis.nctu</A> (Let It Be), 信区: programming <BR>标 题: Re: 问一下UNIX programming 的基本问题 <BR>发信站: 交大资科_BBS (Mon Nov 6 01:44:35 1995) <BR>转信站: star!news.ee.nthu!news.cis.nctu!cis_nctu <BR> <BR>It's a basic problem for fork(2V), <BR>I think you should `man 2 fork` first, <BR>then you will see: <BR> <BR> fork() creates a new process. The new process (child pro- <BR> cess) is an exact copy of the calling process except for the <BR> following: <BR> .......[cut]..... <BR> <BR>Well, when child is running, his parent is also running, <BR>they just get a different pid = fork() value. So they <BR>will choose different way to continue running program... <BR> <BR>One will choose: if (pid == 0) , it's child <BR>the other will choose: if (pid > 0), it's parent <BR> <BR>They run in the same time! <BR>(in your case, parent just wait wait wait wait child return value.. <BR> it seems parent not work, but in fact, WAIT is also a job) <BR> <BR>Anyway, you should man fork to get some useful information first. <BR> <BR>PS: sorry, I can only read but can't write Chinese now... :~~ <BR> I can't find a good chinese keyin method for my CXterm <BR> <BR>-- <BR>><I>----------------------------☆☆☆☆☆----------------------------< </I><BR><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -