The previous article on Linux multi-process development explains the fork () creation process. This article describes the vfork () creation process in detail.
Vfork () and fork () are different:
1. vfork () is the same as fork (). It is called once and returned twice.
2. fork () creates a sub-process. The sub-process only completely copies the resources of the parent process. Such a sub-process is independent of the parent process and has good concurrency.
3. when vfork () creates a sub-process, the operating system does not completely copy the resources of the parent process. Resources created by vfork share the address space of the parent process, the child process runs completely in the address space of the parent process, and the child process modifies any data in the address space.
As seen by the parent process.
4. When vfork () creates a sub-process, which process runs first depends on the scheduling algorithm of the system. When vfork is a process, vfork guarantees
The child process runs first. After it calls exec or exit, the parent process can be scheduled to run.
Note: If the child process depends on a behavior of the parent process before exec or exit is called, a deadlock will occur.
The following code is used to explain:
# include <unistd.h># include <sys/types.h># include <stdio.h>int globVar = 5;int main(void){ pid_t pid; int var = 1, i; printf("fork is diffirent with vfork \n"); pid = fork(); /*pid = vfork()*/ switch(pid) { case 0: i = 3; while(i-- > 0) { printf("Child process is running \n"); globVar++; var++; sleep(1); } printf("Child's globVar = %d, var = %d\n",globVar, var); break; case -1: perror("Process creation failed \n"); exit(0); default: i = 5; while(i-- > 0) { printf("Parent process is runnig \n"); globVar++; var++; sleep(1); } printf("Parent's globVar = %d, var = %d\n", globVar, var); exit(0); } exit(0);}
The running result is as follows:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/16261413D-0.png "title =" vfork_1.png "/>
At this time, fork () is used to create a new process, analysis:
The child process inherits the global and local variables of the parent process. In the child process, the global variables globVar and local variables var increase by 3, which are 8 and 4, respectively, the parent process increases by 5 and the result is 10 and 6, which proves that the child process has its own address space. Both global and local variables do not affect the modifications made by child and parent processes.
Note that the pid = fork (); vfork () is used. The result is as follows:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1626145N8-1.png "title =" vfork_2.png "/>
Analysis:
After vfork () creates a child process, globVar and var increase by 8 in the parent process because the child process of vfork () shares the address space of the parent process, the sub-process variable modification is visible to the parent process.
After fork () creates a child process, the execution sequence of the Parent and Child processes is uncertain.
After vfork () creates a sub-process, the sub-process runs first, Because vfork () ensures that the sub-process runs first, the parent process is blocked before the child process calls exit or exec.
In the process, S is blocked. D is in non-disruptive status.
You can go to ps to see it.
Note: Be cautious when using vfork. It is best not to allow the child process to modify the global and local variables shared with the parent process.
This article from the "_ Liang_Happy_Life _ Dream" blog, please be sure to keep this source http://liam2199.blog.51cto.com/2879872/1231680