why not fork's return value 0?
I know the child process making successful, then the fork return value is 0
but i tried if return value(pid) == 0
then printf
code. unfortunately not print.
fork(void)
{
int i, pid;
struct proc *np;
struct proc *curproc = myproc();
// Allocate process.
if((np = allocproc()) == 0){
return -1;
}
// Copy process state from proc.
if((np->pgdir = copyuvm(curproc->pgdir, curproc->sz)) == 0){
kfree(np->kstack);
np->kstack = 0;
np->state = UNUSED;
return -1;
}
np->sz = curproc->sz;
np->parent = curproc;
*np->tf = *curproc->tf;
// Clear %eax so that fork returns 0 in the child.
np->tf->eax = 0;
for(i = 0; i < NOFILE; i++)
if(curproc->ofile[i])
np->ofile[i] = filedup(curproc->ofile[i]);
np->cwd = idup(curproc->cwd);
safestrcpy(np->name, curproc->name, sizeof(curproc->name));
pid = np->pid;
acquire(&ptable.lock);
np->state = RUNNABLE;
release(&ptable.lock);
if(pid ==0)
cprintf("child process made%d",pid); // why not print zero ..
else
cprintf("pid value is %d",pid);
return pid;
}
child proecss making sucessful, that is fork()'s return value is 0 ! (i tested in other main code about fork() ) ex) roved google code(foo.c)
but not detected pid is 0 in fork(). where is fork()'s return value 0 when child process constructed?
System calls are executed by the process' context who called them. That means the process who called them will receive the system call function return value. The fork implementation has the same behavior as all other system calls but is a bit special because 2 processes supposly return from it although only the parent process actually called it.
The child process's stack is being built to simulate a system call was previously made, together with it's simulated return value stored in the trapframe's eax register (which used for holding the function return value).
When the child process is selected to run by the scheduler, it's first line of code that run will be the forkret function and trapret as the simulated stack was prepared by allocproc function.