RFORK(2)
β¨ RFORK(2) β¨
okay bestie, this is the rfork(2) page from the System Calls Manual and it is literally the blueprint for how new processes get born on this system. buckle up. π¦
πΈ NAME πΈ
rfork β manipulate process resources π
π LIBRARY π
Standard C Library (libc, -lc)
π SYNOPSIS π
#include <unistd.h>
pid_t
rfork(int flags);
π DESCRIPTION π
so likeβ¦ forking, vforking, or rforking? those are the ONLY ways new processes get created, periodt. the flags argument you hand to rfork() is the vibe check that decides which resources of the invoking process (the parent) get shared with the new process (the child), or just get reset to their default values. π
and what counts as "resources"? we're talking the open file descriptor table β and yes, when that bad boy is shared, it lets processes open and close files for other processes π β plus the open files themselves.
the flags argument is either RFSPAWN or the logical OR of some subset of the following. not me giving you a whole menu π:
RFPROCβ set it and a brand new process gets created β¨. don't set it and the changes just affect the current process instead. no new baby, just a glow-up.RFNOWAITβ set it and the child gets dissociated from the parent π. when the child exits it will not leave a status for the parent to collect. go readwait(2)if you want the lore.RFFDGβ set it and the invoker's file descriptor table (peepintro(2)) gets copied π«Ά. otherwise the two processes share one single table. one table, two besties.RFCFDGβ set it and the new process starts with a clean file descriptor table π§Ό. this one is mutually exclusive withRFFDG, so pick a lane.RFTHREADβ set it and the new process shares the file descriptor to process leaders table with its parent π―. it only applies when neitherRFFDGnorRFCFDGare set.RFMEMβ set it and the kernel forces sharing of the entire address space, typically by sharing the hardware page table directly π₯. the child inherits and shares all the segments the parent process owns, whether they're normally shareable or not. the stack segment is not split (both parent and child return on the same stack) and sorfork()with theRFMEMflag may not generally be called directly from high level languages including C π. may be set only withRFPROC. a helper function exists to help with this whole situation and will cause the new process to run on the provided stack β seerfork_thread(3)for the tea. and like, heads up: a lot of code will not run correctly in such an environment. not me warning you twice πRFSIGSHAREβ set it and the kernel forces sharing thesigactsstructure between the child and the parent π«.RFTSIGZMBβ set it and the kernel delivers a specified signal to the parent upon the child exit, instead of the defaultSIGCHLDπ£. the signal number signum is specified by oring theRFTSIGFLAGS(signum)expression intoflags. specifying signal number0disables signal delivery upon the child exit.RFLINUXTHPNβ set it and the kernel deliversSIGUSR1instead ofSIGCHLDupon thread exit for the child π½. this is intended to mimic certain Linux clone behaviour. iykyk.
file descriptors in a shared file descriptor table stay open until either they're explicitly closed or all processes sharing the table exit. that's the rule, no cap. π
if RFSPAWN is passed, rfork will use vfork(2) semantics but reset all signal actions in the child to default β¨. this flag is what the posix_spawn(3) implementation in libc uses, so it's kind of a big deal.
if RFPROC is set, the value returned in the parent process is the process id of the child process; the value returned in the child is zero. without RFPROC, the return value is zero. process id's range from 1 to the maximum integer (int) value π’. the rfork() system call will sleep, if necessary, until required process resources are available. nap time, bestie π΄.
and plot twist β the fork() system call can be implemented as a call to rfork(RFFDG | RFPROC) but is not, for backwards compatibility. iconic behaviour honestly.
π RETURN VALUES π
upon successful completion, rfork() 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 -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 rfork() system call will fail and no child process will be created if:
[EAGAIN]β the system-imposed limit on the total number of processes under execution would be exceeded π. the limit is given by thesysctl(3)MIB variableKERN_MAXPROC. (the limit is actually ten less than this except for the super user, fun little detail fr fr).[EAGAIN]β the user is not the super user, and the system-imposed limit on the total number of processes under execution by a single user would be exceeded. the limit is given by thesysctl(3)MIB variableKERN_MAXPROCPERUID.[EAGAIN]β the user is not the super user, and the soft resource limit corresponding to the resource argumentRLIMIT_NOFILEwould be exceeded (peepgetrlimit(2)).[EINVAL]β both theRFFDGand theRFCFDGflags were specified. told you they were mutually exclusive π«.[EINVAL]β any flags not listed above were specified. no inventing flags, bestie π.[EINVAL]β an invalid signal number was specified.[ENOMEM]β there is insufficient swap space for the new process. we are out of room π.
π SEE ALSO π
fork(2), intro(2), minherit(2), vfork(2), pthread_create(3), rfork_thread(3)
π¦ HISTORY π¦
the rfork() function first appeared in Plan9. we love an origin story π.
and that's the whole page, bestie!! π this one came from the Debian folks and it's dated September 25, 2019 β so like, a little vintage but still absolutely serving. go make some processes, you've got this π«Άβ¨π
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms