問題描述
我想這個問題說明了一切.
I guess the question says it all.
我想在 Windows 上分叉.最相似的操作是什么,如何使用.
I want to fork on Windows. What is the most similar operation and how do I use it.
推薦答案
Cygwin 有全功能的 fork()在 Windows 上.因此,如果您可以接受使用 Cygwin,那么在性能不成問題的情況下,問題就解決了.
Cygwin has fully featured fork() on Windows. Thus if using Cygwin is acceptable for you, then the problem is solved in the case performance is not an issue.
否則你可以看看Cygwin是如何實現fork()的.來自相當古老的 Cygwin 架構 doc:
Otherwise you can take a look at how Cygwin implements fork(). From a quite old Cygwin's architecture doc:
5.6.流程創建Cygwin 中的 fork 調用特別有趣因為它不能很好地映射在Win32 API.這使它很難以正確實施.目前,Cygwin fork 是一個非寫時復制實現類似于早期出現的UNIX 的風格.
5.6. Process Creation The fork call in Cygwin is particularly interesting because it does not map well on top of the Win32 API. This makes it very difficult to implement correctly. Currently, the Cygwin fork is a non-copy-on-write implementation similar to what was present in early flavors of UNIX.
發生的第一件事父進程派生一個子進程是父初始化一個空間在 Cygwin 進程表中孩子.然后它創建一個暫停使用 Win32 的子進程CreateProcess 調用.接下來,家長進程調用 setjmp 來保存自己的上下文并設置一個指向 this 的指針Cygwin 共享內存區域(共享在所有 Cygwin 任務中).然后填滿在孩子的 .data 和 .bss 部分通過從它自己的地址空間復制進入暫停兒童的地址空間.在孩子的地址空間之后被初始化,子進程同時運行父級等待互斥鎖.孩子發現它已經分叉并且longjumps 使用保存的跳轉緩沖區.然后孩子將互斥鎖設置為父母正在等待并阻止另一個互斥鎖.這是信號復制其堆棧和堆的父級進入孩子,然后它釋放孩子所在的互斥鎖等待并從叉子返回稱呼.最后,孩子從在最后一個互斥鎖上阻塞,重新創建任何傳遞給它的內存映射區域通過共享區域,并從分叉本身.
The first thing that happens when a parent process forks a child process is that the parent initializes a space in the Cygwin process table for the child. It then creates a suspended child process using the Win32 CreateProcess call. Next, the parent process calls setjmp to save its own context and sets a pointer to this in a Cygwin shared memory area (shared among all Cygwin tasks). It then fills in the child's .data and .bss sections by copying from its own address space into the suspended child's address space. After the child's address space is initialized, the child is run while the parent waits on a mutex. The child discovers it has been forked and longjumps using the saved jump buffer. The child then sets the mutex the parent is waiting on and blocks on another mutex. This is the signal for the parent to copy its stack and heap into the child, after which it releases the mutex the child is waiting on and returns from the fork call. Finally, the child wakes from blocking on the last mutex, recreates any memory-mapped areas passed to it via the shared area, and returns from fork itself.
雖然我們有一些關于如何通過以下方式加快我們的分叉實施減少上下文的數量在父子之間切換進程,fork 幾乎肯定會在 Win32 下總是效率低下.幸運的是,在大多數情況下spawn 提供的調用系列Cygwin 可以代替fork/exec 對只有一點點努力.這些調用清晰地映射在頂部Win32 API.結果,他們效率更高.改變編譯器的驅動程序調用spawn 而不是 fork 是微不足道的改變和增加編譯速度提高百分之二十到三十我們的測試.
While we have some ideas as to how to speed up our fork implementation by reducing the number of context switches between the parent and child process, fork will almost certainly always be inefficient under Win32. Fortunately, in most circumstances the spawn family of calls provided by Cygwin can be substituted for a fork/exec pair with only a little effort. These calls map cleanly on top of the Win32 API. As a result, they are much more efficient. Changing the compiler's driver program to call spawn instead of fork was a trivial change and increased compilation speeds by twenty to thirty percent in our tests.
然而,spawn 和 exec 展示了他們的自己的一系列困難.因為那里沒有辦法在下面做一個實際的 execWin32,Cygwin 得自己發明進程 ID (PID).結果,當一個進程執行多個exec調用,會有多個Windows與單個 Cygwin 關聯的 PIDPID.在某些情況下,每個的存根這些 Win32 進程可能會逗留,等待他們執行的 Cygwin進程退出.
However, spawn and exec present their own set of difficulties. Because there is no way to do an actual exec under Win32, Cygwin has to invent its own Process IDs (PIDs). As a result, when a process performs multiple exec calls, there will be multiple Windows PIDs associated with a single Cygwin PID. In some cases, stubs of each of these Win32 processes may linger, waiting for their exec'd Cygwin process to exit.
聽起來工作量很大,不是嗎?是的,它太慢了.
Sounds like a lot of work, doesn't it? And yes, it is slooooow.
文檔已過時,請參閱此優秀回答 更新
the doc is outdated, please see this excellent answer for an update
這篇關于Windows 最接近 fork() 的是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!