Vfork and Fork

Source: Internet
Author: User
First, process creation steps and introduction to creating functions
1. Create a new process using the fork () or vfork () function
2. Use the EXEC function family to modify the process created. The process created using fork () is a full copy of the current process. However, we created the process to allow the new process to execute the new program, so we need to modify the new process created by the EXEC function family, so that he has something different from the parent process that can be modified to execute the new program, of course, The modified subprocess contains the information to execute the program.
In Linux, fork () and vfork () are the two functions used to create processes, and their related information is as follows:
To create a process function:
pid_t
Fork (void)//successfully returns 0, failure returns-1
Fork () is used to create a new process that is a subprocess of the current process, and can control whether the process is in the parent process or in a child process through the fork () return quality 6来. If you are running in a parent process, the process number that the PID is a subprocess is returned, and if in a child process the PID returned is 0
pid_t
Vfork (void)//successfully returns 0, failure returns-1
The Vfork () function is similar to the fork () function and is used to create child processes. It is simply used to create a new process, and the parent-child process shares the virtual memory space. In the kernel, however, the implementation of Vfork () calls the fork () function, and the calling function is as follows:
Sys_vfork (struct Pt_regs *regs)
{
Return Do_fork (Clone_vfork | CLONE_VM | SIGCHLD, regs->gr[30], regs, 0, NULL, NULL);
}
In the above function, pid_t is an implied type and is actually a type of int. Implicit type only the physical representation of the data type is unknown or irrelevant
II. implementation process and differences
1, Linux is through the _cloen () system call to implement fork (), this call through a series of parameter flags to indicate the resources required by the parent-child process. Fork (), Vfork (), _cloen () library functions invoke Cloen () according to the parameter flags they need, and then Cloen () to invoke the Do_fork () function, the do_fork () function, which is the real function of creating the process. He finished most of the work of creating the process. He also invokes the copy_process () function. Then let the process start running.
2, Vfork () and fork () function the same, in addition to not copying a page table entry for the parent process, that is, the resource associated with the parent process is not replicated, the parent-child process will share the address space, and any actual changes to the virtual memory space by the child process are actually modifying the contents of the rough space within the parent process virtual. And its parent process is blocked until the child process exits or executes the EXEC () function family. This allows the parent-child process to share the address space, avoiding the fork in resource replication is consumed.
3, write-time copy mechanism: The Linux system uses the "Copy in Action" method, which is a method of delaying resource duplication, which does not replicate the related resources of the parent process at the time of creation, and the parent-child process disguises the replicated resources that have been implemented by accessing the same physical memory. This sharing is the system mode is read-only way, this is different from the vfork, when the child process of memory data in these operations, it will be the pilgrimage resource replication. It is because of this mechanism that vfork () seems to have had little effect.
Third, in the process window creation process copy_process () function completes the work//excerpt: Linux kernel design and implementation
1. Call Dup_task_struct () to create a kernel stack, thread_info structure, and task_struct for the heart process, which are the same values as the current process. At this point, the child process and the parent process's descriptor are exactly the same.
2. After checking the newly created subprocess, the number of processes used by the current user does not exceed the limit on the resources assigned to him.
3. Now, the subprocess is starting to differentiate itself from the parent process, and many members within the process descriptor are cleared 0 or set to the initial value. The member values of the process descriptor are not inherited, but primarily statistics, most of the data in the process descriptor is shared.
4. Next, the status of the child process is set to a terminal wait state to ensure that he is not operational
5, Copy_process () calls Copy_flags () to update the flags members of TASK_STRUCT. Indicates that the process is a PF_SUPER[RIV flag with super User privileges being cleared by 0. Indicates that the PE_FORKNOEXEC flag that the process has not called the EXEC () function is set.
6, call Get_pid () to obtain a valid PID for the new process
7, according to the parameters to Cloen () copy_process () copy or share open files, file system letters, signal processing functions, process address space and namespace.
8, let the parent and child processes divide the remaining time slices.
9. Finally, Copy_proccess () do the cleanup and return a pointer to the child process only

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.