Skip to content

Commit

Permalink
Allow to run forkcmd() and waitcmd() interleaved.
Browse files Browse the repository at this point in the history
  • Loading branch information
bluhm committed Jan 21, 2024
1 parent d63b628 commit 716262a
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions Logcmd.pm
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,40 @@ sub forkcmd {
}
_exit(126);
}
return $pid => [@cmd];
return ($pid => [@cmd]);
}

my %waitstatus;
sub waitcmd {
my %pidcmds = @_;
my $total = keys %pidcmds;
my $failed = 0;
while (my $pid = each %waitstatus) {
my $cmd = delete $pidcmds{$pid}
or next;
# someone else has waited for our process
my $status = delete $waitstatus{$pid};
my @cmd = @$cmd;
if ($status) {
logeval { croak "Command '@cmd' failed: $status" };
$failed++;
} else {
logmsg "Command '@cmd' finished.\n";
}
}
while (keys %pidcmds) {
(my $pid = wait) == -1
(my $pid = wait()) == -1
and croak "Wait failed: $!";
my $cmd = delete $pidcmds{$pid}
or croak "Wait for pid $pid without command";
my $status = $?;
my $cmd = delete $pidcmds{$pid};
unless ($cmd) {
# we have waited for someone else's process
$waitstatus{$pid} = $status;
next;
}
my @cmd = @$cmd;
if ($?) {
logeval { croak "Command '@cmd' failed: $?" };
if ($status) {
logeval { croak "Command '@cmd' failed: $status" };
$failed++;
} else {
logmsg "Command '@cmd' finished.\n";
Expand Down

0 comments on commit 716262a

Please sign in to comment.