Details returned by the fork () function

Source: Internet
Author: User
Details returned by the fork () function

For fork, the Parent and Child processes share the same section.CodeSpace, so it seems like there are two responses. In fact, for the parent process that calls fork, if the child process that comes out of fork is not scheduled, then, the parent process is returned from the fork system call, And sys_fork is analyzed to know that fork returns the ID of the sub-process. Let's look at the child process from the fork. The copy_process function shows that the return address of the child process is ret_from_fork (returned at the same code point as the parent process), and the return value is directly set to 0. Therefore, when the sub-process is scheduled, it also returns from fork, and the return value is 0.
Note: 1. The execution location of the parent process or child process after fork returns. (The eax value of the current process is used as the return value first) 2. The location where the returned PID is stored twice. (In eax)

The process calls copy_process to get the value of lastpid (put in eax, after fork returns normally, the value returned by the parent process is lastpid)
The eax of the sub-process task status segment TSS is set to 0,
Fork. c
P-> TSS. eax = 0; (if the sub-process needs to be executed, process switching is required. When a switchover occurs, the eax value in the TSS of the sub-process is transferred to the eax register, when a sub-process is executed, the eax content is first returned)
When the sub-process starts execution, copy_process returns the value of eax.
After fork (), two tasks are performed simultaneously. The parent process uses its TSS, the child process uses its own TSS, and each uses the eax value during the switchover.

Therefore, "one call and two responses" are two different processes!

Example:

Int main ()
{
Pid_t PID;
PID = fork ();

If (PID <0 ){
Fprintf (stderr, "fork failed ");
Exit (-1 );
}
Else
If (pid = 0 ){
Printf ("child process // n ");
}
Else
{
Printf ("parent process // n ");
}
Return 0;
}

ThisProgramWhy does the execution always show: child process
Parent Process
Instead of parent and child?

A: Check this sentence: pid = fork ()
when this sentence is executed, the current process enters fork () to run. At this time, fork () embedded Assembly will be used in the system call: int 0x80 (for specific code, see the kernel version 0.11 unistd. the 133 rows _ syscall0 function of the H file ). Then the sys_fork system call will be run in the kernel according to the system call function number previously written to eax. Then, sys_fork first calls the C function find_empty_process to generate a new process, and then calls the C function copy_process to copy the content of the parent process to the child process, however, the eax value in the TSS of the sub-process is assigned 0 (this is why the sub-process returns 0). After the value is assigned, copy_process returns a new process (the sub-process) the value is saved to eax. At this time, the child process is generated. At this time, the child process and the parent process have the same code space, and the EIP of the procedure pointer register points to the same next instruction address, after fork returns its parent process normally, fork () returns the child process number because the value in eax is the newly created child process number, execute else (pid> 0). When a process switches to run a sub-process, the running environment of the sub-process is restored, that is, the TSS task status segment of the sub-process is loaded, the eax value (set to 0 in copy_process) is also loaded into the eax register. Therefore, when the sub-process is running, fork returns 0 and runs if (pid = 0 ).
first, it is displayed that the child process should be related to the kernel mechanism. After fork is a new process, the process will be rescheduled. At this time, the child process will always run first (related to the process priority ?)

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.