fork - create a new process
#include <sys/types.h> #include <unistd.h> pid_t fork(void);
The fork() function creates a new process. The new process (child process) is an exact copy of the calling process (parent process) except as detailed below.
- The child process has a unique process ID.
- The child process ID also does not match any active process group ID.
- The child process has a different parent process ID (that is, the process ID of the parent process).
- The child process has its own copy of the parent's file descriptors. Each of the child's file descriptors refers to the same open file description with the corresponding file descriptor of the parent.
- The child process has its own copy of the parent's open directory streams. Each open directory stream in the child process may share directory stream positioning with the corresponding directory stream of the parent.
- The child process may have its own copy of the parent's message catalogue descriptors.
- The child process' values of tms_utime, tms_stime, tms_cutime and tms_cstime are set to 0.
- The time left until an alarm clock signal is reset to 0.
- All semadj values are cleared.
- File locks set by the parent process are not inherited by the child process.
- The set of signals pending for the child process is initialised to the empty set.
- Interval timers are reset in the child process.
- If the Semaphores option is supported, any semaphores that are open in the parent process will also be open in the child process.
- If the Process Memory Locking option is supported, the child process does not inherit any address space memory locks established by the parent process via calls to mlockall() or mlock().
- Memory mappings created in the parent are retained in the child process. MAP_PRIVATE mappings inherited from the parent will also be MAP_PRIVATE mappings in the child, and any modifications to the data in these mappings made by the parent prior to calling fork() will be visible to the child. Any modifications to the data in MAP_PRIVATE mappings made by the parent after fork() returns will be visible only to the parent. Modifications to the data in MAP_PRIVATE mappings made by the child will be visible only to the child.
- If the Process Scheduling option is supported, for the SCHED_FIFO and SCHED_RR scheduling policies, the child process inherits the policy and priority settings of the parent process during a fork() function. For other scheduling policies, the policy and priority settings on fork() are implementation-dependent.
- If the Timers option is supported, per-process timers created by the parent are not inherited by the child process.
- If the Message Passing option is supported, the child process has its own copy of the message queue descriptors of the parent. Each of the message descriptors of the child refers to the same open message queue description as the corresponding message descriptor of the parent.
- If the Asynchronous Input and Output option is supported, no asynchronous input or asynchronous output operations are inherited by the child process.
The inheritance of process characteristics not defined by this document is implementation-dependent. After fork(), both the parent and the child processes are capable of executing independently before either one terminates.
A process is created with a single thread. If a multi-threaded process calls fork(), the new process contains a replica of the calling thread and its entire address space, possibly including the states of mutexes and other resources. Consequently, to avoid errors, the child process may only execute async-signal safe operations until such time as one of the exec functions is called. Fork handlers may be established by means of the pthread_atfork() function in order to maintain application invariants across fork() calls.
Upon successful completion, fork() returns 0 to the child process and returns the process ID of the child process to the parent process. Otherwise, -1 is returned to the parent process, no child process is created, and errno is set to indicate the error.
The fork() function will fail if:
- [EAGAIN]
- 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 or by a single user {CHILD_MAX} would be exceeded.
The fork() function may fail if:
- [ENOMEM]
- Insufficient storage space is available.
None.
None.
None.
alarm(), exec, fcntl(), semop(), signal(), times(), <sys/types.h>, <unistd.h>.
Derived from Issue 1 of the SVID.