]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/vfork.2
This has never been a 'real' syscall on any Unix.
[thirdparty/man-pages.git] / man2 / vfork.2
CommitLineData
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
28vfork \- 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.)
37The
38.IR vfork ()
39function has the same effect as
40.IR fork (),
41except that the behaviour is undefined if the process created by
42.IR vfork ()
43either modifies any data other than a variable of type pid_t used
44to store the return value from
45.IR vfork (),
46or returns from the function in which
47.IR vfork ()
48was called, or calls any other function before successfully calling
49.IR _exit ()
50or one of the
51.I exec
52family of functions.
53.SH ERRORS
54.TP
55.B EAGAIN
56Too many processes - try again.
57.TP
58.B ENOMEM
59There is insufficient swap space for the new process.
60.SH "LINUX DESCRIPTION"
61.BR vfork ,
62just like
63.BR fork (2),
64creates a child process of the calling process.
65For details and return value and errors, see
66.BR fork (2).
67.PP
68.B vfork()
69is a special case of
70.BR clone (2).
71It is used to create new processes without copying the page tables of
72the parent process. It may be useful in performance sensitive applications
73where a child will be created which then immediately issues an
74.IR execve() .
75.PP
76.B vfork()
77differs from fork in that the parent is suspended until the child makes
78a call to
79.BR execve (2)
80or
81.BR _exit (2).
82The child shares all memory with its parent, including the stack, until
83.I execve()
84is issued by the child. The child must not return from the
85current function or call
86.IR exit() ,
87but may call
88.IR _exit() .
89.PP
90Signal handlers are inherited, but not shared. Signals to the parent
91arrive after the child releases the parent.
92.SH "HISTORIC DESCRIPTION"
93Under Linux,
94.IR fork ()
95is implemented using copy-on-write pages, so the only penalty incurred by
96.IR fork ()
97is the time and memory required to duplicate the parent's page tables,
98and to create a unique task structure for the child.
99However, in the bad old days a
100.IR fork()
101would require making a complete copy of the caller's data space,
102often needlessly, since usually immediately afterwards an
103.IR exec ()
104is done. Thus, for greater efficiency, BSD introduced the
105.B vfork
106system call, that did not fully copy the address space of
107the parent process, but borrowed the parent's memory and thread
108of control until a call to
109.IR execve ()
110or an exit occurred. The parent process was suspended while the
111child was using its resources.
112The use of vfork was tricky - for example, not modifying data
113in the parent process depended on knowing which variables are
114held in a register.
115.SH BUGS
116It is rather unfortunate that Linux revived this spectre from the past.
117The BSD manpage states:
118"This system call will be eliminated when proper system sharing mechanisms
119are implemented. Users should not depend on the memory sharing semantics of
120.I vfork
121as it will, in that case, be made synonymous to
122.IR fork .\c
123"
124
125Formally speaking, the standard description given above does not allow
126one to use
127.IR vfork ()
128since a following
129.IR exec
130might fail, and then what happens is undefined.
131
132Details of the signal handling are obscure and differ between systems.
133The BSD manpage states:
134"To avoid a possible deadlock situation, processes that are children
135in the middle of a
136.I vfork
137are never sent SIGTTOU or SIGTTIN signals; rather, output or
138.IR ioctl s
139are allowed and input attempts result in an end-of-file indication."
140
141Currently (Linux 2.3.25),
142.BR strace (1)
143cannot follow
144.IR vfork()
145and requires a kernel patch.
146.SH HISTORY
147The
148.IR vfork ()
149system 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'.
152In BSD 4.4 it was made synonymous to
153.IR fork (),
154but NetBSD introduced it again,
155cf. http://www.netbsd.org/Documentation/kernel/vfork.html .
156In Linux, it has been equivalent to
157.IR fork ()
158until 2.2.0-pre6 or so. Since 2.2.0-pre9 (on i386, somewhat later on
159other architectures) it is an independent system call. Support was
160added in glibc 2.0.112.
161.SH "CONFORMING TO"
162The
163.B vfork
164call may be a bit similar to calls with the same name in other
165operating systems. The requirements put on
166.B vfork
167by the standards are weaker than those put on
168.BR fork ,
169so an implementation where the two are synonymous
170is compliant. In particular, the programmer cannot
171rely on the parent remaining blocked until a call of
172.I execve()
173or
174.I _exit()
175and 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)