vfork() explained: to use or not to use.

The vfork() call is a special implementation of the fork() call.

vfork() is available on about all unices systems.

From the Linux Man Page:

 

vfork(2)  - Linux man page

NAME
     vfork - create a child process and block parent
.....
.....
.....

Linux Description
     vfork(), just like fork, creates a child process of the calling process.
     For details and return value and errors, see fork.
     vfork() is a special case of  clone(2). It is used to create new processes
     without copying the page tables of the parent process. It may be useful in
     performance sensitive applications where a child will be created which then
     immediately issues an execve().

     vfork() differs from fork() in that the parent is suspended until the
     child makes a call to execve(2) or _exit(2).

.........

 

From the Mac OSX man page:

 

 

 

VFORK(2)                    BSD System Calls Manual                   VFORK(2)

NAME
     vfork -- spawn new process in a virtual memory efficient way

SYNOPSIS
     #include <unistd.h>

     pid_t
     vfork(void);

DESCRIPTION
     Vfork() can be used to create new processes without fully copying the
     address space of the old process, which is horrendously inefficient in a
     paged environment.  It is useful when the purpose of fork(2) 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(2) or an exit (either by a call to exit(2) 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 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 childs context from the procedure that
     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 can't 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.)

As you can see from these manual pages the vfork() call has been implemented speed up the inefficient fork() call.

While the vfork() call is useful if you need to launch another process from your running process, you shall take care of it. For example, as shown in POS33, due to the implementation of the vfork() function, the parent process is suspended while the child process executes. If a user sends a signal to the child process, delaying its execution, the parent process (which is privileged) is also blocked. This means that an unprivileged process can cause a privileged process to halt, which is a privilege inversion resulting in a denial of service.

So, yhe best practice is to use the fork() call, however if you need the vfork() for your own project remeber that this call can cause a denial of service without highest privilegies.

 

Gg1

Posted by at April 12, 2011
Filed in category: iphone, linux, Mac OS X, The Prince: C, Tower of Babel, xAppSoftware News, and tagged with: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <font color="" face="" size=""> <span style="">

Statistical data collected by Statpress SEOlution (blogcraft).