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 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. Otherwise you can take a look at how Cygwin implements fork(). From a quite old Cygwin's architecture doc:
Sounds like a lot of work, doesn't it? And yes, it is slooooow. EDIT: the doc is outdated, please see this excellent answer for an update |
|||||||||||||||||||||
|
|
I certainly don't know the details on this because I've never done it it, but the native NT API has a capability to fork a process (the POSIX subsystem on Windows needs this capability - I'm not sure if the POSIX subsystem is even supported anymore). A search for ZwCreateProcess() should get you some more details - for example this bit of information from Maxim Shatskih:
Though note that Corinna Vinschen indicates that Cygwin found using ZwCreateProcess() still unreliable:
|
|||||||||||||||||||||
|
|
Well, windows doesn't really have anything quite like it. Especially since fork can be used to conceptually create a thread or a process in *nix. So, I'd have to say:
and
|
||||
|
|
|
People have tried to implement fork on Windows. This is the closest thing to it I can find: Taken from: http://doxygen.scilab.org/5.3/d0/d8f/forkWindows_8c_source.html#l00216
|
|||||||||||||
|
|
The following document provides some information on porting code from UNIX to Win32: http://msdn.microsoft.com/en-us/library/y23kc048(vs.71).aspx Among other things, it indicates that the process model is quite different between the two systems and recommends consideration of CreateProcess and CreateThread where fork()-like behavior is required. |
|||
|
|
|
The UNIX process creation is quite different. It basically duplicates the current process almost in total, each in their own address space and starts running them separately. The real equivalent of If you're porting software to Windows and you don't mind a translation layer, Cygwin may provide the capability that you want (I've never had the need to test |
|||
|
|
|
Your best options are CreateProcess() or CreateThread(). There is more information on porting here. |
|||
|
|
|
There is no easy way to emulate fork() on Windows. I suggest you to use threads instead. |
|||
|
|
|
The closest you say... Let me think... This must be fork() I guess :) For details see Does Interix implement fork()? |
|||||
|
|
"as soon as you want to do file access or printf then io are refused"
"Also, you probably shouldn't use the Zw* functions unless you're in kernel mode, you should probably use the Nt* functions instead."
ZwGetContextThread(NtCurrentThread(), &context);
|
|||||
|
|
After a bit of tunning this code works. Process is Forked running where fork left it. Vars are well duplicated allocated memory as well BUT as soon as you want to do file access or printf then io are refused (on Windows 7) |
|||||
|
|
fork() semantics are necessary where the child needs access to the actual memory state of the parent as of the instant fork() is called. I have a piece of software which relies on the implicit mutex of memory copying as of the instant fork() is called, which makes threads impossible to use. (This is emulated on modern *nix platforms via copy-on-write/update-memory-table semantics.) The closest that exists on Windows as a syscall is CreateProcess. The best that can be done is for the parent to freeze all other threads during the time that it is copying memory over to the new process's memory space, then thaw them. Neither the Cygwin frok [sic] class nor the Scilab code that Eric des Courtis posted does the thread-freezing, that I can see. Also, you probably shouldn't use the Zw* functions unless you're in kernel mode, you should probably use the Nt* functions instead. There's an extra branch that checks whether you're in kernel mode and, if not, performs all of the bounds checking and parameter verification that Nt* always do. Thus, it's very slightly less efficient to call them from user mode. |
|||
|
|