secret
Created

  • Download Gist
gistfile1.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#!/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);
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.