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