Solved

Difference between vfork and fork

Posted on 2001-06-09
3
1,951 Views
Last Modified: 2013-12-26
Hi!

I need to know which is the main difference between fork and vfork on Unix Systems.

Thanks
0
Comment
Question by:ppt
3 Comments
 
LVL 3

Expert Comment

by:superschlonz
ID: 6172396
fork creates a copy of the process. You have 2 processes running after the fork and
both processes continue at the next statement after the call to fork.

vfork virtually creates a new process that runs in the same adress space as the old one.
the old process is stopped as long as the new process doesn't call exec or _exit. After
the new process called exec or _exit the old process continues at the statement after vfork.

Use vfork if you want to run another process with exec and want the old process to
continue running after the new one has been started.
Here is a very simple example:

int execFailed = 0;
int newPid;

newPid = vfork();
if( newPid == 0 )
{
  exec( ... );
  // if we get here exec failed
  execFailed = 1;
}
else( if newPid == -1 )
{
  // vfork failed
}
else if( execFailed )
{
  // exec failed
}

But under Linux  this code will not work because fork and vfork are the same.
0
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 6177417
That's because vfork was a performance hack done solely because the original BSD 4.2 system didn't have copy-on-write capabilities in the VM.  Once you have copy-on-write, you don't need vfork any more.
0
 

Accepted Solution

by:
rajiv_indya earned 50 total points
ID: 6264076
Solution:  
vfork can be used to create new processes without fully copying the
   address space of the old process. It is useful when the purpose of
   fork would have been to create a new system context for an execve.
   vfork differs from fork in that the child borrows the parent's memory
   and thread of control until a call to execve or an exit (either by a
   call to exit or abnormally.) The parent process is suspended while the
   child is using its resources.
   
   vfork returns 0 in the child's context and (later) the process ID
   (PID) of the child in the parent's context.
   
   vfork can normally be used just like fork. It does not work, however,
   to return while running in the child's context from the procedure
   which called vfork since the eventual return from vfork would then
   return to a no longer existent stack frame. Be careful, also, to call
   _exit rather than exit if you cannot execve, since exit will flush and
   close standard I/O channels, and thereby mess up the parent processes
   standard I/O data structures. Even with fork it is wrong to call exit
   since buffered data would then be flushed twice.
 
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to create frequencies of a variable from SAS dataset? 10 139
Whole sheet autoscrub still needed 19 54
Tab names are off by one 5 54
Define unique primary key 9 95
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seveā€¦
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question