1VFORK(2) Linux Programmer's Manual VFORK(2)
2
3
4
6 vfork - create a child process and block parent
7
9 #include <unistd.h>
10
11 pid_t vfork(void);
12
13 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
14
15 vfork():
16 Since glibc 2.12:
17 (_XOPEN_SOURCE >= 500) && ! (_POSIX_C_SOURCE >= 200809L)
18 || /* Since glibc 2.19: */ _DEFAULT_SOURCE
19 || /* Glibc <= 2.19: */ _BSD_SOURCE
20 Before glibc 2.12:
21 _BSD_SOURCE || _XOPEN_SOURCE >= 500
22
24 Standard description
25 (From POSIX.1) The vfork() function has the same effect as fork(2), ex‐
26 cept that the behavior is undefined if the process created by vfork()
27 either modifies any data other than a variable of type pid_t used to
28 store the return value from vfork(), or returns from the function in
29 which vfork() was called, or calls any other function before success‐
30 fully calling _exit(2) or one of the exec(3) family of functions.
31
32 Linux description
33 vfork(), just like fork(2), creates a child process of the calling
34 process. For details and return value and errors, see fork(2).
35
36 vfork() is a special case of clone(2). It is used to create new pro‐
37 cesses without copying the page tables of the parent process. It may
38 be useful in performance-sensitive applications where a child is cre‐
39 ated which then immediately issues an execve(2).
40
41 vfork() differs from fork(2) in that the calling thread is suspended
42 until the child terminates (either normally, by calling _exit(2), or
43 abnormally, after delivery of a fatal signal), or it makes a call to
44 execve(2). Until that point, the child shares all memory with its par‐
45 ent, including the stack. The child must not return from the current
46 function or call exit(3) (which would have the effect of calling exit
47 handlers established by the parent process and flushing the parent's
48 stdio(3) buffers), but may call _exit(2).
49
50 As with fork(2), the child process created by vfork() inherits copies
51 of various of the caller's process attributes (e.g., file descriptors,
52 signal dispositions, and current working directory); the vfork() call
53 differs only in the treatment of the virtual address space, as de‐
54 scribed above.
55
56 Signals sent to the parent arrive after the child releases the parent's
57 memory (i.e., after the child terminates or calls execve(2)).
58
59 Historic description
60 Under Linux, fork(2) is implemented using copy-on-write pages, so the
61 only penalty incurred by fork(2) is the time and memory required to du‐
62 plicate the parent's page tables, and to create a unique task structure
63 for the child. However, in the bad old days a fork(2) would require
64 making