I have a script which needs to periodically start programs out of a array with program names via Perl on Linux. The problem is that from time to time one of the programs takes too long/hangs and needs to be aborted.
Currently I am starting the program using qx/$cmd/
in a seperate thread which reads from a shared start queue. The main thread enqueues every x seconds a new element into the queue.
If there are elements in the queue, the main thread kills the child thread and starts a new child.
This works fine from a functional perspective, but now I have realised that this leads to a memory leak. How would you design such a program ? Is there any CPAN module which can help? Please let me know if you need further code to understand the problem.
The main thread looks like this:
if (!$startQueue->pending) {
$startQueue->enqueue($programList[$i++]);
} else {
$log->warn("Aborting hanging execution");
$starterThread->kill('KILL')->detach();
$log->info("Creating new thread");
$starterThread=threads->create("starterThread");
}
The child thread like this:
sub starterThread{
$SIG{'KILL'}=sub{threads->exit();};
$log->info("Starter Thread started");
while() {
my $programName=$startQueue->dequeue();
$log->debug("programName:$programName");
qx/$programName/;
}
}