vfork(2)

โœจ vfork โœจ

okay bestie, welcome to the vfork(2) page ๐Ÿ’– it's a system call, it lives in the System Calls Manual, and it's honestly giving "the weird cousin of fork(2) that everyone has opinions about". buckle up ๐ŸŽ€

๐ŸŒธ NAME ๐ŸŒธ

vfork โ€” create a child process and block parent

๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc) โ€” like, the normal one, no special linker flags needed ๐ŸŒˆ

๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

#include <unistd.h>

pid_t vfork(void);

๐Ÿงš Feature Test Macro Requirements for glibc (see feature_test_macros(7)): ๐Ÿงš

vfork():
    Since glibc 2.12:
        (_XOPEN_SOURCE >= 500) && ! (_POSIX_C_SOURCE >= 200809L)
            || /* Since glibc 2.19: */ _DEFAULT_SOURCE
            || /* glibc <= 2.19: */ _BSD_SOURCE
    Before glibc 2.12:
        _BSD_SOURCE || _XOPEN_SOURCE >= 500

yeah it's a whole conditional vibe check, but it's all right there โญ

๐Ÿ’œ DESCRIPTION ๐Ÿ’œ

๐ŸŽ€ Standard description ๐ŸŽ€

(from POSIX.1, the actual official tea โ˜•) The vfork() function has the same effect as fork(2), EXCEPT the behavior is undefined if the process created by vfork() does any of these things:

  • modifies any data other than a variable of type pid_t used to store the return value from vfork()
  • returns from the function in which vfork() was called
  • calls any other function before successfully calling _exit(3) or one of the exec(3) family of functions
undefined behavior alert ๐Ÿšจ like, do NOT do these things, this is not a "oopsie" situation, this is an "anything can happen" situation ๐Ÿ’€

๐ŸŒˆ Linux description ๐ŸŒˆ

vfork(), just like fork(2), creates a child process of the calling process. For details and return value and errors, go look at fork(2) โ€” it's all the same tea there ๐Ÿซ–

vfork() is a special case of clone(2). It's used to create new processes without copying the page tables of the parent process. It may be useful in performance-sensitive applications where a child is created which then immediately issues an execve(2). it's giving speedrun energy ๐Ÿ”ฅ

