In POSIX, there is the fork()
function to create a sub-process. How can I achieve fork()
's functionality in Windows?
Asked
Active
Viewed 1.1k times
7
-
4What do want to know? How to create a thread programming on Windows? – Mithrandir Feb 05 '12 at 08:31
-
oh, no , I hava a trouble when i study linux... On Windows, we can use the function CreateThread(...) to creat a thread. But on Linux ,There is a function fork() to create a child process, I want to know wehter there is function similar to fork() on Window... – Mr.Tu Feb 05 '12 at 08:36
-
As far as i know yu can use _spawn(), _beginthread() and _endthread(). – Mithrandir Feb 05 '12 at 08:41
-
Ah, This is a other way to create a thread, But can we create a process? – Mr.Tu Feb 05 '12 at 08:50
-
1Ether i'm a bit thick or your manner of posing question is quite unique ... anyway there is CreateProcess(): http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspx – Mithrandir Feb 05 '12 at 08:54
-
1Related discussion: http://www.gamedev.net/topic/360290-fork-on-windows/ – Liran Orevi Feb 05 '12 at 08:56
-
yes, I understand what you mean. Thank you ... – Mr.Tu Feb 05 '12 at 09:11
-
Perhaps _spawn* in process.h may be sufficient: https://stackoverflow.com/a/46236161/2630028 – solstice333 Sep 15 '17 at 09:26
1 Answers
10
There is no direct equivalent of fork()
on Windows.
CreateProcess()
is the native function that can be used to create a new process (but, again, the semantics are rather different to fork()
's).
To put this another way, on Unix it is possible for a process to cheaply create a clone of itself. There is no inexpensive way to do this on Windows.
If you don't care about the cloning aspect of fork()
, then CreateProcess()
should do just fine.

NPE
- 486,780
- 108
- 951
- 1,012
-
-
5@Tu_JianWu: the new process created by fork() inherits the parent's state, including the value of variables and the current instruction pointer. The new process created by CreateProcess() doesn't, it is started from scratch. Also fork() can only create a new copy of the executable that is already running, CreateProcess() can launch any exectuable. – Harry Johnston Feb 06 '12 at 21:07