forking in win32

Jan 8, 2009 at 9:00am
Hi,
Currently i'm porting an application from linux to windows and i've hit certain roadblocks. Hope this forus helps me out. The scenario is

I've the main application which forks 3 other processes based on particular information and goes on to continue with the main process. this main process again forks 2 other processes based on some other information.

Problem is i'm not able to understand how forking can be exactly achieved on windows since parent's and child's address space are not as they are in linux.
Also synchronisation between the processes is another road block.

Can somebody help me on how to create a child process similar to the one that gets created in fork and main process should continue to execute its piece of code...
Last edited on Jan 8, 2009 at 9:01am
Jan 8, 2009 at 10:46am
I dare say you can't use fork() in Windows, even notionally. MKS seem to have implemented one, but I have no idea how they do it, nor do the cygwin folk as their implementation is best worked around.

Windows can start new programs or spawn new threads of execution within a program. I know of no simple way to get Windows to simulate the Unix fork() function.

Synchronisation across process boundaries is easy under Windows. There's a whole set of objects collectively known as synchronisation objects that you can use.
http://msdn.microsoft.com/en-us/library/ms686364(VS.85).aspx?ppud=4
Jan 8, 2009 at 11:06am
ok in that case, is there a method of execution of the processes where

-> parent thread can continue with its own execution after creating the child

-> child thread can execute in parallel to the parent ?
Jan 8, 2009 at 12:03pm
If you're using Microsoft's C++ compiler, you should use _beginthreadex() to create a new thread. The parent thread and the child thread will run in parallel.

Don't use CreateThread(). The C runtime library provides a number of static variables for each thread (such as errno).
Jan 8, 2009 at 2:06pm
oh if that is the case then wont synchronisation be a problem when large number of threads are created? in my scenario around 7 processes are created !!!
Jan 8, 2009 at 6:27pm
I'd look at replacing your fork code with something like the boost threading library. That would give you platform independent threading and sync functionality.
Jan 9, 2009 at 2:33am
oh which one is that... never heard of it.. can you specify more details about the same.. it would be more helpful..
Jan 9, 2009 at 9:04am
closed account (z05DSL3A)
http://www.boost.org/

http://www.boost.org/doc/libs/1_37_0/doc/html/thread.html
Jan 9, 2009 at 9:35pm
It is possible to fork() in Windows, but only by using some 'undocumented' system calls. It really isn't a safe or good alternative.

As you are porting the application, there is no simple answer. I also recommend playing with the Boost libraries.

There are also memory-mapped files and mutexes if you want to be really simple about the port.

Finally, there is also the option of using CygWin. The purpose of CygWin is to privide a Unix/POSIX environment for applications written with that architecture in mind but for running on Windows.
http://www.cygwin.com/

Good luck!

Hey Grey Wolf! Nice to see you back!
Jan 13, 2009 at 9:16am
closed account (z05DSL3A)
Hi Duoas, It is nice to finally have some more free time to enable me to revisit the site.

Congratulation on the engagement by the way.
Topic archived. No new replies allowed.