The difference between fork and vfork:
1, fork is to create a child process, and the parent process to copy the memory data into the child process.
Vfork is to create a child process and share with the parent process's memory data.
2, Vfork is such a work:
(1), ensure that the child process first execution.
(2), when the child process calls exit () or exec (), the parent process executes down.
3, fork later use of the optimization technology, so that the fork is not immediately after the copy of memory, but only when you need to change, it will be copied from the parent process to the child process, so fork immediately after the cost of executing exec is very small. Vfork is more dangerous because of shared memory.
Why will appear vfork ?
The reason is: only fork at first, but a lot of programs after fork a child process exec an external program, so fork need to copy the parent process of data this action becomes meaningless, but also very heavy, so, made a parent-child process shared vfork. So, Vfork was born to exec.
The difference between exit and return:
Exit function in header file stdlib.h the.
exit (0): run the program normally and exit the program;
exit (1): non-normal operation resulted in exiting the program;
return (): returns a function that, if in main function, exits the function and returns a value that can be written as return (0) , or return 0 .
Detailed said:
1. return returns the function value, which is the keyword;exit is a function.
2.returnis the language level, which represents the return of the call stack;Exitis the system call level, which represents the end of a process.
3.returnis the exit (return) of the function;Exitis the exit of the process.
4. return is provided by the C language, andexit is provided by the operating system (or given in the function library).
5.returnused to end the execution of a function, and to pass the execution information of a function out to other calling functions;ExitThe function is to exit the application, delete the memory space used by the process, and return a state of the application toOS, this status identifies some of the application's operational information, which is related to the machine and the operating system, and is generally0for normal exit, non-0for an abnormal exit.
6. Calling in a non-main functionreturnand theExitthe effect is obvious, butMainfunction is called in thereturnand theExitthe phenomenon is very vague, in most cases the phenomenon is consistent.
Why does return hang, exit () not?
from the above we know that the call to end the child process is exit () instead of return, if you return in vfork, then this means that the main () function is return, note because function Stack Parent-child process sharing , so the stack on the whole program is out of the question. If you return in a subprocess, it is basically the following procedure:
(1), first the main () function of the child process return.
(2), when the main () function is return, exit () or similar function (such as Exitgroup ()) is usually called.
(3), at this time, the parent process receives the child process exit (), starts to return from the Vfork, but the parent process stack is the quilt process dries out, how do you want me to execute? (Note: The stack will return a strange stack address, for some kernel version implementation, directly reported "stack error", however, for some kernel version implementation, it is possible to call main () again, and then entered an infinite loop results until the vfork call return error)
and return to return and exit, return will release the local variable and bounce back to the upper function execution. Exit directly. If you use C + + you know that return calls the destructor of the local object, exit does not. (Note: Exit is not a system call, it is the encapsulation of glibc to the system call _exit () or _exitgroup ().)
As can be seen, the child process call exit () does not modify the function stack, so the parent process executes smoothly.
Kernel Code Analysis:
The Linux Create child process is actually a process that replicates the parent process. So it's more appropriate to say clone. The reason Linux started using fork was that the word clone was not popular at the time. The actual presence of Fork,clone,vfork three system calls. Fork is a full copy, clone is a selective copy, and Vfork completely uses the parent process's resources. You can understand that vfork is the thread that was created. The appearance of Vfork is primarily intended to be considered immediately in the execution of the exec procedure. But later kernel all support copy_on_write, so the mechanism of vfork to improve efficiency is not so obvious.
The three system calls in the kernel are finally called Do_fork:
Fork:return do_fork (SIGCHLD, Regs.esp, S, 0);
Clone: return Do_fork (Clone_flags, newsp, S, 0);
Vfork:return Do_fork (Clone_vfork | CLONE_VM | SIGCHLD, Regs.esp, S, 0);
#define CLONE_VFORK 0x00004000/* Set if the parent wants the child to wake it up on mm_release*/
#define CLONE_VM 0x00000100/* Set if VM shared between processes */
The above two macros indicate that vfork requires the child process to wake the parent process after executing the mm_release and share the virtual memory
Why would a child process be required to advance?
Take virtual memory for example. A process requires a structure to manage its own virtual memory space, which is a pointer to a mm_struct type in the process structure body task_struct. When you fork, the kernel creates a new struct, copies the mm_struct itself and the subordinate structure, and sets the mm_struct of the child process to point to the new memory. And vfork just copied the task_struct itself, and did not recursively go down. Simply put: Fork replicates the memory and vfork copies the pointer.
Related examples:
Fork
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/82/DB/wKiom1dipdCCezOvAABOLcIcNeA694.png-wh_500x0-wm_3 -wmp_4-s_1197728594.png "title=" For1. PNG "alt=" Wkiom1dipdccezovaabolcicnea694.png-wh_50 "/>
Vfork
650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/82/DA/wKioL1dipxyzf6EkAABU_V_8Bgw058.png-wh_500x0-wm_3 -wmp_4-s_130836043.png "title=" VF. PNG "alt=" Wkiol1dipxyzf6ekaabu_v_8bgw058.png-wh_50 "/>
The result of the operation is:
Fork Run Result:
650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/82/DA/wKioL1dip5SwoACRAAAXSuAu6yY596.png-wh_500x0-wm_3 -wmp_4-s_367715572.png "title=" F1. PNG "alt=" Wkiol1dip5swoacraaaxsuau6yy596.png-wh_50 "/>
Vfork Running results:
1. An error occurred while returning the return:
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/82/DA/wKioL1dip1LBNVCLAAA0T-vogDc379.png-wh_500x0-wm_3 -wmp_4-s_2973733184.png "title=" return. PNG "alt=" Wkiol1dip1lbnvclaaa0t-vogdc379.png-wh_50 "/>
2. Exit returns the result:
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/82/DB/wKiom1diplaC4HxLAAAVaPSUz7k360.png-wh_500x0-wm_3 -wmp_4-s_2974913631.png "title=" exit. PNG "alt=" Wkiom1diplac4hxlaaavapsuz7k360.png-wh_50 "/>
operation result Description: Vfrok when the parent and child processes share data segments, the fork is copied. If, in the Vfork child process, returns with return, a segment error occurs.
Fork and Vfork,return and exit understanding