Re: how fork returns value

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Oct 1, 2008 at 8:42 PM, srimugunthan dhandapani
<muggy.mit@xxxxxxxxx> wrote:
> hi all,
> I want to understand how the fork call return 0 in child and 'pid of child'
> in the parent.
> Presently my (naive)understanding is that the %eax value is stored
> differently for the child and the parent.
> Both the child and the parent returns from fork to the same instruction
> address, but will have different return values according to %eax.
> Is my understanding correct?
> Can somebody point out where exaclty in the source this is taken care of?
> Thanks,
> Mugunthan
>

Good question.   Just my guess based on some casual analysis ("====>
are my comments"):

in kernel/fork.c: do_fork(), which is called by sys_fork() (from
arch/x86/kernel/process_32.c, after the system call API fork() is
called):

        p = copy_process(clone_flags, stack_start, regs, stack_size,
                         child_tidptr, NULL, trace);=====> here u can
see that that the entire parent image in memory is copy over to the
child (COW mechnism).
        /*
         * Do this prior waking up the new thread - the thread pointer
         * might get invalid after that point, if the thread exits quickly.
         */
        if (!IS_ERR(p) {
                struct completion vfork;

                nr = task_pid_vnr(p);====>deriving PID of the copied
process (ie, child process).

                if (clone_flags & CLONE_PARENT_SETTID)
                        put_user(nr, parent_tidptr);====>this is
copying the PID (which is nr) to the userspace memory of parent
process, to keep informed of the PID of child.

                if (clone_flags & CLONE_VFORK) {
                        p->vfork_done = &vfork;
                        init_completion(&vfork);
                }

                tracehook_report_clone(trace, regs, clone_flags, nr, p);

So the return value is never passed via "ret" in C or assembly
function.   But it involved writing to userspace memory while running
inside the kernel,

Am I correct?   Thanks.

-- 
Regards,
Peter Teoh

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux