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(): _BSD_SOURCE || _XOPEN_SOURCE >= 500
17
19 Standard Description
20 (From POSIX.1) The vfork() function has the same effect as fork(2),
21 except that the behavior is undefined if the process created by vfork()
22 either modifies any data other than a variable of type pid_t used to
23 store the return value from vfork(), or returns from the function in
24 which vfork() was called, or calls any other function before success‐
25 fully calling _exit(2) or one of the exec(3) family of functions.
26
27 Linux Description
28 vfork(), just like fork(2), creates a child process of the calling
29 process. For details and return value and errors, see fork(2).
30
31 vfork() is a special case of clone(2). It is used to create new pro‐
32 cesses without copying the page tables of the parent process. It may
33 be useful in performance-sensitive applications where a child is cre‐
34 ated which then immediately issues an execve(2).
35
36 vfork() differs from fork(2) in that the parent is suspended until the
37 child terminates (either normally, by calling _exit(2), or abnormally,
38 after delivery of a fatal signal), or it makes a call to execve(2).
39 Until that point, the child shares all memory with its parent, includ‐
40 ing the stack. The child must not return from the current function or
41 call exit(3), but may call _exit(2).
42
43 Signal handlers are inherited, but not shared. Signals to the parent
44 arrive after the child releases the parent's memory (i.e., after the
45 child terminates or calls execve(2)).
46
47 Historic Description
48 Under Linux, fork(2) is implemented using copy-on-write pages, so the
49 only penalty incurred by fork(2) is the time and memory required to
50 duplicate the parent's page tables, and to create a unique task struc‐
51 ture for the child. However, in the bad old days a fork(2) would
52 require making a complete copy of the caller's data space, often need‐
53 lessly, since usually immediately afterwards an exec(3) is done. Thus,
54 for greater efficiency, BSD introduced the vfork() system call, which
55 did not fully copy the address space of the parent process, but bor‐
56 rowed the parent's memory and thread of control until a call to
57 execve(2) or an exit occurred. The parent process was suspended while
58 the child was using its resources. The use of vfork() was tricky: for
59 example, not modifying data in the parent process depended on knowing
60 which variables were held in a register.
61
63 4.3BSD, POSIX.1-2001. POSIX.1-2008 removes the specification of
64 vfork(). The requirements put on vfork() by the standards are weaker
65 than those put on fork(2), so an implementation where the two are syn‐
66 onymous is compliant. In particular, the programmer cannot rely on the
67 parent remaining blocked until the child either terminates or calls
68 execve(2), and cannot rely on any specific behavior with respect to
69 shared memory.
70
72 Linux Notes
73 Fork handlers established using pthread_atfork(3) are not called when a
74 multithreaded program employing the NPTL threading library calls
75 vfork(). Fork handlers are called in this case in a program using the
76 LinuxThreads threading library. (See pthreads(7) for a description of
77 Linux threading libraries.)
78
79 History
80 The vfork() system call appeared in 3.0BSD. In 4.4BSD it was made syn‐
81 onymous to fork(2) but NetBSD introduced it again, cf. http://www.net‐
82 bsd.org/Documentation/kernel/vfork.html . In Linux, it has been equiv‐
83 alent to fork(2) until 2.2.0-pre6 or so. Since 2.2.0-pre9 (on i386,
84 somewhat later on other architectures) it is an independent system
85 call. Support was added in glibc 2.0.112.
86
88 It is rather unfortunate that Linux revived this specter from the past.
89 The BSD man page states: "This system call will be eliminated when
90 proper system sharing mechanisms are implemented. Users should not
91 depend on the memory sharing semantics of vfork() as it will, in that
92 case, be made synonymous to fork(2)."
93
94 Details of the signal handling are obscure and differ between systems.
95 The BSD man page states: "To avoid a possible deadlock situation, pro‐
96 cesses that are children in the middle of a vfork() are never sent
97 SIGTTOU or SIGTTIN signals; rather, output or ioctls are allowed and
98 input attempts result in an end-of-file indication."
99
101 clone(2), execve(2), fork(2), unshare(2), wait(2)
102
104 This page is part of release 3.25 of the Linux man-pages project. A
105 description of the project, and information about reporting bugs, can
106 be found at http://www.kernel.org/doc/man-pages/.
107
108
109
110Linux 2009-06-21 VFORK(2)