- 论坛徽章:
- 0
|
原帖由 fera 于 2008-8-28 15:31 发表
PID fork()
{
//...
将一个假系统级上下文层压到子进程的系统上下文栈,该上下文层包括能够让子进程识别自己的一些信息,并且当子进程获得处理器时从此处开始执行; // 就像进程被调度时保存的上下文。
// 一旦被调度,将从假系统级上下文层开始执行,父子进程将共享下面的代码。
if(IsParent())
{
procTable[aPid]->State = READY_TO_RUN; // 此时子进程才可能被调度。
return aPid;
}
else
{
初始化u area的与分时相关的一些字段;
return 0;
}
}
In Linux 0.99.15, sys_call.S, there's an instruction after a sys call:
movl %eax,EAX(%esp) # save the return value
And the sys_fork() returns the pid of newly created process. When the new process is scheduled, stack is restored and the pid is returned via eax.
That's why fork() returns twice: 1 in parent CONTEXT, indicating whether fork() succeeded or not; 1 in child CONTEXT, with pid returned.
if (IsParent()) mentioned above may just check eax on the stack to see if it's 0 or non-zero. |
|