]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" Copyright (c) 1999 Andries Brouwer (aeb@cwi.nl), 1 Nov 1999 |
2 | .\" | |
3 | .\" Permission is granted to make and distribute verbatim copies of this | |
4 | .\" manual provided the copyright notice and this permission notice are | |
5 | .\" preserved on all copies. | |
6 | .\" | |
7 | .\" Permission is granted to copy and distribute modified versions of this | |
8 | .\" manual under the conditions for verbatim copying, provided that the | |
9 | .\" entire resulting derived work is distributed under the terms of a | |
10 | .\" permission notice identical to this one. | |
11 | .\" | |
12 | .\" Since the Linux kernel and libraries are constantly changing, this | |
13 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
14 | .\" responsibility for errors or omissions, or for damages resulting from | |
15 | .\" the use of the information contained herein. The author(s) may not | |
16 | .\" have taken the same level of care in the production of this manual, | |
17 | .\" which is licensed free of charge, as they might when working | |
18 | .\" professionally. | |
19 | .\" | |
20 | .\" Formatted or processed versions of this manual, if unaccompanied by | |
21 | .\" the source, must acknowledge the copyright and authors of this work. | |
22 | .\" | |
23 | .\" 1999-11-10: Merged text taken from the page contributed by | |
24 | .\" Reed H. Petty (rhp@draper.net) | |
25 | .\" | |
26 | .TH VFORK 2 1999-11-01 "Linux 2.2.0" "Linux Programmer's Manual" | |
27 | .SH NAME | |
28 | vfork \- create a child process and block parent | |
29 | .SH SYNOPSIS | |
30 | .B #include <sys/types.h> | |
31 | .br | |
32 | .B #include <unistd.h> | |
33 | .sp | |
34 | .B pid_t vfork(void); | |
35 | .SH "STANDARD DESCRIPTION" | |
36 | (From XPG4 / SUSv2 / POSIX draft.) | |
37 | The | |
38 | .IR vfork () | |
39 | function has the same effect as | |
40 | .IR fork (), | |
41 | except that the behaviour is undefined if the process created by | |
42 | .IR vfork () | |
43 | either modifies any data other than a variable of type pid_t used | |
44 | to store the return value from | |
45 | .IR vfork (), | |
46 | or returns from the function in which | |
47 | .IR vfork () | |
48 | was called, or calls any other function before successfully calling | |
49 | .IR _exit () | |
50 | or one of the | |
51 | .I exec | |
52 | family of functions. | |
53 | .SH ERRORS | |
54 | .TP | |
55 | .B EAGAIN | |
56 | Too many processes - try again. | |
57 | .TP | |
58 | .B ENOMEM | |
59 | There is insufficient swap space for the new process. | |
60 | .SH "LINUX DESCRIPTION" | |
61 | .BR vfork , | |
62 | just like | |
63 | .BR fork (2), | |
64 | creates a child process of the calling process. | |
65 | For details and return value and errors, see | |
66 | .BR fork (2). | |
67 | .PP | |
68 | .B vfork() | |
69 | is a special case of | |
70 | .BR clone (2). | |
71 | It is used to create new processes without copying the page tables of | |
72 | the parent process. It may be useful in performance sensitive applications | |
73 | where a child will be created which then immediately issues an | |
74 | .IR execve() . | |
75 | .PP | |
76 | .B vfork() | |
77 | differs from fork in that the parent is suspended until the child makes | |
78 | a call to | |
79 | .BR execve (2) | |
80 | or | |
81 | .BR _exit (2). | |
82 | The child shares all memory with its parent, including the stack, until | |
83 | .I execve() | |
84 | is issued by the child. The child must not return from the | |
85 | current function or call | |
86 | .IR exit() , | |
87 | but may call | |
88 | .IR _exit() . | |
89 | .PP | |
90 | Signal handlers are inherited, but not shared. Signals to the parent | |
91 | arrive after the child releases the parent. | |
92 | .SH "HISTORIC DESCRIPTION" | |
93 | Under Linux, | |
94 | .IR fork () | |
95 | is implemented using copy-on-write pages, so the only penalty incurred by | |
96 | .IR fork () | |
97 | is the time and memory required to duplicate the parent's page tables, | |
98 | and to create a unique task structure for the child. | |
99 | However, in the bad old days a | |
100 | .IR fork() | |
101 | would require making a complete copy of the caller's data space, | |
102 | often needlessly, since usually immediately afterwards an | |
103 | .IR exec () | |
104 | is done. Thus, for greater efficiency, BSD introduced the | |
105 | .B vfork | |
106 | system call, that did not fully copy the address space of | |
107 | the parent process, but borrowed the parent's memory and thread | |
108 | of control until a call to | |
109 | .IR execve () | |
110 | or an exit occurred. The parent process was suspended while the | |
111 | child was using its resources. | |
112 | The use of vfork was tricky - for example, not modifying data | |
113 | in the parent process depended on knowing which variables are | |
114 | held in a register. | |
115 | .SH BUGS | |
116 | It is rather unfortunate that Linux revived this spectre from the past. | |
117 | The BSD manpage states: | |
118 | "This system call will be eliminated when proper system sharing mechanisms | |
119 | are implemented. Users should not depend on the memory sharing semantics of | |
120 | .I vfork | |
121 | as it will, in that case, be made synonymous to | |
122 | .IR fork .\c | |
123 | " | |
124 | ||
125 | Formally speaking, the standard description given above does not allow | |
126 | one to use | |
127 | .IR vfork () | |
128 | since a following | |
129 | .IR exec | |
130 | might fail, and then what happens is undefined. | |
131 | ||
132 | Details of the signal handling are obscure and differ between systems. | |
133 | The BSD manpage states: | |
134 | "To avoid a possible deadlock situation, processes that are children | |
135 | in the middle of a | |
136 | .I vfork | |
137 | are never sent SIGTTOU or SIGTTIN signals; rather, output or | |
138 | .IR ioctl s | |
139 | are allowed and input attempts result in an end-of-file indication." | |
140 | ||
141 | Currently (Linux 2.3.25), | |
142 | .BR strace (1) | |
143 | cannot follow | |
144 | .IR vfork() | |
145 | and requires a kernel patch. | |
146 | .SH HISTORY | |
147 | The | |
148 | .IR vfork () | |
149 | system call appeared in 3.0BSD. | |
150 | .\" In the release notes for BSD 4.2 Sam Leffler wrote: `vfork: Is still | |
151 | .\" present, but definitely on its way out'. | |
152 | In BSD 4.4 it was made synonymous to | |
153 | .IR fork (), | |
154 | but NetBSD introduced it again, | |
155 | cf. http://www.netbsd.org/Documentation/kernel/vfork.html . | |
156 | In Linux, it has been equivalent to | |
157 | .IR fork () | |
158 | until 2.2.0-pre6 or so. Since 2.2.0-pre9 (on i386, somewhat later on | |
159 | other architectures) it is an independent system call. Support was | |
160 | added in glibc 2.0.112. | |
161 | .SH "CONFORMING TO" | |
162 | The | |
163 | .B vfork | |
164 | call may be a bit similar to calls with the same name in other | |
165 | operating systems. The requirements put on | |
166 | .B vfork | |
167 | by the standards are weaker than those put on | |
168 | .BR fork , | |
169 | so an implementation where the two are synonymous | |
170 | is compliant. In particular, the programmer cannot | |
171 | rely on the parent remaining blocked until a call of | |
172 | .I execve() | |
173 | or | |
174 | .I _exit() | |
175 | and cannot rely on any specific behaviour w.r.t. shared memory. | |
176 | .\" In AIXv3.1 vfork is equivalent to fork. | |
177 | .SH "SEE ALSO" | |
178 | .BR clone (2), | |
179 | .BR execve (2), | |
180 | .BR fork (2), | |
181 | .BR wait (2) |