The Linux system provides a fork function to create child processes. The fork function, compared to the normal function, is a special place where the fork function is called once, but returns two times. Once returned in the parent process, another return value is in the child process. ====================================================function Prototypes:return Value:
- When the call fails, 1 is returned to the parent process, and the child process is not created.
- When the call succeeds, the process ID of the child process is returned in the parent process, and 0 is returned in the child process
====================================================child processes created using the Fork function replicate the address space of the parent process, including code snippets, data segments, heaps, and stacks, and the child process replicates the PCB of the parent process, but the process ID will certainly be different. This shows that the parent and child processes are independent of each other, each with their own address space (although the virtual address is the same, but the physical address of the map is certainly not the same). If a child process modifies a variable value, it does not affect the value of the variable in the parent process. consider that if the address space of the parent process is copied each time a child process is created, and usually after the fork, the EXEC function is used to execute the different programs, so the code snippet and the data segment of the child process are cleaned up again. In this way, efficiency is reduced. So today's fork will use a "copy on write" technique to improve this disadvantage. This technology is characterized by " read-time sharing, copy -on-write", when the child process wants to read the data, and the parent process to share the address space, when the child process to write data, only to replicate the parent process, and only the parent process of the modified "page". This reduces the time to replicate the parent process address space and improves efficiency. =======================================================when the fork function is called, the call flow in the kernel? when we call the fork function in the application, we first go through the interrupt instruction into the kernel state (the x86 schema uses the int instruction, the ARM architecture uses the SWI instruction), and then the system call number to find the Sys_fork function, the final sys_fork function calls The Do_fork function. When the fork function creates a child process, the child process and the parent process copy the page table entry, and the kernel sets the page table entry to read-only permission, and when the parent-child process attempts to modify the contents of the page, it then modifies the new page table entry for the child process, and the content of the page is copied from the parent process . The Vfork function copies the page table entries of the parent process, but the child process does not create a new page table entry unless the Exec family function is called.
Process Control (iii)---fork function