1vfork(2) System Calls vfork(2)
2
3
4
6 vfork, vforkx - spawn new process in a virtual memory efficient way
7
9 #include <unistd.h>
10
11 pid_t vfork(void);
12
13
14 #include <sys/fork.h>
15
16 pid_t vforkx(int flags);
17
18
20 The vfork() and vforkx() functions create a new process without fully
21 copying the address space of the old process. These functions are use‐
22 ful in instances where the purpose of a fork(2) operation is to create
23 a new system context for an execve() operation (see exec(2)).
24
25
26 Unlike with the fork() function, the child process borrows the parent's
27 memory and thread of control until a call to execve() or an exit
28 (either abnormally or by a call to _exit() (see exit(2)). Any modifica‐
29 tion made during this time to any part of memory in the child process
30 is reflected in the parent process on return from vfork() or vforkx().
31 The parent process is suspended while the child is using its resources.
32
33
34 In a multithreaded application, vfork() and vforkx() borrow only the
35 thread of control that called vfork() or vforkx() in the parent; that
36 is, the child contains only one thread. The use of vfork() or vforkx()
37 in multithreaded applications, however, is unsafe due to race condi‐
38 tions that can cause the child process to become deadlocked and conse‐
39 quently block both the child and parent process from execution indefi‐
40 nitely.
41
42
43 The vfork() and vforkx() functions can normally be used the same way as
44 fork() and forkx(), respectively. The calling procedure, however,
45 should not return while running in the child's context, since the even‐
46 tual return from vfork() or vforkx() in the parent would be to a stack
47 frame that no longer exists. The _exit() function should be used in
48 favor of exit(3C) if unable to perform an execve() operation, since
49 exit() will invoke all functions registered by atexit(3C) and will
50 flush and close standard I/O channels, thereby corrupting the parent
51 process's standard I/O data structures. Care must be taken in the child
52 process not to modify any global or local data that affects the behav‐
53 ior of the parent process on return from vfork() or vforkx(), unless
54 such an effect is intentional.
55
56
57 Unlike fork() and forkx(), fork handlers are not run when vfork() and
58 vforkx() are called.
59
60
61 The vfork() and vforkx() functions are deprecated. Their sole legiti‐
62 mate use as a prelude to an immediate call to a function from the exec
63 family can be achieved safely by posix_spawn(3C) or posix_spawnp(3C).
64
65 Fork Extensions
66 The vforkx() function accepts a flags argument consisting of a bitwise
67 inclusive-OR of zero or more of the following flags, which are defined
68 in the header <sys/fork.h>:
69 FORK_NOSIGCHLD
70 FORK_WAITPID
71
72
73 See fork(2) for descriptions of these flags. If the flags argument is
74 0, vforkx() is identical to vfork().
75
77 Upon successful completion, vfork() and vforkx() return 0 to the child
78 process and returns the process ID of the child process to the parent
79 process. Otherwise, −1 is returned to the parent process, no child
80 process is created, and errno is set to indicate the error.
81
83 The vfork() and vforkx() functions will fail if:
84
85 EAGAIN The system-imposed limit on the total number of processes
86 under execution (either system-quality or by a single user)
87 would be exceeded. This limit is determined when the system
88 is generated.
89
90
91 ENOMEM There is insufficient swap space for the new process.
92
93
94
95 The vforkx() function will fail if:
96
97 EINVAL The flags argument is invalid.
98
99
101 See attributes(5) for descriptions of the following attributes:
102
103
104
105
106 ┌─────────────────────────────┬─────────────────────────────┐
107 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
108 ├─────────────────────────────┼─────────────────────────────┤
109 │Interface Stability │Obsolete │
110 ├─────────────────────────────┼─────────────────────────────┤
111 │MT-Level │Unsafe │
112 └─────────────────────────────┴─────────────────────────────┘
113
115 exec(2), exit(2), fork(2), ioctl(2), atexit(3C), exit(3C),
116 posix_spawn(3C), posix_spawnp(3C), signal.h(3HEAD), wait(3C),
117 attributes(5), standards(5)
118
120 To avoid a possible deadlock situation, processes that are children in
121 the middle of a vfork() or vforkx() are never sent SIGTTOU or SIGTTIN
122 signals; rather, output or ioctls are allowed and input attempts result
123 in an EOF indication.
124
125
126 To forestall parent memory corruption due to race conditions with sig‐
127 nal handling, vfork() and vforkx() treat signal handlers in the child
128 process in the same manner as the exec(2) functions: signals set to be
129 caught by the parent process are set to the default action (SIG_DFL) in
130 the child process (see signal.h(3HEAD)). Any attempt to set a signal
131 handler in the child before execve() to anything other than SIG_DFL or
132 SIG_IGN is disallowed and results in setting the handler to SIG_DFL.
133
134
135 On some systems, the implementation of vfork() and vforkx() cause the
136 parent to inherit register values from the child. This can create prob‐
137 lems for certain optimizing compilers if <unistd.h> is not included in
138 the source calling vfork() or if <sys/fork.h> is not included in the
139 source calling vforkx().
140
141
142
143SunOS 5.11 13 Dec 2006 vfork(2)