Cow Technology:
In LinuxProgramIn, fork () will generate a child process that is exactly the same as the parent process, but the child process will later be called by the exec system. For efficiency considerations, in Linux, the "Copy at write time" technology is introduced, that is, only the process space is available.When the content of each segment changes, the content of the parent process will be copied to the child process..
The physical space of the sub-process does not exist.CodeHow can I get the command to execute the exec system call?
After forkBefore ExecThe two processes useFor the same physical space (memory zone), the Child Process Code segment, data segment, and stack all point to the physical space of the parent process.That is to say, the twoDifferent virtual spacesBut the correspondingThe physical space is the same. When the Parent and Child processes haveChange the behavior of the corresponding segmentAnd thenAllocate physical space for corresponding segments of sub-ProcessesIf it is not because of exec, the kernel willAllocate physical space to data and stack segments of sub-Processes(At this point, the two have their own process space, which does not affect each other), while the code segment continues to share the physical space of the parent process (the Code of the two is identical ). However, because of exec, the Code executed by the two is different,The child process code segment will also allocate separate physical space.
I can see another one on the Internet.DetailsThat is, after fork, the kernel will place the sub-process in front of the queue so that the sub-process can be executed first to avoid the execution of the parent process resulting in write-time replication, and then the sub-process executes the exec system call, meaningless replication results in a reduction in efficiency.
Cow details:
Now there is a parent process P1, which is a subject, so it has a soul and a body. Now, in its virtual address space (with corresponding Data Structure Representation), there are four parts: Body segment, data segment, heap, and stack, the kernel allocates physical blocks for these four parts. That is, text block, data block, heap block, and stack block. As for how to allocate resources, this is what the kernel does, which is not detailed here.
1. Now P1 uses the fork () function to create a sub-process P2 for the process,
Kernel:
(1) copy the text segment, data segment, heap, and stack of P1. Note that the content is the same.
(2) Allocate physical blocks for these four parts. For P2, refer to the text block> the physical block of the Text Segment of Pi,In fact, it is not to allocate a body block for P2.Let the P2 Text Segment point to the P1 text block, data segment-> P2's own data segment block (assign the corresponding block for it), heap-> P2's own heap block, stack-> P2 stack block. As shown in: the arrow from left to right indicates copying content.
2. Write-time replication technology:The kernel only creates a virtual space structure for the newly generated child process. They are used to copy the virtual structure of the parent process, but do not allocate physical memory for these segments. They share the physical space of the parent process, when the Parent and Child processes change the corresponding segments, allocate physical space for the corresponding segments of the Child processes.
3. vfork (): this practice is even more popular. The virtual address space structure of the kernel connection sub-process is not created, and the virtual space of the parent process is directly shared. Of course, in this way, the physical space of the parent process is shared.
Through the above analysis, I believe that everyone has a deep understanding of the process. How does it reflect itself in layers? If the process is a subject, then it has a soul and body, the system must create corresponding entities, soul entities and physical entities for its implementation. Both have corresponding data structures in the system, and physical entities represent their physical meanings. Lkd
The traditional fork () system calls directly copy all the resources to the newly created process. This implementation is too simple and inefficient because the data it copies may not be shared. Worse, if a new process intends to execute a new image immediately, all copies are discarded. In Linux, fork () uses the copy-on-write page. Copy at write time is a technology that can delay or even avoid copying data. At this time, the kernel does not copy the whole process address space, but shares the same copy with the child process. Data is copied only when data needs to be written, so that each process has its own copy. That is to say,Resource replication is only performed when data needs to be written.Before that, it is shared only in read-only mode. This technology delays the copy of pages in the address space until the actual writing occurs. InWhen pages are not written at all-for example, fork () Immediately calls exec ()-they do not need to be copied. The actual overhead of Fork () is to copy the page table of the parent process and create a unique process descriptor for the child process.Generally, an executable file is run immediately after a process is created, this optimization avoids copying a large amount of data that is not used at all (the address space usually contains dozens of megabytes of data ). Since UNIX emphasizes the fast execution of processes, this optimization is very important. Here, we will add one point:Linux
Cow and exec are not necessarily related
PS: in fact, cow technology is not only used in Linux processes, but other types of C ++ strings also support Cow Technology in some ide environments, for example:
StringStr1 ="Hello World";StringStr2 = str1;
Then run the Code:
Str1 [1] ='Q'; Str2 [1] ='W';
After the first two statements, str1The address for storing data in str2 is the same. After the content is modified, the address of str1 is changed, and the address of str2 is the original one, this is the application of Cow Technology in C ++, but vs2005 does not seem to support cow.