Skip to content

anonymous /gist:9cfe9946190242637f09 secret

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#!/usr/local/bin/perl
use strict;
####################### Main Program ######################################
my $g_LOGPATH = '.';
my $g_LOGFILE="parent.log";
my @pids;
opendir(DIR, '.') || die "cant open dir";
foreach my $file ( readdir(DIR) ) {
my $pid = fork;
if ( $pid ) {
setEnvironment();
#parent process code goes here
printf "%s\n", "parent";
push(@pids, $pid);
next;
}
$g_LOGFILE="child.log";
setEnvironment();
#child code goes here
printf "%s file $file\n", "child";
exit;
}
closedir(DIR);
#wait for @pids
while( @pids ) {
my $pid = waitpid(-1, 0);
@pids = grep {$_!=$pid} @pids;
}
sub setEnvironment()
{
close STDOUT;
unless ( open(STDOUT, "|tee -ai $g_LOGPATH/$g_LOGFILE") ) {
die "Cannot open log file $g_LOGPATH/$g_LOGFILE $!";
return 2;
}
*STDERR = *STDOUT;
STDOUT->autoflush(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.