I need to do a Win32 fork, and the child needs to stay waiting on the parent until the parent signals for the child to exit, then the parent waits until the child exits. I got the code from
perlfork. Tried it on 5.12 and 5.17 same result.
use strict;
use warnings;
my ($child, $parent);
pipe($child, $parent) or die;
my $pid = fork();
die "fork() failed: $!" unless defined $pid;
if ($pid) {
close $child;
}
else {
close $parent;
print "child waiting\n";
my $read;
read($child, $read, 1);
print "child exiting\n";
exit(0);
}
print $parent "exit now\r\n\r";
print "parent going to wait\n";
waitpid($pid, 0);
All I get in console is
parent going to wait
child waiting
and then I kill perl.exe since it hung. If I add a "close($parent);" after "print $parent "exit now\r\n\r";" it works. I dont know why. Can someone explain what is happening here?
update: the real purpose of this code is for it to be part of a unit test to make an XS module psuedo-fork safe. Since the object was copied during the fork, when the child psuedo proc exits, the C resource is freed, and using the object in the parent caused a crash. The C resource has its own internal reference count which can be queried in C, so I need to check refcount before the fork, make sure it is 1, do a fork, check refcount, make sure it is 2, then tell the child to exit, when child exists, check refcount, make sure it is 1. If no solution was possible (BrowserUK gave 2), I would have been forced to add
Win32::IPC as a build/test dep.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.