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:
“When you write down your life, every page should contain something no one has ever heard about.”
—Elias Canetti (b. 1905)
“However intense my experience, I am conscious of the presence and criticism of a part of me, which, as it were, is not a part of me, but a spectator, sharing no experience, but taking note of it, and that is no more I than it is you. When the play, it may be the tragedy, of life is over, the spectator goes his way. It was a kind of fiction, a work of the imagination only, so far as he was concerned.”
—Henry David Thoreau (18171862)