Forkvsvfork-linux general technology-Linux programming and kernel information about Linux processes. For details, see the following section. After fork, the child process and the parent process will continue to execute the commands after the fork call. A child process is a copy of the parent process. It will obtain the data space, heap and stack copies of the parent process. These are copies and the Parent and Child processes do not share the memory. That is to say, modifying the variable with the same name in the parent process does not affect the value of the variable in the parent process. However, the Parent and Child processes share something, which is simply the body segment of the program. The body segment stores machine commands executed by the cpu, usually read-only.
Since we often follow exec after fork, to improve efficiency, many implementations do not completely copy data segments and stacks, but adopt write-time replication, it is a bit similar to some cache and Memory Data Synchronization Methods.
Another way to improve efficiency is to use vfork. vfork originated from 2.9BSD at the earliest. The difference between it and fork is that it does not completely copy the address space of the parent process to the child process, because the sub-process will immediately call exec. the child process from vfork runs in the space of the parent process. It exists for exec calling, so it does not need to copy these things, because it is useless to copy. If the sub-process modifies a variable, this will affect the parent process.
Another difference between vfork and fork is that vfork ensures that the sub-process runs first. Only after it calls exec or exit can the parent process be scheduled to run. The fork Parent and Child processes run in an indefinite order, which depends on the scheduling algorithm of the kernel.
Therefore, when fork is used, the program code is reused-I mean the program code is the machine instruction part executed by the cpu, which has nothing to do with how many processes are running, even a program with frequent execution requires only one copy in the memory, and it may be read-only during the execution period. Of course, if you execute, it's another thing.
In addition, the data space and heap and stack in the parent process may generate copies. The actual situation depends on whether you are using vfork or fork. fork to generate copies, while vfork shares this part of memory.
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