Solved

Difference between vfork and fork

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Include multiple hostnames in this command? 3 55
Replacement selected text 2 62
Smart Camera scanning and reading information 3 106
JQuery serialize and unserialize 8 86
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

810 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