Linux programming-vfork () function details, vfork function details
Required header file:
# Include <sys/types. h>
# Include <unistd. h>
Pid_t vfork (void );
Function:
The vfork () function and the fork () function (fork (), please click this link) are the same as creating a new process in an existing process, however, the sub-processes they create are different.
Parameters:
None
Return Value:
Success: The child process returns 0, and the parent process returns the child process ID. Pid_t, which is an unsigned integer.
Failed:-1 is returned.
Fork () and vfock () Both create a process. What is the difference between them?
1) fork (): The execution sequence of parent and child processes is uncertain.
Vfork (): ensure that the sub-process runs first. After it calls exec (process replacement) or exit (exit process), the parent process can be scheduled to run.
2) fork (): The child process copies the address space of the parent process. The child process is a replica of the parent process.
Vfork (): The child process shares the address space of the parent process (precisely, the data is shared with the parent process before the exec (process replacement) or exit (exit process) is called)
Let's write an example to test it. After the sub-process created through vfork () is executed, it will be executed by the parent process:
# Include <stdio. h> # include <stdlib. h> # include <unistd. h> int main (int argc, char * argv []) {pid_t pid; pid = vfork (); // create process if (pid <0) {// error perror ("vfork");} if (0 = pid) {// sub-process sleep (3 ); // latency of 3 seconds printf ("I am son \ n"); _ exit (0); // exit sub-process, must} else if (pid> 0) {// parent process printf ("I am father \ n");} return 0 ;}
The code above has delayed the sub-process for 3 s. The result is that the parent process is executed only after the sub-process is finished. The running result is as follows:
Next, let's verify that the child process shares the address space of the parent process:
# Include <stdio. h> # include <stdlib. h> # include <unistd. h> int a = 10; int main (int argc, char * argv []) {pid_t pid; int B = 20; pid = vfork (); // create a process if (pid <0) {// error perror ("vfork");} if (0 = pid) {// sub-process a = 100, B = 200; printf ("son: a = % d, B = % d \ n", a, B); _ exit (0); // exit the sub-process, required} else if (pid> 0) {// parent process printf ("father: a = % d, B = % d \ n", a, B );} return 0 ;}
Generally, the running result shows that modifying the values of a and B in a sub-process will affect the values of a and B in the parent process, as shown below:
Vfork () ensures that the sub-process runs first. After it calls exec (process replacement) or exit (exit process), the parent process can be scheduled to run.If the child process does not call exec or exit, the program will lead to a deadlock. The program is a problematic program and has no meaning.The test code is as follows:
# Include <stdio. h> # include <stdlib. h> # include <unistd. h> int main (int argc, char * argv []) {pid_t pid; pid = vfork (); // create process if (pid <0) {// error perror ("vfork");} if (0 = pid) {// sub-process printf ("I am son \ n"); sleep (1 ); // The sub-process does not call exec or exit} else if (pid> 0) {// parent process printf ("I am father \ n"); sleep (1 );} return 0 ;}
The running result is as follows:
So,When using vfork () to create a process, you must call exec (process replacement) or exit (exit process) in the sub-process. Otherwise, the program may have problems and it is meaningless..
For sample code downloading in this tutorial, click here.