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:

    When you’ve been blind as long as I have, you learn to see through your senses. I can’t explain it exactly, but you get a feeling about people when you meet them. You see a picture of them in your mind. Not just what they look like, but what they really are. You see them much more clearly than you do with your eyes. Maybe that’s why they say looks are deceptive.
    —George Bricker. Jean Yarbrough. Helen Page (Jane Adams)

    The cup of blessing that we bless, is it not a sharing in the blood of Christ? The bread that we break, is it not a sharing in the body of Christ?
    Bible: New Testament, 1 Corinthians 10:16.