I know a lot of questions regarding waitpid and timeouts, but they all cover this, killing a child from an alarm handler.
This is not what I want, I want the process to execute, but send it from waitpid.
The problem with the litter that I am trying to solve is the daemon process with the main loop that processes the queue. Tasks are processed in turn.
If the task hangs, the entire main loop freezes. Getting around this fork() and waitpid seemed like an obvious choice. However, if the task hangs, the loop freezes.
I can think of workarounds when I do not use waitpid at all, but I will have to monitor the running processes in a different way, because I still want to process one task at a time in parallel with possible possible freezing of tasks.
I could even kill a task, but I would like to run it to check what exactly is going wrong. A leak handler is also possible that provides some debugging information.
In any case, the most convenient way to solve this problem is to waitpid timeout, if you have one.
Edit:
This is how I used fork () and waitpid, and it can be clearer what is meant by a child.
my $pid = fork(); if ($pid == 0){
Edit:
Using waitpid WNOHANG does what I want. Is this use good practice or will you do it differently?
use strict; use warnings; use 5.012; use POSIX ':sys_wait_h'; my $pid = fork(); if ($pid == 0){ say "child will sleep"; sleep 20; say "child slept"; } else { my $time = 10; my $status; do { sleep 1; $status = waitpid -1, WNOHANG; $time--; } while ($time && not $status ); say "bye"; }