分页: 1 / 1

[问]关于fork()调用

发表于 : 2009-03-25 19:35
chenwl
操作系统设计与实现,引言:
while(TRUE) {
read_command(command, paramenters);
if( fork() != 0 ){
/*Parent code.*/
waitpid(-1, &status, 0);
} else {
/*Child code*/
execve(command, paramenters, 0);
}

这个fork()函数……,到底谁调用了fork(),不是SHELL吗(SHELL相当于一个应用程序吧
)。
那fork()岂不是肯定返回一个正数了,因为SHELL是父进程。

还有看这句话
“fork() 的返回值对子进程为0,对父进程为一个正数,即子进程的标识号。”
fork()的返回值还有“对谁”的说法……
额……我不知道该怎么问下去了,望高手指点。

Re: [问]关于fork()调用

发表于 : 2009-03-25 19:44
dbzhang800
建议找本书unix或linux编程的书看看,或者google或baidu等稍微搜一下 fork

Re: [问]关于fork()调用

发表于 : 2009-03-25 22:59
BigSnake.NET
lz 没理解什么叫 fork

fork 就是分支, 就是一个变两个, 所以需要通过返回值判断谁是调用 fork 的人, 谁是被 fork 出来的.

Re: [问]关于fork()调用

发表于 : 2009-03-26 15:29
chenwl
BigSnake.NET 写了:lz 没理解什么叫 fork

fork 就是分支, 就是一个变两个, 所以需要通过返回值判断谁是调用 fork 的人, 谁是被 fork 出来的.
两个分支都执行到相同的代码段吗?

fork()返回前子进程已经创建好了,并且在等待fork()返回?

Re: [问]关于fork()调用

发表于 : 2009-03-29 20:21
anewbie
chenwl 写了:
BigSnake.NET 写了:lz 没理解什么叫 fork

fork 就是分支, 就是一个变两个, 所以需要通过返回值判断谁是调用 fork 的人, 谁是被 fork 出来的.
两个分支都执行到相同的代码段吗?

fork()返回前子进程已经创建好了,并且在等待fork()返回?
The fork() system call creates a new process by duplicating an existing one. The process that calls fork() is the parent, whereas the new process is the child. The parent resumes execution and the child starts execution at the same place, where the call returns.

Re: [问]关于fork()调用

发表于 : 2009-03-29 21:26
chenwl
anewbie 写了:
chenwl 写了:
BigSnake.NET 写了:lz 没理解什么叫 fork

fork 就是分支, 就是一个变两个, 所以需要通过返回值判断谁是调用 fork 的人, 谁是被 fork 出来的.
两个分支都执行到相同的代码段吗?

fork()返回前子进程已经创建好了,并且在等待fork()返回?
The fork() system call creates a new process by duplicating an existing one. The process that calls fork() is the parent, whereas the new process is the child. The parent resumes execution and the child starts execution at the same place, where the call returns.
:em11

明白了