vfork() differs from fork(2) in that the calling thread is suspended until the child terminates (either normally, by calling _exit(3), or abnormally, after delivery of a fatal signal), or it makes a call to execve(2). Until that point, the child shares all memory with its parent, including the stack. The child must not return from the current function or call exit(3) (which would have the effect of calling exit handlers established by the parent process and flushing the parent's stdio(3) buffers), but may call _exit(3). literally the one job: _exit(3) or exec, nothing else ๐Ÿ˜ญ

As with fork(2), the child process created by vfork() inherits copies of various of the caller's process attributes (e.g., file descriptors, signal dispositions, and current working directory); the vfork() call differs only in the treatment of the virtual address space, as described above. everything else? same, bestie ๐Ÿ‘‘

Signals sent to the parent arrive after the child releases the parent's memory (i.e., after the child terminates or calls execve(2)). so no queue-jumping with signals ๐Ÿšฆ

๐Ÿฆ„ Historic description ๐Ÿฆ„

Under Linux, fork(2) is implemented using copy-on-write pages, so the only penalty incurred by fork(2) is the time and memory required to duplicate the parent's page tables, and to create a unique task structure for the child. But in the bad old days ๐Ÿ˜ญ a fork(2) would require making a complete copy of the caller's data space, often needlessly, since usually immediately afterward an exec(3) is done. So, for greater efficiency, BSD introduced the vfork() system call, which did not fully copy the address space of the parent process, but borrowed the parent's memory and thread of control until a call to execve(2) or an exit occurred. The parent process was suspended while the child was using its resources. The use of vfork() was tricky: for example, not modifying data in the parent process depended on knowing which variables were held in a register. like, actual register-level detective work, the audacity ๐Ÿ’…

๐Ÿ”ฎ VERSIONS ๐Ÿ”ฎ

The requirements put on vfork() by the standards are weaker than those put on fork(2), so an implementation where the two are synonymous is compliant (read: legally, vfork can just be fork and nobody can be mad ๐Ÿซ ). In particular, the programmer cannot rely on the parent remaining blocked until the child either terminates or calls execve(2), and cannot rely on any specific behavior with respect to shared memory. so, like, don't build your entire architecture on it, okay? ๐Ÿ™ˆ

Some people consider the semantics of vfork() to be an architectural blemish, and the 4.2BSD man page straight-up said:

โ€œThis system call will be eliminated when proper system sharing mechanisms are implemented. Users should not depend on the memory sharing semantics of vfork as it will, in that case, be made synonymous to fork.โ€

the way they were SO dramatic in 1980s manpages ๐Ÿ’€ iconic honestly.

But even though modern memory management hardware has decreased the performance difference between fork(2) and vfork(), there are various reasons why Linux and other systems have retained vfork():

  • Some performance-critical applications require the small performance advantage conferred by vfork().
  • vfork() can be implemented on systems that lack a memory-management unit (MMU), but fork(2) can't be implemented on such systems. (POSIX.1-2008 removed vfork() from the standard; the POSIX rationale for the posix_spawn(3) function notes that that function, which provides functionality equivalent to fork(2)+exec(3), is designed to be implementable on systems that lack an MMU.) ๐Ÿง โœจ
  • On systems where memory is constrained, vfork() avoids the need to temporarily commit memory (see the description of /proc/sys/vm/overcommit_memory in proc(5)) in order to execute a new program. (This can be especially beneficial where a large parent process wishes to execute a small helper program in a child process.) By contrast, using fork(2) in this scenario requires either committing an amount of memory equal to the size of the parent process (if strict overcommitting is in force) or overcommitting memory with the risk that a process is terminated by the out-of-memory (OOM) killer. OOM killer cameo ๐Ÿ˜ฑ

๐Ÿง Linux notes ๐Ÿง

Fork handlers established using pthread_atfork(3) are not called when a multithreaded program employing the NPTL threading library calls vfork(). Fork handlers are called in this case in a program using the LinuxThreads threading library. (See pthreads(7) for a description of Linux threading libraries.) iykyk ๐Ÿงต

A call to vfork() is equivalent to calling clone(2) with flags specified as:

CLONE_VM | CLONE_VFORK | SIGCHLD

๐Ÿ“œ STANDARDS ๐Ÿ“œ

None. ๐Ÿ˜ถ periodt. no standard claimed this child.

๐Ÿ•ฐ๏ธ HISTORY ๐Ÿ•ฐ๏ธ

4.3BSD; POSIX.1-2001 (but marked OBSOLETE). POSIX.1-2008 removes the specification of vfork().

The vfork() system call appeared in 3.0BSD. In 4.4BSD it was made synonymous to fork(2) but NetBSD introduced it again; see http://www.netbsd.org/Documentation/kernel/vfork.html. In Linux, it has been equivalent to fork(2) until Linux 2.2.0-pre6 or so. Since Linux 2.2.0-pre9 (on i386, somewhat later on other architectures) it is an independent system call. Support was added in glibc 2.0.112. the lore is deep bestie ๐Ÿ“–

โš ๏ธ CAVEATS โš ๏ธ

The child process should take care not to modify the memory in unintended ways, since such changes will be seen by the parent process once the child terminates or executes another program. In this regard, signal handlers can be especially problematic: if a signal handler that is invoked in the child of vfork() changes memory, those changes may result in an inconsistent process state from the perspective of the parent process (e.g., memory changes would be visible in the parent, but changes to the state of open file descriptors would not be visible). so like, memory yes, file descriptors no, and that mismatch is the whole problem ๐Ÿ˜ต๐Ÿ’ซ

When vfork() is called in a multithreaded process, only the calling thread is suspended until the child terminates or executes a new program. This means that the child is sharing an address space with other running code. This can be dangerous if another thread in the parent process changes credentials (using setuid(2) or similar), since there are now two processes with different privilege levels running in the same address space. As an example of the dangers, suppose that a multithreaded program running as root creates a child using vfork(). After the vfork(), a thread in the parent process drops the process to an unprivileged user in order to run some untrusted code (e.g., perhaps via plug-in opened with dlopen(3)). In this case, attacks are possible where the parent process uses mmap(2) to map in code that will be executed by the privileged child process. ๐Ÿšจ๐Ÿ”“ that's not a vibe, that's a CVE bestie, please be careful

๐Ÿ› BUGS ๐Ÿ›

Details of the signal handling are obscure and differ between systems. The BSD man page states:

"To avoid a possible deadlock situation, processes that are children in the middle of a vfork() are never sent SIGTTOU or SIGTTIN signals; rather, output or ioctls are allowed and input attempts result in an end-of-file indication."

translation: deadlock avoidance said "no SIGTTOU, no SIGTTIN, you get EOF and you'll LIKE it" ๐Ÿ’€

๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

clone(2), execve(2), fork(2), _Fork(3), unshare(2), wait(2), _exit(3)


and that's the whole page bestie!! ๐ŸŽ‰ this was Linux man-pages version 6.20, dated 2026-06-05, and it lives in section 2 (vfork(2)) of the System Calls Manual. go forth and vfork() responsibly, which honestly means: mostly just use fork(2) or posix_spawn(3) ๐Ÿ˜Œ๐Ÿ’– mwah ๐Ÿซถโœจ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms