fork(2)


fork, forkall, fork1 -- create a new process

Synopsis

   #include <sys/types.h> 

#include <unistd.h>

pid_t fork(void);

pid_t fork1(void);

pid_t forkall(void);

Description

fork (also known as forkall) causes creation of a new process. The new process (child process) is an exact copy of the calling process (parent process). This means 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 new process created by forkall duplicates the calling process, including the set of threads (and underlying LWPs) that exist as of the time of the call.

The child process differs from the parent process in the following ways:

The fork1 system call will create a new process with a single, multiplexed (that is, not bound) thread. The new process has a single LWP. The parent process is not changed.

The fork1 system call should be used by multithreaded process that intend to have the new process call the exec system call. Since exec will terminate all but one thread, there is no need to duplicate all threads with forkall.

Return values

On success, fork returns 0 to the child process and returns the process ID of the child process to the parent process. On failure, fork returns a value of (pid_t)-1 to the parent process, sets errno to identify the error, and no child process is created.

Errors

In the following conditions, fork fails and sets errno to:

EAGAIN
The system limit on number of LWPs per real user-id would be exceeded if the call succeeds. and the calling process does not have the P_SYSOPS privilege. The system lacked the necessary resources to create another process.

EAGAIN
Total amount of system memory available when reading via raw I/O is temporarily insufficient.

EINTR
A forkall has been interrupted by a forkall executed by another thread in the process.

References

alarm(2), exec(2), fcntl(2), getrlimit(2), nice(2), plock(2), priocntl(2), ptrace(2), semop(2), shmop(2), signal(2), system(3S), times(2), umask(2), wait(2)

Notices

Considerations for threads programming

fork is synonymous with forkall.

Threads in the creating process are unaffected by these system calls except possibly for the error indication EINTR in the creating process.

It is expected that processes created via fork1 (only the calling thread replicated) will soon call exec(2) to redefine the address space. The single remaining thread should take care when requesting resources (for example, locks) that were duplicated from the parent. The fork1 system call does duplicate the lock (part of the address space) but not the thread that may have been holding that lock at the time of the system call. The surviving thread would block forever since there is no thread to release the resource.


30 January 1998
© 1998 The Santa Cruz Operation, Inc. All rights reserved.