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:

    I drink the five o’clock martinis
    and poke at this dry page like a rough
    goat. Fool! I fumble my lost childhood
    for a mother and lounge in sad stuff
    with love to catch and catch as catch can.
    Anne Sexton (1928–1974)

    ... probably all of the women in this book are working to make part of the same quilt to keep us from freezing to death in a world that grows harsher and bleaker—where male is the norm and the ideal human being is hard, violent and cold: a macho rock. Every woman who makes of her living something strong and good is sharing bread with us.
    Marge Piercy (b. 1936)