Skip to content

Instantly share code, notes, and snippets.

@riywo
Created March 24, 2011 14:05
Show Gist options
  • Save riywo/885109 to your computer and use it in GitHub Desktop.
Save riywo/885109 to your computer and use it in GitHub Desktop.
Parallel::ForkManager使って普通のforkっぽいことをやる
use strict;
use warnings;
use IO::Handle;
use Parallel::ForkManager;
my $pm = Parallel::ForkManager->new(2);
my ($pread, $pwrite);
pipe $pread, $pwrite;
$pwrite->autoflush(1);
print time." parent $$\n";
if (!$pm->start) {
close $pread;
for (1..3) {
print $pwrite time." write_child $$\n";
sleep 1;
}
close $pwrite;
print time." child finish $$\n";
$pm->finish;
}
elsif (!$pm->start) {
close $pwrite;
while (<$pread>) {
chomp;
print time." read_child $$ '$_'\n";
}
sleep 3;
print time." child finish $$\n";
$pm->finish;
}
else {
close $pread;
close $pwrite;
$pm->wait_all_children;
print time." parent finish $$\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment