Skip to content

Instantly share code, notes, and snippets.

Created November 5, 2012 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/9cfe9946190242637f09 to your computer and use it in GitHub Desktop.
Save anonymous/9cfe9946190242637f09 to your computer and use it in GitHub Desktop.
#!/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