vfork()

Greg Hunt hunt at dg-rtp.rtp.dg.com
Wed Mar 20 04:16:26 AEST 1991


In article <8372 at rsiatl.Dixie.Com>, stan at Dixie.Com (Stan Brown) writes:
> 
> 	A quick question.  I have run into a couple of packages that
> 	use a routine called vfork().  My libraries/system cals don't
> 	include this.
> 
> 	Could someone tell me how this is different from fork()
> 	Also any ideas on how to implemet it using fork() (or
> 	otherwise) would be appreicated.

The vfork(2) system call does the same thing as the fork(2) system call
except that the child process shares the same address space as the
parent process, instead of having a complete copy of the parent's
address space made for it during the fork.  It uses the same mechanism
as fork(2) to indicate which process is the child (the call returns
zero) and which process is the parent (the call returns the PID of the
child).

This is a more efficient way of forking processes that are simply going
to immediately do an exec(2) of some sort to load and execute a
different executable.  Avoiding the copying of the parent's address
space can save a fair bit of time.

Using vfork(2), however, has some gotchas.  Since the child is sharing
the parent's address space, the child must be extremely careful about
modifiying anything in the address space, since those changes will
affect the parent process when it regains control after the child has
executed an exec(2) of some sort, exit'ed, or terminated abnormally.
This means it can't return from the routine that it's in, and it must
be extremely careful about changing global and local variables, etc.

You should be able to replace the vfork(2) calls with fork(2) calls,
but you need to see what, if any, changes the child is making to the
parent's address space before it does its exec(2) call.  If the child
isn't changing anything, then replacing the vfork(2) calls with fork(2)
calls should work without problems.  If however, the child is changing
parts of the parent's address space, then you'll have to figure out
some other way for the child to communicate those changes to the
parent, since with the fork(2) the child won't be sharing the address
space with the parent.  Maybe you can figure out a way to make the
changes in other local variables before the fork(2), and then have the
parent copy those new local variables into the real local or global
variables that the child wanted to alter.

Hope this helps.  Good luck!

-- 
Greg Hunt                        Internet: hunt at dg-rtp.rtp.dg.com
DG/UX Kernel Development         UUCP:     {world}!mcnc!rti!dg-rtp!hunt
Data General Corporation
Research Triangle Park, NC, USA  These opinions are mine, not DG's.



More information about the Comp.unix.questions mailing list