On 11/8/06, Jen Spinney <[EMAIL PROTECTED]> wrote:


use strict;
use warnings;

print "First Trial:\n\n";

if (open(CHILD, "|-"))
{
    print "parent starts: ", (scalar localtime),"\n";
    sleep 5;
    print "parent ends: ", (scalar localtime),"\n";
}
else
{
    print "child starts: ", (scalar localtime),"\n";
    print "child ends: ", (scalar localtime),"\n";
    exit;
}

print "\nSecond Trial:\n\n";

if (open(CHILD, "|-"))
{
    print "parent starts: ", (scalar localtime),"\n";
    print CHILD "printing to child\n";
    sleep 5;
    print "parent ends: ", (scalar localtime),"\n";
}
else
{
    print "child starts: ", (scalar localtime),"\n";
    my $input = <STDIN>;
    print "child ends: ", (scalar localtime),"\n";
    exit;
}

__END__


I know that the OS determines which one of two processes that are
happening at virtually the same time will be executed. But, there is a
five second pause in my first example before it would print 'child
starts'.  This more recent code should demonstrate this more clearly.
Or maybe I've misunderstood you?


There are a couple of things going on here.

First, the issue of what prints when is driven by buffering. Since you
haven't turned on autofulsh on any of your file handles, the buffer is
flushed whenever it's convenient for the the system. The results may
be more what you expect if you print to STDERR instead of STDOUT.

This also looks to be the culprit in your second example. You print to
the child, but you never do anything to guarantee the buffer is
flushed, so the child doesn't see the "\n" on STDIN until Perl
automatically reaps the filehandle and flushes the buffer when the
parent tries to exit. In this case, turn on autoflush on CHILD and see
if you don't get something more like your expected result.

HTH,

-- jay
--------------------------------------------------
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.downloadsquad.com  http://www.engatiki.org

values of β will give rise to dom!

Reply via email to