The vfork() function creates a new process as does
fork(), except that the child process shares the
same address space as the calling process. Execution of the calling
process is blocked until the child process calls one of the
exec() family of functions, or calls
Because parent and child share the address space, you must not
return from the function that called vfork(); doing so can
corrupt the parent's stack.
You should use vfork()
when your child process simply modifies the process state and then
calls one of the exec() functions. Because of the shared
address space, you must avoid doing anything in the child that
impacts the parent when it resumes execution. For example, if
your exec() call fails, you must call
_exit(), and not exit(),
because calling exit() would close standard I/O
stream buffers for the parent as well as the child.
Handlers registered with pthread_atfork() are not
invoked when vfork() is called, because doing so
would adversely affect the shared address space.
If successful, vfork()
returns 0 in the child process, and returns the process ID of the
child process to the parent process. On failure, it returns -1
and sets errno to one of the following values:
The system lacked the necessary
resources to create another process, or the system-imposed
limit on the total number of processes under execution
system-wide would be exceeded.
UNIX 98, with exceptions.
The vfork() function
provides an efficient mechanism to create a new process, in those
instances where you need to manipulate process state (for example,
closing file descriptors) prior calling one of the exec()
family of functions. vfork() does not create a new Windows
process context, however. Hence calling getpid()
in the child of a vfork() operation returns the same
value as in the parent. The process ID returned by vfork()
is actually the process ID of the exec()ed child. Refer to
Process Management in the
Windows Concepts chapter
of the PTC MKS Toolkit UNIX to Windows Porting Guide for a
detailed discussion of the process model.
PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition
- _exit(), execl(), execle(), execlp(), execlpe(), execv(), execve(), execvp(), execvpe(), exit(), fork(), getpid(), pthread_atfork()
PTC MKS Toolkit 10.1 Documentation Build 15.