fork(2) CLIX fork(2)
NAME
fork - Creates a new process
LIBRARY
Standard C Library (libc.a)
SYNOPSIS
#include <sys/types.h>
pid_t fork(
void );
DESCRIPTION
The fork() function creates a new process (child process) that is
identical to 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() functions)
⊕ Signal handling settings (that is, SIG_DFL, SIG_IGN, SIG_HOLD
referenced in signal.h, function address)
⊕ Set-user-ID mode bit
⊕ Set-group-ID mode bit
⊕ Profiling on/off status
⊕ Nice value (see the nice() function)
⊕ Process group ID
⊕ Session ID
⊕ tty group ID (described in the exit() function)
⊕ Current directory
⊕ Root directory
⊕ File mode creation mask (described in the umask() function)
⊕ File size limit (described in the ulimit() function)
2/94 - Intergraph Corporation 1
fork(2) CLIX fork(2)
⊕ Attached shared memory segments (described in the shmop() function)
⊕ Attached mapped file segments
⊕ The child process inherits all mapped regions with the same protection
and sharing mode as in the parent process.
The child process differs from the parent process in the following ways:
⊕ The child process has a unique process ID and does not match any active
process group ID.
⊕ The parent process ID of the child process matches the process ID of
the parent.
⊕ The child process has its own copy of the parent process's file
descriptors. However, each of the child's file descriptors shares a
common file pointer with the corresponding file descriptor of the
parent process.
⊕ All semadj values are cleared. (Information about semadj values can be
found in the semop() function.)
⊕ Process locks, text locks, and data locks are not inherited by the
child process. (Information about locks can be found in the plock()
function.)
⊕ The child process's utime(), stime(), cutime(), and cstime() functions
are set to 0.
⊕ Any pending alarms are cleared in the child process.
⊕ Any signals pending for the parent process are cleared for the child
process.
EXAMPLES
To create a new process:
int fork_retval;
fork_retval = fork();
switch(fork_retval)
case -1:
perror("Fork failed");
return(-1);
case 0:
/* Child */
...
2 Intergraph Corporation - 2/94
fork(2) CLIX fork(2)
default:
/* Parent, fork_retval is child's process ID */
...
RETURN VALUES
Upon successful completion, fork() returns a value of 0 to the child
process and returns the process ID of the child process to the parent
process. If fork() fails, a value of -1 is returned to the parent
process, no child process is created, and the global variable errno is set
to indicate the error.
ERRORS
The fork() function fails if one or more of the following are true:
[EAGAIN] The system-imposed limit on the total number of processes
executing would be exceeded.
[EAGAIN] The system-imposed limit on the total number of processes
executing for a single user would be exceeded.
[EAGAIN] The total amount of system memory available when reading
through raw input/output is temporarily insufficient.
RELATED INFORMATION
Functions: exec(2), exit(2), times(2), plock(2), nice(2), ptrace(2),
semop(2), shmop(2), sigaction(2), signal(2), sigset(2), ulimit(2),
umask(2), wait(2), waitpid(2)
2/94 - Intergraph Corporation 3