Your Browser is not longer supported
Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...
{{viewport.spaceProperty.prod}}
fork - create new process
&pagelevel(4)&pagelevel
Syntax | #include <unistd.h> Optional #include <sys/types.h> (End) pid_t fork(void); |
Description | fork() creates a new process. The new process (child process) is an exact copy of thecalling process (parent process) in all of the following points:
real and effective user and group IDs environment close-on-exec bit (see exec() ) signal actions (SIG_DFL , SIG_IGN , address of the signal handling function) supplementary group IDs set-user-ID mode bit set-group-ID mode bit nice value (see nice() ) all attached shared memory segments (see shmat() ) process group ID session ID (see exit() ) current working directory root directory file mode creation mask (see umask() ) resource limits (see getrlimit() ) controlling terminal
The child process differs from the parent process in the following points: - The child process has a unique process ID. The child process ID also does not match any active process group ID.
The child process also 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. All the child ́s file descriptors share the same file description as the corresponding file descriptor of the parent. The child process has its own copy of the parent ́s directory streams. All directory streams in the child process may share the file position indicator with the corresponding directory stream of the parent. The child process may have its own copy of the parent ́s message catalog descriptors. The child process values for the tms structure components tms_utime , tms_stime , tms_cutime and tms_cstime are set to 0 (see times() ). The time left until an alarm clock signal is reset to 0 (see
alarm() ). All semadj values are deleted (see semop() ). File locks set by the parent process are not inherited by the child process (see also fcntl() ). The set of signals pending for the child process is initialized to the empty set.
A process is created with a single thread. If a ”multi-threaded” process calls fork() , the new process contains a copy of the calling thread and its entire address space, including the state of Mutex objects and other resources. Fork handlers can be set up with the pthread_atfork() function. BS2000 - BS2000 files, with the exception of memory pools, are not inherited with
fork() . The following BS2000 resources are also not inherited: - Opened BS2000 files do not remain open
- AID breakpoints
- Task File Table (TFT)
- SYSFILE assignments
- Registered STXIT and contingency routines (End)
|
Return val. | 0 | upon successful completion. fork() returns 0 to the child process and returns the process ID of the child process to the parent process. |
| -1 | if an error occurs. -1 is returned to the parent process, no child process is created, and errno is set to indicate the error. |
Errors | fork() will fail if:
|
| EAGAIN
| The system lacks 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, or if DIV or FASTRAM areas are stored in the parent process. |
| Extension |
| ENOMEM
| The swap area is too small. (End) |
Notes | As of this version, fork() can also be used in signal handling and contingency routines. |
See also | alarm() , exec , fcntl() , semop() , signal() , times() , sys/types.h , unistd.h .
|