in reply to Windows Background forking in Perl /HTTP Error Capture

As far as I know, the exec command does not launch the argument program in background mode. It launches the program in the foreground and never returns to the Perl program. But the Perl program really appears to end when the argument program completes. This is an example on Unix of an exec command taking a bit of time to execute:
$ time perl -e 'print "start program. \n"; exec ("time ls -lR /usr | w +c");' start program. 36311 269214 1917392 real 0m8.933s user 0m1.309s sys 0m3.883s real 0m9.012s user 0m1.339s sys 0m3.929s
The command passed to exec is counting all the files in the /usr directory and all its subdirectories. It takes a bit of time since there are more than 36000 entries. Under Unix, the time program measures the execution time. The first report states the time taken by the exec command. The second one gives the time taken by the Perl program. As you can see, the Perl program completed only after the exec command finished. No background process here. Under Unix, I could launch my command in the background this way:
$ time perl -e 'print "start program. \n"; exec ("time ls -lR /usr | w +c &");' start program. real 0m0.090s user 0m0.045s sys 0m0.030s Laurent ~ $ 36311 269214 1917392 real 0m4.102s user 0m1.200s sys 0m2.963s
Here, the exec command is really launched in the background and the Perl program completes immediately (after 0.09 sec.) and the exec command prints the result and completes 4 seconds later. I do not know how you would do that under Windows, but you might have to launch an intermediate xxx.bat script that launches the background process and exits immediately.

Replies are listed 'Best First'.
Achieve background processing(forking) in windows
by suzun30 (Acolyte) on Mar 13, 2014 at 17:37 UTC
    Hi Laurent, Thanks for your response. Ok. That makes sense. However, still not sure how to acheive this in Windows. I have been looking for several options online and a lot are very skeptical about forking in windows and all I tried so far has not worked.I tried the fork() emulation in perl for windows. It just never seemed to run the background script, though it did return a pid. I will explore the .bat option. Thanks for your inputs again. Appreciate it.
      I have also tried Proc::Background::Win32 with not much result. It still seemd to wait for the process to finish. I did do the Close STDOUT,STDIN,STDERR filehandles part. Any inputs on this or how to capture a HTTP Error code would help. Thanks in advance.