-
-
Save anonymous/9cfe9946190242637f09 to your computer and use it in GitHub Desktop.
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
#!/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