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
Solved

Difference between vfork and fork

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

839 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