Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

eval system timeout child process

by dasibre (Initiate)
on Sep 29, 2016 at 19:22 UTC ( [id://1172950]=perlquestion: print w/replies, xml ) Need Help??

dasibre has asked for the wisdom of the Perl Monks concerning the following question:

New to perl trying to fix zombie process bug.I have a script that makes a timed system call in an eval block.

my $timeout = timed_system_call("/subprocess_a.rb", 3); if ($timeout == 0) { print "success\n"; } else { print "timed out\n"; } sub timed_system_call { my $command = shift; my $timeout = shift; # seconds my $alarm = 0; eval { local $SIG{ALRM} = sub {$alarm = 1; kill -15, $child_pid;}; alarm $timeout; system($command); alarm 0; }; die $command . " failed with " . $@ if $@ && $alarm != 1; #if alar +m is not 1, then something else caused exit e.g(ctrl-c) alarm 0; return $alarm; }
I need to, ensure the system() call subprocess is killed after timeout, without killing the parent process. On timeout the program should continue to the else block.

Replies are listed 'Best First'.
Re: eval system timeout child process
by Marshall (Canon) on Sep 29, 2016 at 21:46 UTC
Re: eval system timeout child process
by Lotus1 (Vicar) on Sep 29, 2016 at 21:26 UTC

    Hello dasibre. Have you read through perlipc? It has some examples of killing child processes and such. What OS are you running? Not all functionality is available for all platforms.

      using ubuntu-linux
Re: eval system timeout child process
by andal (Hermit) on Sep 30, 2016 at 06:52 UTC

    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.

      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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1172950]
Approved by Paladin
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-25 17:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found