Implementation of different return values of fork two times

Source: Internet
Author: User
Tags call back

"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. Which is to open from this moment

Begins, 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 piece of code space, so it feels as if it had returned two times, in fact, for a call to the fork

In the case of a parent process, if the fork is not scheduled, the parent process returns from the fork system call, parsing the Sys_fork

Know, fork returns the ID of the child process. Then look at the fork out of the child process, by the Copy_process function can be seen, the return of the child process

The address is ret_from_fork (returned with the parent process at the same code point), and the return value is set directly to 0. So when the child process gets scheduled,

Hou, also returned from fork, 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 returned 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,
F
In ORK.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 hosting

, the EAX content is first made as the return value when the child process executes

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 called using an embedded assembly for the system call: int

0x80 (see the 133-line _syscall0 function of kernel version 0.11 of the unistd.h file for specific code). Then enter the kernel according to the previous write

The EAX system call function number will run the Sys_fork system call. Then, the C function is first called in Sys_fork find_empty_process

Produces a new process, and then calls the C function copy_process to copy the contents of the parent process to the child process, but the EAX in the child process TSS

Value is assigned 0 (which is why 0 is returned in a subprocess), and when the assignment completes, Copy_process returns the new process (the Zijin

, this value 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, when Fork returns to its parent process normally, because the value in EAX is a new

The child process number that is created, so fork () returns the subprocess number, executes else (pid>0), and when a process is switched to run a subprocess, the first

The runtime environment in which the child process is restored is the TSS task state segment??? The subprocess is loaded, where the EAX value (copy_process 0) is also mounted

EAX registers, so when a subprocess runs, fork returns 0 execution if (pid==0).

"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 of int 0x80, and also the place where the child process begins to execute ....

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 is shared in a copy_on_write way

User stack

(This is what happens under Linux), and before that the parent process modifies the stack (which is almost certainly true if the parent process returns first), that

, the system has created a copy of the user stack for the parent process, and the original user stack for the parent process was left to the child process. So the system of the new process

The stack 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.