What is vfork()?
chris at umcp-cs.UUCP
chris at umcp-cs.UUCP
Fri Nov 4 07:46:19 AEST 1983
Vfork() is intended to be a more efficient version of fork(),
especially for large programs. It is mainly useful for programs
wishing to exec() some other program. The idea behind vfork() is
to not actually copy all the pages of the program, when it isn't
going to use most of them anyway.
4.1 behavior:
Like fork(), vfork() produces a copy of a process. Unlike
fork(), it doesn't actually copy the process. Instead it
fiddles with kernel data structures a bit, copies the page
tables (memory allocation list) for the process to the new
process, and suspends the "parent" (who has, at the moment,
no memory, since it's all given to the "child"). The child
gets to run until it exit()s or exec()s something else.
Then, instead of releasing the memory/page tables/etc, the
kernel gives everything back to the parent and lets it
continue.
This "cheating" has the interesting side effect that the
code:
main () {
int i = 0;
if (vfork() == 0) /* child */
exit (i = 10);
printf ("i = %d\n", i);
}
prints "10". The child can modify the parent! However, this
is not useful for IPC since the parent is held suspended until
the child exit()s or exec()s. Also, this behavior is considered
wrong, and will change (has changed?) in future releases of
Berkeley Unix. Oh yeah, if "i" were a "register int" you'd
get 0 (howzat for consistency?).
4.2: (maybe, maybe not yet, ``definitely'' in 4.3)
The vfork() system call will be (has been?) changed to allow
both processes to run simultaneously, sharing memory resources
as long as the child does not attempt to write to the shared
memory. If the child does write to the shared memory, the
write is trapped and the memory is copied to a new page (or,
if necessary, depending on hardware, maybe the entire process
is copied) and the child shares one less page (or no pages).
This will actually be done to the fork() system call as the
behavior of fork() and vfork() will then be identical. The
new implementation is merely much more efficient (on paged
systems anyway).
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci
UUCP: {seismo,allegra,brl-bmd}!umcp-cs!chris
CSNet: chris at umcp-cs ARPA: chris.umcp-cs at CSNet-Relay
More information about the Comp.unix.wizards
mailing list