views:

2471

answers:

4

I've had some trouble forking of processes from a Perl CGI script when running on Windows. The main issue seems to be that 'fork' is emulated when running on windows, and doesn't actually seem to create a new process (just another thread in the current one). This means that web servers (like IIS) which are waiting for the process to finish continue waiting until the 'background' process finishes.

Is there a way of forking off a background process form a CGI script under Windows? Even better, is there a single function I can call which will do this in a cross platform way?

(And just to make life extra difficult, I'd really like a good way to redirect the forked processes output to a file at the same time).

A: 

perlfork:

Perl provides a fork() keyword that corresponds to the Unix system call of the same name. On most Unix-like platforms where the fork() system call is available, Perl's fork() simply calls it.

On some platforms such as Windows where the fork() system call is not available, Perl can be built to emulate fork() at the interpreter level. While the emulation is designed to be as compatible as possible with the real fork() at the 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.

Espo
+2  A: 

Use Win32::Process->Create with DETACHED_PROCESS parameter

aku
+8  A: 

If you want to do this in a platform independent way, Proc::Background is probably the best way.

Leon Timmermans
A: 

I've found real problems with fork() on Windows, especially when dealing with Win32 Objects in Perl. Thus, if it's going to be Windows specific, I'd really recommend you look at the Thread library within Perl.

I use this to good effect accepting more than one connection at a time on websites using IIS, and then using even more threads to execute different scripts all at once.

Spedge