On 11/8/06, John W. Krahn <[EMAIL PROTECTED]> wrote:
Jen Spinney wrote:
> Hello all!

Hello,

> 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
> {
>    my $time = scalar localtime;
>    my $input = <STDIN>;
>    print "child starts: $time\n";
>    exit;
> }
> __END__
>
> My output looks like this:
> First Trial:
>
> parent starts: Wed Nov  8 13:45:14 2006
> child starts: Wed Nov  8 13:45:14 2006
> parent ends: Wed Nov  8 13:45:19 2006
>
> Second Trial:
>
> parent starts: Wed Nov  8 13:45:19 2006
                            ^^^^^^^^
> parent ends: Wed Nov  8 13:45:24 2006
> child starts: Wed Nov  8 13:45:19 2006
                           ^^^^^^^^

> I expected the first trial print statements to execute in the same
> order as the second trial print statements, but the child print
> statements don't do execute until after the parent process is over.

Order of execution is determined by the OS, not by your program.

> It seems that the line (in the child process, second trial)
> my $input = <STDIN>;
> is blocking, but why?

Look carefully at the times displayed, it is not blocking.



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall

John,

Thanks for your reply.  The times that were printed show that the two
processes are definately starting in parallel -- it's not that I think
one is finishing before the other one starts.  Perhaps a better
demonstration would be this code:

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__

Order of execution is determined by the OS, not by your program.

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?

Here is the output from the newer script:
First Trial:

parent starts: Wed Nov  8 14:58:26 2006
child starts: Wed Nov  8 14:58:26 2006
child ends: Wed Nov  8 14:58:26 2006
parent ends: Wed Nov  8 14:58:31 2006

Second Trial:

parent starts: Wed Nov  8 14:58:31 2006
child starts: Wed Nov  8 14:58:31 2006
parent ends: Wed Nov  8 14:58:36 2006
child ends: Wed Nov  8 14:58:36 2006

(the child ends 5 seconds later... it waits for the parent to end... why?)

- Jen

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to