Fork (operating System) - Vfork and Page Sharing

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:

It is rather unfortunate that Linux revived this specter from the past. The BSD man page states: "This system call will be eliminated when proper system sharing mechanisms are implemented. Users should not depend on the memory sharing semantics of vfork as it will, in that case, be made synonymous to fork(2)."

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:

    Any man who can write a page of living prose adds something to our life, and the man who can, as I can, is surely the last to resent someone who can do it even better. An artist cannot deny art, nor would he want to. A lover cannot deny love.
    Raymond Chandler (1888–1959)

    We fight our way through the massed and leveled collective safe taste of the Top 40, just looking for a little something we can call our own. But when we find it and jam the radio to hear it again it isn’t just ours—it is a link to thousands of others who are sharing it with us. As a matter of a single song this might mean very little; as culture, as a way of life, you can’t beat it.
    Greil Marcus (b. 1945)