VFORK(2)
π VFORK(2) π
okay bestie, sit down, hydrate, because we're talking about vfork β it's a system call, it's in the System Calls Manual, and it is lowkey a menace. β¨
πΈ NAME πΈ
vfork β create a new process without copying the address space. no copy, no drama, just vibes (and danger, but we'll get there π).
π LIBRARY π
Standard C Library (libc, -lc). she lives there. π«Ά
β¨ SYNOPSIS β¨
#include <unistd.h>
pid_t
vfork(void);
π DESCRIPTION π
so like, real talk first: this function is hard to use correctly from application software, so the recommendation is literally to use posix_spawn(3) or fork(2) instead. that's not me being shady, that's the manpage being shady. both of us. π
The vfork() system call can be used to create new processes without fully copying the address space of the old process, which is inefficient in a paged environment. it's giving "why are we duplicating all this memory when we're just gonna throw it away" energy. ποΈβ¨
It's useful when the purpose of fork(2) would have been to create a new system context for an execve(2). like, if you were only forking so you could immediately exec β that's the moment.
The vfork() system call differs from fork(2) in that the child borrows the parent process's address space and the calling thread's stack until a call to execve(2) or an exit (either by a call to _exit(2) or abnormally). borrows. not copies. BORROWS. π
The calling thread is suspended while the child is using its resources. Other threads continue to run. (important, keep that.)
The vfork() system call returns 0 in the child's context and (later) the pid of the child in the parent's context.
β οΈ Many problems can occur when replacingfork(2)withvfork(). for example, it does not work to return while running in the child's context from the procedure that calledvfork()since the eventual return fromvfork()would then return to a no longer existent stack frame. the stack is GONE bestie. it's not there. π
Also, changing process state which is partially implemented in user space β such as signal handlers with libthr(3) β will corrupt the parent's state. no cap. do not do this.
Be careful, also, to call _exit(2) rather than exit(3) if you cannot execve(2), since exit(3) will flush and close standard I/O channels, and thereby mess up the parent processes standard I/O data structures. π
(Even with fork(2) it is wrong to call exit(3) since buffered data would then be flushed twice.) so like... this is a general life lesson, not just a vfork thing.
π RETURN VALUES π
Same as for fork(2). that's it. that's the whole section. iconic minimalism. π
π₯ SEE ALSO π₯
go read those. the group chat has links. π¦
β HISTORY β
The vfork() system call appeared in 3BSD. ancient. vintage. a true elder. π
π BUGS π
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 ioctl(2) calls are allowed and input attempts result in an end-of-file indication.
like... she just doesn't get the signals. it's not personal, it's protocol. π«Ά
so yeah bestie, use posix_spawn(3) or fork(2), call _exit(2), and never, ever return from the child. mwah. π
this has been the Debian edition of VFORK(2) from May 22, 2016 β periodt. πβ¨π
π¦ 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
- Copyright (c) 1980, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms