fork, f_fork, p_fork, or vfork Subroutine
Purpose
Creates a new process.
Libraries
fork, f_fork, and vfork: Standard C Library (libc.a)
Syntax
#include <unistd.h>
pid_t fork(void)
pid_t p_fork(uint)
unsigned int flags;
pid_t f_fork(void)
int vfork(void)
Description
Each fork subroutine creates a new process. The new process (child process) is an almost exact copy of the calling process (parent process). The child process inherits the following attributes from the parent process:
- Environment
- Close-on-exec flags (described in the exec subroutine)
- Signal handling settings (such as the SIG_DFL value, the SIG_IGN value, and the Function Address parameter)
- Set user ID mode bit
- Set group ID mode bit
- Profiling on and off status
- Nice value
- All attached shared libraries
- Process group ID
- tty group ID (described in the exit, atexit, or _exit subroutine, signal subroutine, and raise subroutine)
- Current directory
- Root directory
- File-mode creation mask (described in the umask subroutine)
- File size limit (described in the ulimit subroutine)
- Attached shared memory segments (described in the shmat subroutine)
- Attached mapped file segments (described in the shmat subroutine)
- Debugger process ID and multiprocess flag if the parent process has multiprocess debugging enabled (described in the ptrace subroutine).
The child process differs from the parent process in the following ways:
- The child process has only one user thread; it is the one that called the fork subroutine.
- The child process has a unique process ID.
- The child process ID does not match any active process group ID.
- The child process has a different parent process ID.
- The child process has its own copy of the file descriptors for the parent process. However, each file descriptor of the child process shares a common file pointer with the corresponding file descriptor of the parent process.
- All semadj values are cleared. For information about semadj values, see the semop subroutine.
- Process locks, text locks, and data locks are not inherited by the child process. For information about locks, see the plock subroutine.
- If multiprocess debugging is turned on, the trace flags are inherited from the parent; otherwise, the trace flags are reset. For information about request 0, see the ptrace subroutine.
- The child process utime, stime, cutime, and cstime subroutines are set to 0. (For more information, see the getrusage , times, and vtimes subroutines.)
- Any pending alarms are cleared in the child process. (For more information, see the incinterval, setitimer, and alarm subroutines.)
- The set of signals pending for the child process is initialized to an empty set.
- The child process can have its own copy of the message catalogue for the parent process.
The f_fork subroutine is similar to fork, except for:
- It is required that the child process calls one of the exec functions immediately after it is created. Since the fork handlers are never called, the application data, mutexes and locks are all undefined in the child process.
The p_fork subroutine is similar to fork except that zero or more flags can
be specified. The FORK_NO_SHM flag causes a child process to be created without
inheriting shared memory. The FORK_FAST flag creates a child process without
calling fork handlers. Calling p_fork(FORK_FAST) is equivalent to calling
f_fork(). Calling p_fork(0) is equivalent to calling
fork().
The vfork subroutine is supported as a compatibility interface for
older Berkeley Software Distribution (BSD) system programs and can be used by compiling with the
Berkeley Compatibility Library (libbsd.a). Calling vfork() is equivalent to
calling fork().
For additional information, see the /usr/lpp/GL/README file.
Parameters
- flags
- Specifies the behavior of the fork operation. You can specify zero or more of the following values:
- FORK_NO_SHM
- Shared memory is not inherited by the child process.
- FORK_FAST
- The fork handlers are not called. This is similar to the f_fork() subroutine. The child process must call one of the exec functions immediately.
Return Values
Upon successful completion, each fork subroutine returns a value of 0 to the child process and returns the process ID of the child process to the parent process. Otherwise, a value of -1 is returned to the parent process, no child process is created, and the errno global variable is set to indicate the error.
Error Codes
The fork subroutines are unsuccessful if one or more of the following are true:
| Item | Description |
|---|---|
| EAGAIN | Exceeds the limit on the total number of processes running either system-wide or by a single user, or the system does not have the resources necessary to create another process. |
| ENOMEM | Not enough space exists for this process. |
| EPROCLIM | If WLM is running, the limit on the number of processes or threads in the class may have been met. |