OpenBSD manual page server

Manual Page Search Parameters

RFORK(2) System Calls Manual RFORK(2)

rforkcontrol new processes

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

int
rfork(int flags);

The fork functions (fork(2), vfork(2), and ()) create new processes. The new process (child process) is an exact copy of the calling process (parent process), except as outlined in the fork(2) manual page. rfork() is used to manipulate the resources of the parent process and the child process.

Operations currently supported include whether to copy or share the file descriptor table between the two processes, whether to share the address space, and whether the parent should wait(2) for the child process to _exit(2). () takes a single argument, flags, which controls which of these resources should be manipulated. They are defined in the header file ⟨sys/param.h⟩ and are the logical OR of one or more of the following:

Copy the parent's file descriptor table. If this flag is unset, the parent and child will share the parent's file descriptor table. Descriptors will remain in existence until they are closed by all child processes using the table copies as well as by the parent process. May not be used in conjunction with RFCFDG.
Create a new process. The current implementation requires this flag to always be set.
Force sharing of the entire address space between the parent and child processes. The child will then inherit all the shared segments the parent process owns. Subsequent forks by the parent will then propagate the shared data and BSS segments among children.
Child processes will have their resources reaped immediately and implicitly when they terminate instead of turning into zombies, so the parent process may not call wait(2) to collect their exit statuses and have their resources released explicitly.
Zero the child's file descriptor table (i.e. start with a blank file descriptor table). May not be used in conjunction with RFFDG.
Create a kernel thread in the current process instead of a separate process. Must be combined with RFMEM. Automatically enables RFNOWAIT. The kern.rthreads sysctl must be enabled for this to succeed.

fork(2) can be implemented as a call to () using "RFFDG|RFPROC", but isn't for backwards compatibility. If a process has file descriptor table sharing active, setuid or setgid programs will not execve(2) with extra privileges.

The parent process returns the process ID (PID) of the child process. The child process returns 0. The range of the process ID is defined in ⟨sys/proc.h⟩ and is currently between 1 and 32766, inclusive.

rfork() will fail and no child process will be created if:

[]
Cannot allocate memory. The new process image required more memory than was allowed by the hardware or by system-imposed memory management constraints. A lack of swap space is normally temporary; however, a lack of core is not. Soft limits may be increased to their corresponding hard limits.
[]
Invalid argument. Some invalid argument was supplied.
[]
Resource temporarily unavailable. The system-imposed limit on the total number of processes under execution would be exceeded. This limit is configuration-dependent.
[]
Resource temporarily unavailable. The system-imposed limit MAXUPRC on the total number of processes under execution by a single user would be exceeded. MAXUPRC is currently defined in ⟨sys/param.h⟩ as CHILD_MAX, which is currently defined as 80 in ⟨sys/syslimits.h⟩.
[]
The RFTHREAD flag was set but the kern.rthreads sysctl was not enabled.

_exit(2), execve(2), fork(2), intro(2), vfork(2)

The rfork() function first appeared in Plan 9.

RFTHREAD cannot be used from C, as the two threads would return on the same stack.

June 29, 2010 OpenBSD-5.1