1VFORK(2) Linux Programmer's Manual VFORK(2)
2
3
4
6 vfork - create a child process and block parent
7
9 #include <sys/types.h>
10 #include <unistd.h>
11
12 pid_t vfork(void);
13
14 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
15
16 vfork():
17 Since glibc 2.12:
18 _BSD_SOURCE ||
19 (_XOPEN_SOURCE >= 500 ||
20 _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED) &&
21 !(_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700)
22 Before glibc 2.12:
23 _BSD_SOURCE || _XOPEN_SOURCE >= 500 ||
24 _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
25
27 Standard description
28 (From POSIX.1) The vfork() function has the same effect as fork(2),
29 except that the behavior is undefined if the process created by vfork()
30 either modifies any data other than a variable of type pid_t used to
31 store the return value from vfork(), or returns from the function in
32 which vfork() was called, or calls any other function before success‐
33 fully calling _exit(2) or one of the exec(3) family of functions.
34
35 Linux description
36 vfork(), just like fork(2), creates a child process of the calling
37 process. For details and return value and errors, see fork(2).
38
39 vfork() is a special case of clone(2). It is used to create new pro‐
40 cesses without copying the page tables of the parent process. It may
41 be useful in performance-sensitive applications where a child is cre‐
42 ated which then immediately issues an execve(2).
43
44 vfork() differs from fork(2) in that the calling thread is suspended
45 until the child terminates (either normally, by calling _exit(2), or
46 abnormally, after delivery of a fatal signal), or it makes a call to
47 execve(2). Until that point, the child shares all memory with its par‐
48 ent, including the stack. The child must not return from the current
49 function or call exit(3), but may call _exit(2).
50
51 As with fork(2), the child process created by vfork() inherits copies
52 of various of the caller's process attributes (e.g., file descriptors,
53 signal dispositions, and current working directory); the vfork() call
54 differs only in the treatment of the virtual address space, as
55 described above.
56
57 Signals sent to the parent arrive after the child releases the parent's
58 memory (i.e., after the child terminates or calls execve(2)).
59
60 Historic description
61 Under Linux, fork(2) is implemented using copy-on-write pages, so the
62 only penalty incurred by fork(2) is the time and memory required to
63 duplicate the parent's page tables, and to create a unique task struc‐
64 ture for the child. However, in the bad old days a fork(2) would
65 require making a complete copy of the caller's data space, often need‐
66 lessly, since usually immediately afterward an exec(3) is done. Thus,
67 for greater efficiency, BSD introduced the vfork() system call, which
68 did not fully copy the address space of the parent process, but bor‐
69 rowed the parent's memory and thread of control until a call to
70 execve(2) or an exit occurred. The parent process was suspended while
71 the child was using its resources. The use of vfork() was tricky: for
72 example, not modifying data in the parent process depended on knowing
73 which variables were held in a register.
74
76 4.3BSD; POSIX.1-2001 (but marked OBSOLETE). POSIX.1-2008 removes the
77 specification of vfork().
78
79 The requirements put on vfork() by the standards are weaker than those
80 put on fork(2), so an implementation where the two are synonymous is
81 compliant. In particular, the programmer cannot rely on the parent
82 remaining blocked until the child either terminates or calls execve(2),
83 and cannot rely on any specific behavior with respect to shared memory.
84
86 Some consider the semantics of vfork() to be an architectural blemish,
87 and the 4.2BSD man page stated: "This system call will be eliminated
88 when proper system sharing mechanisms are implemented. Users should
89 not depend on the memory sharing semantics of vfork() as it will, in
90 that case, be made synonymous to fork(2)." However, even though modern
91 memory management hardware has decreased the performance difference
92 between fork(2) and vfork(), there are various reasons why Linux and
93 other systems have retained vfork():
94
95 * Some performance-critical applications require the small performance
96 advantage conferred by vfork().
97
98 * vfork() can be implemented on systems that lack a memory-management
99 unit (MMU), but fork(2) can't be implemented on such systems.
100 (POSIX.1-2008 removed vfork() from the standard; the POSIX rationale
101 for the posix_spawn(3) function notes that that function, which pro‐
102 vides functionality equivalent to fork(2)+exec(3), is designed to be
103 implementable on systems that lack an MMU.)
104
105 Linux notes
106 Fork handlers established using pthread_atfork(3) are not called when a
107 multithreaded program employing the NPTL threading library calls
108 vfork(). Fork handlers are called in this case in a program using the
109 LinuxThreads threading library. (See pthreads(7) for a description of
110 Linux threading libraries.)
111
112 A call to vfork() is equivalent to calling clone(2) with flags speci‐
113 fied as:
114
115 CLONE_VM | CLONE_VFORK | SIGCHLD
116
117 History
118 The vfork() system call appeared in 3.0BSD. In 4.4BSD it was made syn‐
119 onymous to fork(2) but NetBSD introduced it again, cf.
120 ⟨http://www.netbsd.org/Documentation/kernel/vfork.html⟩. In Linux, it
121 has been equivalent to fork(2) until 2.2.0-pre6 or so. Since
122 2.2.0-pre9 (on i386, somewhat later on other architectures) it is an
123 independent system call. Support was added in glibc 2.0.112.
124
126 Details of the signal handling are obscure and differ between systems.
127 The BSD man page states: "To avoid a possible deadlock situation, pro‐
128 cesses that are children in the middle of a vfork() are never sent
129 SIGTTOU or SIGTTIN signals; rather, output or ioctls are allowed and
130 input attempts result in an end-of-file indication."
131
133 clone(2), execve(2), fork(2), unshare(2), wait(2)
134
136 This page is part of release 3.53 of the Linux man-pages project. A
137 description of the project, and information about reporting bugs, can
138 be found at http://www.kernel.org/doc/man-pages/.
139
140
141
142Linux 2012-08-05 VFORK(2)