http://qs321.pair.com?node_id=989766

bulk88 has asked for the wisdom of the Perl Monks concerning the following question:

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.