Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Fork in Win32 environment

by esr (Scribe)
on Jun 27, 2006 at 20:31 UTC ( #557869=perlquestion: print w/replies, xml ) Need Help??
esr has asked for the wisdom of the Perl Monks concerning the following question:

Has anyone been able to successfully use fork() in the Windows and OLE environment? I have tried a simple test case and I get bizarre error messages after blowing the Perl interpreter out of the water.

Here is a snippet of the test case code:

$| = 1; if ($pid = fork) { print "this is the parent $pid\n"; while ($prange ne '$A$3') { eval {$prange = $excel->ActiveCell->{Address};}; print "$@\n" if ($@); print "Active cell address $prange\n"; sleep 3; } } elsif (defined $pid) { print "this is the child $pid\n"; $msg = "When finished editing, close the file\."; Win32::MsgBox("$msg",vbOKOnly,$mbt); #$ans = <STDIN>; exit; } else { print "fell through\n"; }
The message box appears correctly. The output in the "Run" (aka Command Prompt) window from which the program is started is as follows:
this is the parent -2072 this is the child 0 Active cell address $C$23 Active cell address $C$23
and that continues until I click on "OK" in the message box. At that point I get the following messages:
Attempt to free non-existent shared string 'Range' at <eval 1> line 1. Free to wrong pool 1e5da80 not 223f90 at <eval 1> line 1.
If I uncomment the "$ans = <STDIN>;" statement I get the same error messages but they now say:
Attempt to free non-existent shared string 'Range' at <eval 1> line 1, + <STDIN> line 1. Free to wrong pool 1e5dce0 not 223f90 at <eval 1> line 1, <STDIN> line + 1.
If I comment out the "exit" statement in the child block I immediately get the window that says the "Perl Command Line Interpreter has encountered a problem and needs to close" and asks if I want to send a report to Microsoft. In this case the command prompt window has this very interesting error message:
Undefined subroutine &Carp::shortness_heavy called at c:/Perl/lib/Carp line 182. Free to wrong pool 1e5da30 not 223f90.
I then get "Active cell address" output every 3 seconds until I click on "Don't send" to Microsoft box. Then the Perl program terminates.

Is there something basic that I am missing?

Replies are listed 'Best First'.
Re: Fork in Win32 environment
by Corion (Pope) on Jun 27, 2006 at 20:34 UTC

    fork() is emulated on Win32, and the emulation is spotty at best. It can't work with OLE due to how fork() is implemented (a complete memory copy of almost the whole Perl process). OLE objects are not copied and hence the whole memory management breaks.

Re: Fork in Win32 environment
by bart (Canon) on Jun 28, 2006 at 09:22 UTC
    I don't fully grasp why your code fails. I think as fork is emulated by threading, both child and parent attempt to hang on to the same object, and free it when they are finished, so the problems start as soon as one of them (the child) exits. Yet the child makes no use of the OLE object, so it really is a pity. Ideally, you ought to be able to relinquish the OLE object without actually freeing it.

    If this is about all that your child does, perhaps you should think of using Win32::Process to run the child with the messagebox in a completely independent process. You can use They didn't give me a fork so I have to eat with a spawn. as sample code for the setup.

    You may have to add extra code to resynchronize parent and child after both have finished with their respective tasks, but there's a lot of methods available in Win32::Process to that effect for use in the parent, most notably Wait; so the parent will not go on beyond your loop before the button on the messagebox is pushed.

Re: Fork in Win32 environment
by radiantmatrix (Parson) on Jun 29, 2006 at 17:57 UTC

    fork is essentially broken in Win32. However, for most problems, threads is an acceptable cross-platform solution (at least for recent Perls).

    Depending on what you're doing, an event loop might be quite acceptable; consider POE and friends.

    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    I haven't found a problem yet that can't be solved by a well-placed trebuchet

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://557869]
Approved by Corion
and cookies bake in the oven...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2017-05-22 23:38 GMT
Find Nodes?
    Voting Booth?