?
Solved

Difference between vfork and fork

Posted on 2001-06-09
3
Medium Priority
?
1,965 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses

777 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