How does windows work without fork? So many things start with a call to fork: starting a new process (fork/exec), creating a daemon, creating independent tasks within one program…
The nice thing about all of these things is, if a new process goes bad, it probably won’t take out the parent process. You don’t get the same safety with a thread. Therefore, fork is the basis of a lot of my reliable software.
I’m fairly confident that windows doesn’t have anything similar to fork. Otherwise the perl fork emulation could simply wrap whatever windows has. Sadly it doesn’t quite work that nicely.
While the emulation is designed to be as compatible as possible with the real fork() at the level of the Perl program, there are certain important differences that stem from the fact that all the pseudo child "processes" created this way live in the same real process as far as the operating system is concerned.
…
If the parent process is killed (either using Perl’s kill() builtin, or using some external means) all the pseudo-processes are killed as well, and the whole process exits.
I have been approaching this problem of reliable windows software from too low a level. Rather than copying what I do on Unix and start with fork (using Perl more or less as my OS wrapper), I now use web servers to manage my processes.
If you’re just spawning external programs, see Win32::Job. For less control, “system(1, $cmd, @args)” works, too. See docs for “system” in perlport.
Hi xdg,
Spawning processes is only part of what I need, but thanks for the pointer to Win32::Job. Previously I used Win32::Process which is a bit more fiddly.
I’m not *entirely* sure that i understand what you’re asking here in the first place. However, as far as creating daemons on windows goes, this is probably the easiest way to do it:
http://search.cpan.org/~mithaldu/Win32-Detached-1.103080/lib/Win32/Detached.pm
Searching Google for “NtCreateProcess fork”, you’ll find a few references to Gary Nebbett’s “Windows NT/2000 native API reference”, which does implement fork() in an example.
In short, it’s easy to fork an NT process. Making the fork a usable Win32 process is a bit more involved, not documented, and probably fragile from OS release to OS release, but is possible.
An example implementation of exec() is also provided in the book, with similar caveats.