(Recently, I have returned to the Linux kernel. Continue .)
The process in Linux is similar to the thread. One reason for this is that they all use the same data structure, that is, task_struct. The difference is that there is an independent user space, and one is a shared user space (if there is no user space, it is a kernel thread, no need ).
Linux User processes cannot be directly created because such APIs do not exist. It can only be copied from a process, and then switched to the program file to be run through the exec API.
There are three types of replication APIs: fork, clone, and vfork.
The three APIs actually call a kernel internal function do_fork, but the parameters are different.
Vfork is actually part of the fork process to simplify and improve efficiency. Fork differs from clone. Fork is the full replication of Process resources, including the PCB of the process, the system stack of the process, the user space of the process, and the device opened by the process. In clone, only the first two items are copied, and the last two items are shared with the parent process.
In the replication of the four resources, the user space is relatively large. If the replication is complete, the efficiency will be very low. In Linux, the "Copy at write time" technology is used. That is to say, when the fork is executed, it does not actually copy all pages of the user space, but just copies the page table. In this way, no matter the parent process or child process, when a user space write operation occurs, the "write copy" operation will be triggered, so that an available user space will be allocated separately to make it completely independent. This is a very effective way to improve efficiency.
For clone, these page tables are shared with the parent process, so they are truly shared. Therefore, the protection of shared data must be ensured by upper-layer applications.