Created
March 21, 2011 11:57
-
-
Save riywo/879355 to your computer and use it in GitHub Desktop.
perlのfork&execで `echo "show databases" | mysql -uroot` を再現してみた
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use Data::Dumper; | |
use IO::Handle; | |
my ($parent_in, $parent_out); | |
my ($child_in, $child_out); | |
pipe $parent_out, $child_in; | |
pipe $child_out, $parent_in; | |
$parent_in->autoflush(1); | |
$child_in->autoflush(1); | |
if (my $pid = fork) { | |
close $parent_out; | |
close $parent_in; | |
my ($parent_in2, $child_out2); | |
pipe $child_out2, $parent_in2; | |
$parent_in2->autoflush(1); | |
if (my $pid2 = fork) { | |
close $child_out; | |
close $parent_in2; | |
my @lines = <$child_out2>; | |
print "@lines"; | |
waitpid($pid2,0); | |
} | |
else { | |
close $child_out2; | |
open STDIN, '<&', $child_out; | |
open STDOUT, '>&', $parent_in2; | |
exec ('mysql', '-uroot'); | |
} | |
waitpid($pid,0); | |
} | |
else { | |
close $child_out; | |
close $child_in; | |
open STDIN, '<&', $parent_out; | |
open STDOUT, '>&', $parent_in; | |
exec ('echo', 'show databases'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment