Alpha Vijayan
April 22nd, 2004, 12:13 AM
What is the difference b/w fork() and vfork()?I heared that fork() is used by SystemV and vfork() by BSD.The difference must be in the address space allocated to the child process.Can anybodu five the exact difference.Thank u
dimm_coder
April 28th, 2004, 03:45 AM
Have U read the appropriate man pages for fork() and vfork()?
Well, just a brief overview.
When some process calls fork(), a child process is created. After that there are 2 processes (parent and child) are exist and they have the same memory data space copies (but share executable segments of course).
Let's check the penalties incurred by fork():
*In old days*, the OS has (1) to create copies of all parent's data segments, (2) duplicate the parent's page tables and (3) create a new task struct. The step (1) is a bottleneck and requires enough time.
What's worse is that usually the next child process's command is one of the exec() -family functions. After that, the child process's address space is replaced by a new one, created for a new program. So these steps (1), (2) are just a waste of kernel's time. Thus BSD implemented vfork(). U should call vfork() when the next command in the child process is exec().
The child process "borrows" the parent's memory space until this call (exec()) will be completed and the child will have its own. The parent process is suspended while the child is using its resources.
That require from the child:
(From XPG4 / SUSv2 / POSIX draft.) The vfork() function has the same
effect as fork(), except that the behaviour is undefined if the process
created by vfork() either modifies any data other than a variable of
type pid_t used to store the return value from vfork(), or returns from
the function in which vfork() was called, or calls any other function
before successfully calling _exit() or one of the exec family of func-
tions.
So, U see, that the child process should call exec() or exit() and do nothing to modify the temporary borrowed address space.
In our days (well, 80-years), copy_on_write was implemetned. So the step (1) is not required for fork() any more. That memory is shared until someone (parent or child) modifies it.
Thus the fork() call is not so a big bottleneck, but some performance sensitive applications may prefer to use vfork() instead.
From Linux man pages:
vfork() is a special case of clone(2). It is used to create new pro-
cesses without copying the page tables of the parent process. It may
be useful in performance sensitive applications where a child will be
created which then immediately issues an execve().
Alpha Vijayan
April 29th, 2004, 01:37 AM
Mr.Dimm_codder,
Thank u so much for ur patience to give me a reply.
have a nice day