sub childDone {
# Resource is freed up so place it back in the queue to be used for future toasts.
# $SIG{CHLD} = \&childDone;
my $chld;
$chld = waitpid(-1, WNOHANG) until $chld;
return unless $chld > 0;
#print "\n$$ sees $chld is done";
system(`touch /tmp/child.$chld`);
# Toasting using this resource is now done
my $str="toasting." . $jobs{$chld};
$cache->set($str,0);
#print "\tresource " . $jobs{$chld} . " is free again!\n";
push @resource, $jobs{$chld};
#print "The resources to use are: @resource\n";
delete $jobs{$chld};
alarm 0; # send SIGALRM so sleep() wakes up
};
$SIG{CHLD} = \&childDone;
####
@resources = {cdrw0 cdrw1 cdrw2}
my $chld;
while (@thisround) {
my @sort_resource = sort(@resource);
@resource = @sort_resource;
while (@resource) {
last unless @thisround;
my $resource = shift @resource;
my $data = shift @thisround;
if (($chld = fork()) > 0) {
# parent code
# % jobs holds pid -> resource
$jobs{$chld} = $resource;
} else {
# child code
runcmd($resource, $data);
exit; # end chld, trigger $SIG{CHLD}
}
}
sleep until @resource == $num_resources;
last unless @thisround;
}
sleep until keys %jobs == 0; # wait for all jobs
####
my $pid = open (SAFEKID, "-|");
if ( $pid == 0 ) {
open STDERR, ">/dev/null";
exec("cdrecord", "-immed", "-dummy", "-eject", "gracetime=$gracetime", "-tao", "dev=$resource", "$distroDirs{$distro}/$data");
} else {
my @output = ;
close SAFEKID;
}