vfork() — Create a new process

Standards

Standards / Extensions C or C++ Dependencies

XPG4.2
Single UNIX Specification, Version 3

both  

Format

#define _XOPEN_SOURCE_EXTENDED 1
#include <sys/types.h>
#include <unistd.h>

pid_t vfork(void);
Note: Although POSIX.1 does not require that the <sys/types.h> include file be included, XPG4 has it as an optional header. Therefore it is recommended that you include it for portability.

General description

The vfork() function creates a new process. The vfork() function has the same effect as fork(), except that the behavior is undefined, if the process created by vfork() attempts to call any other C/370™ function before calling either exec() or _exit(). The new process (the child process) is an exact duplicate of the process that calls vfork() (the parent process), except for the following:
  • The child process has a unique process ID (PID), which does not match any active process group ID.
  • The child has a different parent process ID, that is, the process ID of the process that called vfork().
  • The child has its own copy of the parent's file descriptors. Each file descriptor in the child refers to the same open file description as the corresponding file descriptor in the parent.
  • The child has its own copy of the parent's open directory streams. Each child's open directory stream may share directory stream positioning with the corresponding parent's directory stream.
  • The following elements in the tms structure are set to 0 in the child:
    • tms_utime
    • tms_stime
    • tms_cutime
    • tms_cstime

    For more information about these elements, see times() — Get process and child process times.

  • The child does not inherit any file locks previously set by the parent.
  • The child process has no alarms set (similar to the results of a call to alarm() with an argument value of 0).
  • The child has no pending signals.
  • The child process may have its own copy of the parent's message catalog descriptors.
  • All semadj values are cleared.
  • Interval timers are reset in the child process.

In all other respects, the child is identical to the parent. Because the child is a duplicate, it contains the same call to vfork() that was in the parent. Execution begins with this vfork() call, which returns a value of 0; the child then proceeds with normal execution.

The vfork() function is not supported from a multithread environment.

For more information on vfork() from a z/OS® perspective, refer to z/OS UNIX System Services Programming: Assembler Callable Services Reference.

You can use z/OS memory files from a z/OS UNIX program. However, use of the vfork() function from the program removes access from a hiperspace memory file for the child process. Use of an exec function from the program clears a memory file when the process address space is cleared.

Special behavior for C: For POSIX resources, vfork() behaves as just described. But in general, MVS™ resources that existed in the parent do not exist in the child. This is true for open streams in MVS data sets and assembler-accessed z/OS facilities, such as STIMERS. In addition, z/OS allocations (through JCL, SVC99, or ALLOCATE) are not passed to the child process.

Note: The vfork() function has been moved to obsolescence in Single UNIX Specification, Version 3 and may be withdrawn in a future version. The fork() function is preferred for portability.

Returned value

If successful, vfork() returns 0 to the child process and the process ID of the newly created child to the parent process.

If unsuccessful, vfork() fails to create a child process and returns -1 to the parent. vfork() sets errno to one of the following values:
Error Code
Description
EAGAIN
There are insufficient resources to create another process, or else the process has already reached the maximum number of processes you can run.
ELEMSGERR
Language Environment® message file not available.
ELEMULTITHREAD
vfork() was invoked from a multi-threaded environment.
ELENOFORK
Application contains a language that does not support fork().
ENOMEM
The process requires more space than is available.

Related information