Solved

Difference between vfork and fork

Posted on 2001-06-09
3
1,896 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Pressing cancel button with C# UI Automation 3 40
canBalance challenge 34 65
mapAB Challlenge 35 84
unix example issues 18 44
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now