Vfork and Page Sharing
vfork
is another UNIX system call used to create a new process. When a vfork
system call is issued, the parent process will be suspended until the child process has either completed execution or been replaced with a new executable image via one of the execve
family of system calls. Even in vfork
, the pages are shared among the parent and child process. But vfork
does not mandate copy-on-write. Hence if the child process makes a modification in any of the shared pages, no new page will be created and the modified pages are visible to the parent process too. Since there is absolutely no page copying involved (consuming additional memory), this technique is highly efficient when a process needs to execute a blocking command using the child process.
On some implementations, vfork
is the same as fork
.
The vfork
function differs from fork
only in that the child process can share code and data with the calling process (parent process). This speeds cloning activity significantly, at a risk to the integrity of the parent process if vfork
is misused.
The use of vfork
for any purpose except as a prelude to an immediate call to a function from the exec
family, or to _exit
, is not advised. In particular the Linux man page for vfork strongly discourages its use:
The vfork
function can be used to create new processes without fully copying the address space of the old process. If a forked process is simply going to call exec
, the data space copied from the parent to the child by fork
is not used. This is particularly inefficient in a paged environment, making vfork
particularly useful. Depending upon the size of the parent's data space, vfork
can give a significant performance improvement over fork
.
The vfork
function can normally be used just like fork
. It does not work, however, to return while running in the child's context from the caller of vfork
since the eventual return from vfork
would then return to a no longer existent stack frame. Care must also be taken to call _exit
rather than exit
if exec
cannot be called, since exit
flushes and closes standard I/O channels, thereby damaging the parent process's standard I/O data structures. (Even with fork
, it is still incorrect to call exit
, since buffered data would then be flushed twice.)
If signal handlers are invoked in the child process after vfork
, they must follow the same rules as other code in the child process.
Read more about this topic: Fork (operating System)
Famous quotes containing the words page and/or sharing:
“A book is like a manclever and dull, brave and cowardly, beautiful and ugly. For every flowering thought there will be a page like a wet and mangy mongrel, and for every looping flight a tap on the wing and a reminder that wax cannot hold the feathers firm too near the sun.”
—John Steinbeck (19021968)
“By sharing the information and observations with the caregiver, you have a chance to see your child through another pair of eyes. Because she has some distance and objectivity, a caregiver often sees things that a parents total involvement with her child doesnt allow.”
—Amy Laura Dombro (20th century)