Linux 版 (精华区)
发信人: netiscpu (夜☆星光点点☆), 信区: Linux
标 题: ◇ Fork 观念□清 I
发信站: 紫 丁 香 (Sun Nov 8 17:49:20 1998), 转信
寄信人: guest.bbs@hgluo.hust.edu.cn
标 题: ◇ Fork 观念□清 I
发信站: 华南理工大学 BBS木棉站
日 期: Thu Feb 20 14:19:54 1997
注:
以下文章转载自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
--
>----------------------------☆☆☆☆☆----------------------------<
--
m;32m※ 转寄:.华南网木棉站 bbs.gznet.edu.cn.[FROM: mtlab.hit.edu.cn]
--
Enjoy Linux!
-----It's FREE!-----
※ 来源:.紫 丁 香 bbs.hit.edu.cn.[FROM: mtlab.hit.edu.cn]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:4.047毫秒