http://www.perlmonks.org?node_id=588994

tiny_tim has asked for the wisdom of the Perl Monks concerning the following question:

Hi. What is the difference between fork on Windows and fork on Unix. Thanking you in advance.
  • Comment on What is the difference between Windows fork an Unix fork ?

Replies are listed 'Best First'.
Re: What is the difference between Windows fork an Unix fork ?
by Corion (Patriarch) on Dec 11, 2006 at 07:58 UTC

    fork() on Windows does not exist. It is emulated through a variety of options, depending on whether you're using the native ("MSWin32") Perl compiled with MSVC, or the Cygwin Perl. fork() on Cygwin likely works I guess, and on MSWin32, it works for those who implemented it, but I can only recommend to stay away from it and use other mechanisms of launching programs, like system(1,...) or system("start $command"); or Win32::Process.

Re: What is the difference between Windows fork an Unix fork ?
by jbert (Priest) on Dec 11, 2006 at 10:14 UTC
    Outside the world of perl (or cygwin), there isn't a fork on Windows. Unix has two primitives: fork (create new process identical to calling process) and exec (replace details of current process with those loaded from executable file).

    Windows has one primitive: CreateProcess, which is roughly equivalent to fork-then-exec.

    cygwin works around this with crazy mad black magic1, which manages to emulate fork on windows (slowly). The perlfork doc linked above shows perl's own answer to that (presumably used in Activestate and Strawberry perl but not cygwin).

    1: Something like: use CreateProcess to start a compliant, suspended process. Use setjmp to save state, parent copies text and data into waiting child (and setjmp info). Child starts and longjmp's back to saved state. Plus some more shenanigans with copying the stack and heap once the child is running. And probably a lot more. Amazing they ever got it working.

Re: What is the difference between Windows fork an Unix fork ?
by bingos (Vicar) on Dec 11, 2006 at 08:16 UTC
Re: What is the difference between Windows fork an Unix fork ?
by bart (Canon) on Dec 11, 2006 at 12:42 UTC
    There used to be a fantastic page up at Mr. Peabody explains fork(), explaining how fork() works on Unix, and how the fork() emulation in Perl for Win32, 5.6 and higher, works. You may still be able to find it at the Wayback Machine — it currently (still) works, but who knows for how long: I have the impression archive.org no longer adds new pages.

    You have to enable Javascript to load images and enable links, as it is patched live, in DOM.

    For posteriority, I made a copy available at http://users.pandora.be/bartl/classicperl/fork/all.html. Some images are missing, they're not at the Wayback Machine either. I have reconstructed some.

    What's worse: it looks like some images have started to disappear from archive.org. Seems like I was just in time to rescue most.

Re: What is the difference between Windows fork an Unix fork ?
by msk_0984 (Friar) on Dec 11, 2006 at 11:40 UTC
    Hi As in the above monks said The fork system call in Unix creates a new process. The new process inherits various properties from its parent (Environmental variables, File descriptors, etc - see the manual page for details). After a successful fork call, two copies of the original code will be running. In the original process (the parent) the return value of fork will be the process ID of the child. In the new child process the return value of fork will be 0.

    Work Hard Party Harderrr!!
    Sushil Kumar
Re: What is the difference between Windows fork an Unix fork ?
by tiny_tim (Sexton) on Dec 12, 2006 at 05:34 UTC
    Thank you jbert. Your reply explained things much clearer now.