Functions to create a process

Source: Internet
Author: User

Clone (), fork (), vfork () are all system calls to Linux.

Processes typically consist of code snippets, data segments, and PCB process control blocks.

The sub-process created by fork replicates the resources of the father process, including the contents of the memory, the task_struct content, the old and new processes using the same code snippet, copying the data segment and the stack segment, where the replication uses the annotated copy_on_write technique, that is, once the subprocess starts to run, The address space for the old and new processes is separated, and the two run independently.

The advantage is that the child process executes independently of the parent process and has good concurrency.

The disadvantage is that communication between the two requires specialized communication mechanisms such as pipe, FIFO, and System V.

In fact, during the copy process, the child process replicates the task_struct of the parent process, the system stack space, and the page table, pointing to the same page before the child process runs. When a child process changes the variables of the parent process, a new copy of the page involved is created by means of copy_on_write. So fork efficiency is not low.

The Vfork function creates a child process that runs entirely on the address space of the parent process, and the child process modifies any data in the virtual address space as seen by the parent process. This is completely different from fork, where the fork process is a separate space. Another difference is that after the child process created by Vfork, the parent process is blocked until the child process executes exec () and exit ().

When the child process is created only to invoke exec () to execute another program, the child process does not have any references to the address space of the parent process. Therefore, the replication of the address space at this time is redundant, and vfork can reduce unnecessary overhead.

The system calls fork () and vfork () are parameterless, and clone () has parameters. Fork () is all replication, vfork () is shared memory, and clone () is the parent process resource can be selectively copied to the child process, and the data structure is not replicated through the replication of the pointer to let the child process share, specifically to replicate which resources to the child process, by the parameter list of the Clone_ Flags to decide. In addition, clone () returns the PID of the child process.

Lightweight processes are created by the clone () function. The Clone function is powerful and has many parameters, so the process created by him is more complex than the previous 2 methods. Clone allows you to selectively inherit the resources of the parent process, and you can choose to share a virtual space with the parent process like vfork, thus creating a thread, or you can not share it with the parent process, you can even choose to create the process and the parent process is no longer a parent-child relationship, but the Brotherhood

int Clone (int (*FN) (void *), void *child_stack, int flags, void *arg);here fn is a function pointer, we know the process of 4 elements, this is a pointer to the program, is called the "script", Child_stack is obviously for the sub-process allocation system stack space (in Linux, the system stack space is 2 pages, is 8K of memory, which in this memory, A value is placed on the low address, which is the value of the process Control block task_struct, and flags is the flag that describes the resources you need to inherit from the parent process, ARG is the parameter passed to the child process. The system calls the service routine sys_clone, Sys_fork, and sys_vfork all end up calling the Do_fork function to complete.

The parameters of the do_fork are similar to the parameters of the clone system call, but one more regs (the user-mode register saved by the kernel stack). In fact, all the other parameters are taken with regs.

    • Clone
      • Clone's API cloak, which presses FN, arg into the user stack, and then raises the system call. After returning to user mode, the next instruction is FN.
      • Sysclone:parent_tidptr, child_tidptr all passed into Do_fork's parameters.
      • Sysclone: Check if there is a new stack, if not, use the parent process stack (start address is REGS.ESP)
    • Fork, Vfork:
      • The service routine calls Do_fork directly, but the parameters are modified slightly
      • Clone_flags:
        • sys_fork:sigchld|0;
        • sys_vfork:sigchld| (Clone_vfork | clone_vm)
      • User stack: Both are the stacks of the parent process.
      • PARENT_TIDPTR, child_ctidptr are null.
The above has content references from http://www.51develop.net/forum.php?mod=viewthread&tid=8963 copy_process () Creates a process descriptor and all other data structures required by the child process execution. Its parameters are the same as the do_fork () parameter, plus the PID of the child process.

Functions to create a process

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.