Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
When the first child is forked, the STDOUT of the parent is connected to the STDIN of the child, like so:tee('foo'); tee('bar'); tee('baz'); while (<>) { print } sub tee { my $key = shift; # reopen STDOUT in parent and return return if my $pid = open(STDOUT, "|-"); die "cannot fork: $!" unless defined $pid; $|++; # process STDIN in child while (<STDIN>) { print "$key: $_" or die "tee output failed: $!"; } print "$$ exiting...\n"; exit; # don't let the child return to main! }
Parent -> Child1For the second and third forks I would expect the new child to be inserted between the parent and current child, so that after 3 forks:
Parent -> Child3 -> Child2 - Child1So I would expected each line of output to be preprended with:
baz: bar: foo:Instead, each line of output is prepended with:
foo: bar: baz:What is the error in my reasoning? How does this actually work? Thanks.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Understanding forking open
by tlm (Prior) on Apr 12, 2005 at 15:24 UTC | |
|
Re: Understanding forking open
by polettix (Vicar) on Apr 12, 2005 at 15:25 UTC |