]>
Commit | Line | Data |
---|---|---|
9bccf70c A |
1 | .\" Copyright (c) 1980, 1991, 1993 |
2 | .\" The Regents of the University of California. All rights reserved. | |
3 | .\" | |
4 | .\" Redistribution and use in source and binary forms, with or without | |
5 | .\" modification, are permitted provided that the following conditions | |
6 | .\" are met: | |
7 | .\" 1. Redistributions of source code must retain the above copyright | |
8 | .\" notice, this list of conditions and the following disclaimer. | |
9 | .\" 2. Redistributions in binary form must reproduce the above copyright | |
10 | .\" notice, this list of conditions and the following disclaimer in the | |
11 | .\" documentation and/or other materials provided with the distribution. | |
cb323159 | 12 | .\" 3. Neither the name of the University nor the names of its contributors |
9bccf70c A |
13 | .\" may be used to endorse or promote products derived from this software |
14 | .\" without specific prior written permission. | |
15 | .\" | |
16 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
17 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
20 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
21 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
22 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
23 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
24 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
25 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
26 | .\" SUCH DAMAGE. | |
27 | .\" | |
28 | .\" @(#)vfork.2 8.1 (Berkeley) 6/4/93 | |
cb323159 | 29 | .\" $FreeBSD$ |
9bccf70c | 30 | .\" |
cb323159 | 31 | .Dd May 22, 2016 |
9bccf70c | 32 | .Dt VFORK 2 |
cb323159 | 33 | .Os |
9bccf70c A |
34 | .Sh NAME |
35 | .Nm vfork | |
cb323159 A |
36 | .Nd create a new process without copying the address space |
37 | .Sh LIBRARY | |
38 | .Lb libc | |
9bccf70c | 39 | .Sh SYNOPSIS |
cb323159 | 40 | .In unistd.h |
9bccf70c | 41 | .Ft pid_t |
cb323159 | 42 | .Fn vfork void |
9bccf70c | 43 | .Sh DESCRIPTION |
cb323159 A |
44 | .Bf -symbolic |
45 | Since this function is hard to use correctly from application software, | |
46 | it is recommended to use | |
47 | .Xr posix_spawn 3 | |
48 | or | |
49 | .Xr fork 2 | |
50 | instead. | |
51 | .Ef | |
52 | .Pp | |
53 | The | |
39037602 | 54 | .Fn vfork |
cb323159 | 55 | system call |
9bccf70c | 56 | can be used to create new processes without fully copying the address |
cb323159 A |
57 | space of the old process, which is inefficient in a paged |
58 | environment. | |
59 | It is useful when the purpose of | |
9bccf70c A |
60 | .Xr fork 2 |
61 | would have been to create a new system context for an | |
cb323159 A |
62 | .Xr execve 2 . |
63 | The | |
39037602 | 64 | .Fn vfork |
cb323159 | 65 | system call |
9bccf70c | 66 | differs from |
cb323159 A |
67 | .Xr fork 2 |
68 | in that the child borrows the parent process's address space and the | |
69 | calling thread's stack | |
70 | until a call to | |
9bccf70c A |
71 | .Xr execve 2 |
72 | or an exit (either by a call to | |
cb323159 A |
73 | .Xr _exit 2 |
74 | or abnormally). | |
75 | The calling thread is suspended while the child is using its resources. | |
76 | Other threads continue to run. | |
9bccf70c | 77 | .Pp |
cb323159 | 78 | The |
39037602 | 79 | .Fn vfork |
cb323159 | 80 | system call |
9bccf70c A |
81 | returns 0 in the child's context and (later) the pid of the child in |
82 | the parent's context. | |
83 | .Pp | |
cb323159 A |
84 | Many problems can occur when replacing |
85 | .Xr fork 2 | |
86 | with | |
87 | .Fn vfork . | |
88 | For example, it does not work to return while running in the child's context | |
9bccf70c A |
89 | from the procedure that called |
90 | .Fn vfork | |
91 | since the eventual return from | |
92 | .Fn vfork | |
93 | would then return to a no longer existent stack frame. | |
cb323159 A |
94 | Also, changing process state which is partially implemented in user space |
95 | such as signal handlers with | |
96 | .Xr libthr 3 | |
97 | will corrupt the parent's state. | |
98 | .Pp | |
9bccf70c | 99 | Be careful, also, to call |
cb323159 | 100 | .Xr _exit 2 |
9bccf70c | 101 | rather than |
cb323159 A |
102 | .Xr exit 3 |
103 | if you cannot | |
104 | .Xr execve 2 , | |
9bccf70c | 105 | since |
cb323159 | 106 | .Xr exit 3 |
9bccf70c A |
107 | will flush and close standard I/O channels, and thereby mess up the |
108 | parent processes standard I/O data structures. | |
109 | (Even with | |
cb323159 | 110 | .Xr fork 2 |
9bccf70c | 111 | it is wrong to call |
cb323159 | 112 | .Xr exit 3 |
9bccf70c | 113 | since buffered data would then be flushed twice.) |
cb323159 A |
114 | .Pp |
115 | Unlike | |
2d21ac55 | 116 | .Xr fork 2 , |
cb323159 A |
117 | .Fn vfork |
118 | does not run | |
119 | .Xr pthread_atfork 3 | |
120 | handlers. | |
121 | .Sh RETURN VALUES | |
122 | Same as for | |
123 | .Xr fork 2 . | |
2d21ac55 A |
124 | .Sh ERRORS |
125 | The | |
126 | .Fn vfork | |
127 | system call will fail for any of the reasons described | |
128 | in the | |
129 | .Xr fork | |
130 | man page. | |
131 | In addition, it will fail if: | |
132 | .Bl -tag -width Er | |
133 | .\" =========== | |
134 | .It Bq Er EINVAL | |
135 | A system call other than | |
136 | .Xr _exit() | |
137 | or | |
138 | .Xr execve() | |
139 | (or libc functions that make no system calls other than those) | |
140 | is called following calling a | |
141 | .Fn vfork | |
142 | call. | |
143 | .El | |
cb323159 A |
144 | .Sh SEE ALSO |
145 | .Xr _exit 2 , | |
146 | .Xr execve 2 , | |
147 | .Xr fork 2 , | |
148 | .Xr sigaction 2 , | |
149 | .Xr wait 2 , | |
150 | .Xr exit 3 , | |
151 | .Xr posix_spawn 3 | |
152 | .Sh HISTORY | |
153 | The | |
154 | .Fn vfork | |
155 | system call appeared in | |
156 | .Bx 3 . | |
9bccf70c | 157 | .Sh BUGS |
9bccf70c A |
158 | To avoid a possible deadlock situation, |
159 | processes that are children in the middle | |
160 | of a | |
161 | .Fn vfork | |
162 | are never sent | |
163 | .Dv SIGTTOU | |
164 | or | |
165 | .Dv SIGTTIN | |
166 | signals; rather, | |
167 | output or | |
168 | .Xr ioctl 2 | |
169 | calls | |
170 | are allowed | |
171 | and input attempts result in an end-of-file indication. | |
cb323159 A |
172 | .Sh CAVEATS |
173 | There are limits to what you can do in the child process. | |
174 | To be totally safe you should restrict yourself to only | |
175 | executing async-signal safe operations until such time | |
176 | as one of the exec functions is called. All APIs, including | |
177 | global data symbols, in any framework or library should be | |
178 | assumed to be unsafe after a | |
9bccf70c | 179 | .Fn vfork |
cb323159 A |
180 | unless explicitly documented to be safe or async-signal |
181 | safe. If you need to use these frameworks in the child | |
182 | process, you must exec. In this situation it is reasonable | |
183 | to exec yourself. |