Fork () function in linux

Source: Internet
Author: User


Fork () function in linux 1. the traditional fork () system calls directly copy all resources to the newly created process. in Linux, fork () uses the copy-on-write page. copy at write time is a technology that can delay or even avoid data copying. The kernel does not copy the whole process address space at this time, but shares the same copy with the child process. www.2cto.com only copies data when writing data, so that each process has its own copy. That is to say, resource replication is only performed when writing data. Before that, it is shared only in read-only mode. this technology allows the copy of pages in the address space to be postponed until the real-time writing occurs. when pages and books are not written (for example, fork () and then immediately call exec (), they do not need to be copied. 2. linux uses the system to call clone () to implement fork (). then, clone () calls do_fork (). appendix: Implementation of the fork () function in linux: Linux uses the clone () system to call the actual Fork (). This call uses a series of parameter flags to specify the resources that the child process needs to share. The fork (), vfork (), and _ clone () library functions call clone () according to their respective parameter flags (). then, clone () calls do_fork (). do_frok completes most of the work in creation, and its definition is in ker/frok. c file. This function calls the copy_process () function and starts the process to run. The copy_process () function is very interesting: 1. Call dup_task_struct () to create a new kernel stack for the new process, thread_info structure and task_struct. These values are the same as the values of the current process. In this case, the descriptor of the child process and the parent process is identical. 2. Check the newly created sub-process. The number of processes owned by the current user does not exceed the resource limit allocated to the sub-process. 3. Now, the child process starts to differentiate itself from the parent process. Many members in the process descriptor must be cleared by 0 or set as the initial value. The membership value of the process descriptor is not inherited, but mainly statistical information. Most data in the process descriptor is shared. 4. Next, the sub-process status is set to TASK_UNINTERRUPTIBLE to ensure that it will not be put into operation. 5. copy_process () calls copy_flags () to update the flags member of task_struct. The PF_SUPERPRIV flag indicating whether the process has superuser permissions is cleared. 0 indicates that the process has not yet called the PF_FORKNOEXEC flag of the exec () function. Www.2cto.com 6. Call get_pid () to obtain a valid PID for the new process. 7. Copy or share opened files, file system information, signal processing functions, process address spaces and namespaces according to the parameter flag passed to clone. In the second half, these resources will be shared by all the threads of the given process; otherwise, these resources are different for each process and therefore are copied here. 8. Let the parent process and child process divide the remaining time slices equally. 9. At last, copy_process () performs the scanning and returns a pointer to the child process. Return to the do_fork () function. If the copy_process () function returns success, the newly created sub-process is awakened and put into operation. The kernel intentionally selects the sub-process to be executed first. Because half of the sub-processes call the exec () function immediately, this avoids the additional overhead of copy during write. If the parent process is executed first, it may start to write data to the address space.

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.