OSDN Git Service

gas/opcodes: blackfin: move dsp mac func defines to common header
[pf3gnuchains/sourceware.git] / winsup / cygwin / how-vfork-works.txt
1 (THIS DESCRIPTION IS OUT-OF-DATE)
2
3 How does vfork work?
4
5 When a program calls vfork, cygwin attempts to short circuit its
6 normal, expensive fork mechanism.
7
8 Vfork is mainly smoke and mirrors.  A call to vfork contines to execute
9 in the current process but first it returns a pid of 0 so that process
10 will execute code intended for the child in a UNIX system.  Before
11 returning the zero, vfork makes a copy of the current fd table so that
12 closing an fd in the "child" will not affect the "parent".
13
14 Some of this info is stored in a per-thread structure but vfork is not
15 really thread-safe since it also stores the fd "backup" table in the
16 global fd table.
17
18 The process continues to execute until it hits some type of exec call.
19 The exec call is essentially translated into a spawn NO_WAIT call and
20 the new process is started via this mechanism.  After execing, the
21 "child" process no longer should exist, so the spawn code longjmps back
22 to the original vfork call.  The previously opened fds are closed and
23 the parent's fd table is restored.  vfork() then returns the pid of the
24 just-spawned process.
25
26 Meanwhile, the just-spawned child notices that it has been spawned as
27 the result of a vfork and closes the extra file handles.
28
29 This all relies on the fact that the child in a vfork call can affect
30 just about everything in the parent except for the parent's fds.
31 The assumption is that a vfork is always just used as a method for
32 starting a program.
33
34 The assumption is also that all of this is much faster than the
35 slow method that cygwin uses to implement fork().
36