什么是最接近Windows有叉()? [英] What is the closest thing windows has to fork()?

查看:90
本文介绍了什么是最接近Windows有叉()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这个问题说明了一切。

I guess the question says it all.

我要叉子窗口。什么是最相似的操作以及如何使用它。

I want to fork on windows. What is the most similar operation and how do I use it.

推荐答案

Cygwin的已经全功能叉()。因此,如果使用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叉是一个
  非写入时复制的实​​现
  类似什么是present早
  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调用保存自己的
  上下文,并设置在一个指向此
  Cygwin的共享内存区(共享
  所有Cygwin的任务之间)。然后,它填补
  在孩子的。数据和.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.

虽然我们有一些想法,如何
  加快我国​​通过实施叉子
  减少上下文数
  父母与子女之间的切换
  过程中,叉几乎肯定会
  总是在Win32下效率低下。
  幸运的是,在大多数情况下
  所提供的电话产卵家庭
  Cygwin的可被取代的
  叉/ EXEC对只有一点点
  功夫。这些调用地映射在上面
  的Win32 API的。其结果是,他们
  是更有效的。更改
  编译器的驱动程序来调用
  产卵,而不是叉是一个微不足道
  变化和增加编译
  通过速度二十到在百分之三十
  我们的测试。

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.

不过,产卵和exec present他们
  自己的一套困难。因为
  是没有办法做一个实际的EXEC下
  Win32中,Cygwin的有创造自己的
  进程ID(PID)。其结果是,当
  流程执行多个EXEC
  电话,就会有多个Windows
  与单一的Cygwin相关的PID
  PID。的每一个的在某些情况下,存根
  这些Win32进程可能会持续,
  等待他们的exec'd 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.

听起来像很多工作,不是吗?是的,它是slooooow。

Sounds like a lot of work, doesn't it? And yes, it is slooooow.

编辑:DOC已经过时,请参阅本优秀的<一个href=\"http://stackoverflow.com/questions/985281/what-is-the-closest-thing-windows-has-to-fork/985525#985525\">answer一个更新

the doc is outdated, please see this excellent answer for an update

这篇关于什么是最接近Windows有叉()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