[Go to CFHT Home Page] Man Pages
Back to Software Index  BORDER=0Manpage Top Level
    fork(2) manual page Table of Contents

Name

fork, fork1 - create a new process

Synopsis


#include <sys/types.h>
#include <unistd.h>

pid_t fork(void);

pid_t fork1(void);

MT-Level

fork() is Async-Signal-Safe

Description

fork() and fork1() cause creation of a new process. The new process (child process) is an exact copy of the calling process (parent process). The child process inherits the following attributes from the parent process:

Scheduling priority and any per-process scheduling parameters that are specific to a given scheduling class may or may not be inherited according to the policy of that particular class (see priocntl(2) ). The child process differs from the parent process in the following ways:

Record locks set by the parent process are not inherited by the child process (see fcntl(2) ).

MT fork()

Solaris Threads

The following are the fork() semantics in programs that use the Solaris threads API rather than the POSIX threads API (programs linked with -lthread but not -lpthread):

fork() duplicates all the threads (see thr_create(3T) ) and LWP s in the parent process in the child process. fork1() duplicates only the calling thread (LWP ) in the child process.

POSIX Threads

The following are the fork() semantics in programs that use the POSIX threads API rather than the Solaris threads API (programs linked with -lpthread, whether or not linked with -lthread):

The call to fork() is like a call to fork1(), which replicates only the calling thread. There is no call that forks a child with all threads and LWPs duplicated in the child.

Note that if a program is linked with both libraries (-lthread and -lpthread), the POSIX semantic of fork() prevails.

Fork-safety

If fork1() is called in a Solaris thread program or fork() is called in a POSIX thread program, and the child does more than just call exec(), there is a possibility of deadlocking in the child. To ensure that the application is safe with respect to this deadlock, it should use pthread_atfork(3T) . Should there be any outstanding mutexes throughout the process, the application should call pthread_atfork(3T) , to wait for and acquire those mutexes, prior to calling fork(). (See Intro(3) , "MT-Level of Libraries")

Return Values

Upon successful completion, fork() and fork1() 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 (pid_t)-1 is returned to the parent process, no child process is created, and errno is set to indicate the error.

Errors

fork() fails and no child process are created if one or more of the following is true:

EAGAIN
There are two conditions that will cause an EAGAIN error.
The system-imposed limit on the total number of processes under execution
by a single user would be exceeded.
The total amount of system memory available is temporarily insufficient to
duplicate this process.
ENOMEM
There is not enough swap space.

See Also

alarm(2) , exec(2) , exit(2) , fcntl(2) , getitimer(2) , getrlimit(2) , memcntl(2) , mmap(2) , nice(2) , priocntl(2) , ptrace(2) , semop(2) , shmop(2) , times(2) , umask(2) , wait(2) , exit(3C) , plock(3C) , pthread_atfork(3T) , signal(3C) , system(3S) , thr_create(3T) , timer_create(3R)

Notes

Be careful to call _exit() rather than exit(3C) if you cannot execve(), since exit(3C) will flush and close standard I/O channels, and thereby corrupt the parent processes standard I/O data structures. Using exit(3C) will flush buffered data twice. See exit(2) .

When calling fork1() the thread (or LWP ) in the child must not depend on any resources that are held by threads (or LWP s) that no longer exist in the child. In particular, locks held by these threads (or LWP s) will not be released.

In a multi-threaded process, fork() or fork1() can cause blocking system calls to be interrupted and return with an error of EINTR .

fork() and fork1() suspend all threads in the process before proceeding. Threads which are executing in the kernel and are in an uninterruptible wait cannot be suspended immediately; and therefore, cause a delay before fork() and fork1() can complete. During this delay, all other threads will have already been suspended, and so the process will appear "hung."


Table of Contents