Fork the reason for double return values

Source: Internet
Author: User
Tags call back

Turn from: http://blog.csdn.net/livingpark/article/details/4069049

"NOTE4"

First, it must be clear that the return value of the function is stored in the register EAX.

Second, when Fork returns, the new process returns 0 because the EAX is set to 0 when the task structure is initialized;

In fork, the handle process is added to a running queue, and the process scheduler is scheduled to run at the right time. That is, from this point on, the current process splits into two concurrent processes.

Regardless of which process is scheduled to run, the remaining code for the fork function continues to be executed, and the respective values are returned when the execution ends.

"NOTE5"

For fork, the parent-child process shares the same code space, so it feels as if it has returned two times, but for the parent process that calls fork, if the fork out of the child process is not scheduled, then the parent process returns from the fork system call, while parsing sys_fork knows that Fork returns the ID of the child process. Looking at the fork, the Copy_process function shows that the return address of the child process is ret_from_fork (and the parent process returns on the same code point), and the return value is set directly to 0. So when the subprocess is scheduled, it is returned from fork and the return value is 0.
Key Note Two: 1.fork returns the execution location of the parent process or child process. (The value of the current process eax is first done as the return value) 2. Two times the return PID is stored in the location. (in EAX)

Process call copy_process Get Lastpid value (put in eax, fork return after normal, Lastpid is returned in the parent process)
Sub-process Task State segment the eax of TSS is set to 0,
In FORK.C
p->tss.eax=0; (such as the fruit process to be performed requires process switching, and when a switch occurs, the EAX value in the Subprocess TSS is transferred into the EAX register, and the child process executes the EAX content as the return value first)
When the child process starts executing, COPY_PROCESS returns the value of EAX.
After fork (), two tasks are performed simultaneously, the parent process uses his TSS, the subprocess with its own TSS, and when switching, each uses the values in each eax.

So, "call back two times" is 2 different processes.

Look at this sentence: Pid=fork ()
When this sentence is executed, the current process enters fork (), at which point the fork () is used for system calls within an embedded assembly: int 0x80 (see the 133 line _syscall0 function of the kernel version 0.11 unistd.h file for specific code). This is where the kernel runs the sys_fork system call, based on the system call function number previously written to EAX. Then, Sys_fork first calls the C function find_empty_process to produce a new process, and then calls the C function copy_process to copy the contents of the parent process to the child process. However, the EAX value in the Subprocess TSS is assigned a value of 0 (which is why 0 is returned in the subprocess), and when the assignment completes, Copy_process returns the PID of the new process (the subprocess), which is saved to the EAX. This occurs when the child process has the same code space as the parent process. Program Pointer register the EIP points to the same next instruction address, and when Fork returns to its parent process normally, the fork () returns the subprocess number, because the value in EAX is the newly created subprocess number. Execute else (pid>0); When a process switches to run a subprocess, the running environment of the subprocess is first restored, the TSS task state segment of the child process is loaded, where the EAX value (copy_process 0) is also loaded into the EAX register, so Fork returns 0 execution if (pid==0) when the child process is running.

"NOTE5"

The key to understanding it is to understand stack switching and pressure stacks, stack!

About the return of a child process:
The child process replicates the stack content of the parent process, from high to low
Ss
Esp
EFlags
Cs
eip-– This is the next instruction for int 0x80, and it's where the child process starts ....
Ds
Es
Fs
EDX
Ecx
EBX
Gs
Esi
Edi
Ebp
EAX (0)

Because EAX = 0, the child process returns 0 to fork.

Note: The user stack for the new process is set to the user stack of its parent process (the last pop-up ss,esp). If the parent-child process shared the user stack in a copy_on_write way
(This is what happens under Linux), and the parent process has modified the stack (which is almost certainly true if the parent process first returns), then the system has created a copy of the user stack for the parent process, leaving the parent process with the original user stack for the child process. So the system stack for the new process has been emptied, the new process has returned to the user state and returned to the function fork.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.