Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: eval system timeout child process

by andal (Hermit)
on Sep 30, 2016 at 06:52 UTC ( #1172978=note: print w/replies, xml ) Need Help??

in reply to eval system timeout child process

If your problem is with zombie processes, then you should simply do waiting for your child process. There's such function 'wait' or 'waitpid'. In general, if you start child process and then don't do 'wait' on it, then that process after termination shall become 'zombie'. Zombies disappear when their parents finish their work. An alternative to calling 'wait' is to put $SIG{CHLD}="IGNORE"; indicating, that you are not interested in the status of child processes.

From your code it is not clear where do you get the $child_pid from. Generally, the 'system' call does 'wait' for child. When you abort 'system' via ALRM signal, that waiting does not happen, so you get zombie. I don't know if setting SIG{CHLD} would affect 'system' functionality. Alternatively, you can call 'waitpid(-1, WNOHANG)' after you detected that alarm has happened.

Replies are listed 'Best First'.
Re^2: eval system timeout child process
by dasibre (Initiate) on Sep 30, 2016 at 09:44 UTC
    thanks for your explanation andal. the current code doesn't get the child pid, thats one of the things i was trying to figure out how to do using system(). Most of the examples i've seen use fork+exec then kill the forked process. That doesn't work in my situation because it exec doesn't return to the program, unless theres an error according to perldocs.
      because it exec doesn't return to the program, unless theres an error

      That's because exec doesn't create a new process. It replaces the calling program with the specified program, running it in the same process.

      In Linux/Unix/POSIX based systems, the PID returned by fork to the parent is the child you wait for (or kill, if need be).

      my $pid = fork(); unless defined $pid { warn "Can't fork: $!\n"; ...; # do something else exit; } if ($pid == 0) { # child exec @cmd_and_parameters; die "Could not exec $cmd_and_parameters[0]: $!\n"; } else { # parent ...; # do other things waitpid($pid,0) == $pid or die "Error waiting for child: $!\n"; ...; # do more (if needed) }

      Using, for example, Parallel::ForkManager will make this easier, as well as hide any ugly details for OSs that do not have fork (like MS Windows).

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1172978]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2022-12-03 10:05 GMT
Find Nodes?
    Voting Booth?

    No recent polls found