📄 how-vfork-works.txt
字号:
Copyright 2001 Red Hat Inc., Christopher FaylorHow does vfork work?When a program calls vfork, cygwin attempts to short circuit itsnormal, expensive fork mechanism.Vfork is mainly smoke and mirrors. A call to vfork contines to executein the current process but first it returns a pid of 0 so that processwill execute code intended for the child in a UNIX system. Beforereturning the zero, vfork makes a copy of the current fd table so thatclosing an fd in the "child" will not affect the "parent".Some of this info is stored in a per-thread structure but vfork is notreally thread-safe since it also stores the fd "backup" table in theglobal fd table.The process continues to execute until it hits some type of exec call.The exec call is essentially translated into a spawn NO_WAIT call andthe new process is started via this mechanism. After execing, the"child" process no longer should exist, so the spawn code longjmps backto the original vfork call. The previously opened fds are closed andthe parent's fd table is restored. vfork() then returns the pid of thejust-spawned process.Meanwhile, the just-spawned child notices that it has been spawned asthe result of a vfork and closes the extra file handles.This all relies on the fact that the child in a vfork call can affectjust about everything in the parent except for the parent's fds.The assumption is that a vfork is always just used as a method for
starting a program.
The assumption is also that all of this is much faster than theslow method that cygwin uses to implement fork().
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -