FORK(2) | System Calls | FORK(2) |
fork, fork1, forkall, forkx, forkallx - create a new process
#include <sys/types.h> #include <unistd.h> pid_t fork(void);
pid_t fork1(void);
pid_t forkall(void);
#include <sys/fork.h> pid_t forkx(int flags);
pid_t forkallx(int flags);
The fork(), fork1(), forkall(), forkx(), and forkallx() functions create a new process. The address space of the new process (child process) is an exact copy of the address space 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 might or might not be inherited according to the policy of that particular class (see priocntl(2)). The child process might or might not be in the same process contract as the parent (see process(5)). 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)).
Although any open door descriptors in the parent are shared by the child, only the parent will receive a door invocation from clients even if the door descriptor is open in the child. If a descriptor is closed in the parent, attempts to operate on the door descriptor will fail even if it is still open in the child.
A call to forkall() or forkallx() replicates in the child process all of the threads (see thr_create(3C) and pthread_create(3C)) in the parent process. A call to fork1() or forkx() replicates only the calling thread in the child process.
A call to fork() is identical to a call to fork1(); only the calling thread is replicated in the child process. This is the POSIX-specified behavior for fork().
In releases of Solaris prior to Solaris 10, the behavior of fork() depended on whether or not the application was linked with the POSIX threads library. When linked with -lthread (Solaris Threads) but not linked with -lpthread (POSIX Threads), fork() was the same as forkall(). When linked with -lpthread, whether or not also linked with -lthread, fork() was the same as fork1().
Prior to Solaris 10, either -lthread or -lpthread was required for multithreaded applications. This is no longer the case. The standard C library provides all threading support for both sets of application programming interfaces. Applications that require replicate-all fork semantics must call forkall() or forkallx().
The forkx() and forkallx() functions accept a flags argument consisting of a bitwise inclusive-OR of zero or more of the following flags, which are defined in the header <sys/fork.h>:
FORK_NOSIGCHLD
FORK_WAITPID
If the flags argument is 0 forkx() is identical to fork() and forkallx() is identical to forkall().
If a multithreaded application calls fork(), fork1(), or forkx(), and the child does more than simply call one of the exec(2) functions, there is a possibility of deadlock occurring in the child. The application should use pthread_atfork(3C) to ensure safety with respect to this deadlock. Should there be any outstanding mutexes throughout the process, the application should call pthread_atfork() to wait for and acquire those mutexes prior to calling fork(), fork1(), or forkx(). See "MT-Level" on the attributes(7) manual page.
The pthread_atfork() mechanism is used to protect the locks that libc(3LIB) uses to implement interfaces such as malloc(3C). All interfaces provided by libc are safe to use in a child process following a fork(), except when fork() is executed within a signal handler.
The POSIX standard (see standards(7)) requires fork to be Async-Signal-Safe (see attributes(7)). This cannot be made to happen with fork handlers in place, because they acquire locks. To be in nominal compliance, no fork handlers are called when fork() is executed within a signal context. This leaves the child process in a questionable state with respect to its locks, but at least the calling thread will not deadlock itself attempting to acquire a lock that it already owns. In this situation, the application should strictly adhere to the advice given in the POSIX specification: "To avoid errors, the child process may only execute Async-Signal-Safe operations until such time as one of the exec(2) functions is called."
Upon successful completion, fork(), fork1(), forkall(), forkx(), and forkallx() return 0 to the child process and return the process ID of the child process to the parent process. Otherwise, (pid_t)−1 is returned to the parent process, no child process is created, and errno is set to indicate the error.
The fork(), fork1(), forkall(), forkx(), and forkallx() functions will fail if:
EAGAIN
ENOMEM
EPERM
The forkx() and forkallx() functions will fail if:
EINVAL
See attributes(7) for descriptions of the following attributes:
ATTRIBUTE TYPE | ATTRIBUTE VALUE |
Interface Stability | Committed |
MT-Level | Async-Signal-Safe. |
Standard | See below. |
For fork(), see standards(7).
alarm(2), exec(2), exit(2), fcntl(2), getitimer(2), getrlimit(2), memcntl(2), mmap(2), nice(2), priocntl(2), semop(2), shmop(2), times(2), umask(2), waitid(2), door_create(3C), exit(3C), plock(3C), pthread_atfork(3C), pthread_create(3C), signal(3C), system(3C), thr_create(3C) timer_create(3C), wait(3C), contract(5), process(5), attributes(7), privileges(7), standards(7)
An application should call _exit() rather than exit(3C) if it cannot execve(), since exit() will flush and close standard I/O channels and thereby corrupt the parent process's standard I/O data structures. Using exit(3C) will flush buffered data twice. See exit(2).
The thread in the child that calls fork(), fork1(), or fork1x() must not depend on any resources held by threads that no longer exist in the child. In particular, locks held by these threads will not be released.
In a multithreaded process, forkall() in one thread can cause blocking system calls to be interrupted and return with an EINTR error.
August 18, 2019 | OmniOS |