Execute process in Perl with options to stop, resume, kill using IPC
I have almost no idea about forking. I tried to research but I couldn't find a simple example on how to do this. For windows, I found a nice module and wrote this piece of code that does what I want.
Win32::Process::Create( my $ProcessObj,
"$jobs{$id}->{path}", "execute job", 0, NORMAL_PRIORITY_CLASS, "." )
|| die ErrorReport();
print "Available commands:\n1.Suspend\n2.Resume\n3.Kill\n";
while (1) {
chomp( my $input = <STDIN> );
if ( $input eq "1" ) {
$ProcessObj->Suspend();
}
if ( $input eq "2" ) {
$ProcessObj->Resume();
}
if ( $input eq "3" ) {
print "Returned to main menu.\n";
$ProcessObj->Kill(0);
return;
}
}
So my question is, is there a way to do this with forking? And here is my attempt at forking:
unless ( $pid = fork) {
unless (fork) {
exec "$jobs{$id}->{path}";
die "exec failed!";
}
exit 0;
}
waitpid($pid, 0);
I have a program that prints Hello world every 3 seconds and I want to pause, resume and kill it if this example helps.
source to share
Forks::Super
makes it simple and platform independent.
use Forks::Super;
...
my $pid = fork { exec => $jobs{$id}->{path} };
...
$pid->suspend;
...
$pid->resume;
...
$pid->kill; # or $pid->kill('TERM'), $pid->kill('QUIT'), etc.
source to share
If you must go manually, the following signals are used: 'SIGSTOP'
and 'SIGCONT'
.
Command line demo
perl -wE'
$pid = fork // die "Cant fork: $!";
if ($pid == 0) {
for (1..6) { say "\tkid ..."; sleep 1; };
say "\tKID DONE"; exit;
};
sleep 3;
kill "STOP", $pid;
for (1..2) { say "Parent here!"; sleep 1};
kill "CONT", $pid;
wait;
'
prints
kid ... kid ... kid ... Parent here! Parent here! kid ... kid ... kid ... KID DONE
Then you need to implement this in your STDIN
-driven control .
But I suggest trying to allow Forks :: Super to be installed , from mob's answer.
A STDIN
controlled example. The bifurcation process and the parent file are written to the file for the test.
use warnings;
use strict;
use feature 'say';
#use IO::Handle; # needed pre v5.16 (for autoflush)
my $fh_kid;
$SIG{INT} = sub { close $fh_kid; exit 1 };
my $file = 'kidfile.out';
open $fh_kid, '>', $file or die "Can't open $file: $!";
$fh_kid->autoflush;
my $pid = fork // die "Can't fork: $!";
if ($pid == 0) {
$SIG{TERM} = sub { close $fh_kid; exit 1 };
for (1..20) {
say $fh_kid "\tkid, $_";
sleep 1;
}
exit;
}
say "Parent: started $pid";
while (1) {
chomp (my $input = <STDIN>);
if (not $input) {
close $fh_kid;
last;
}
if ($input == 1) {
kill 'STOP', $pid;
say "Parent: STOP-ed $pid";
say $fh_kid "Parent STOP-ed $pid";
}
elsif ($input == 2) {
say "Parent: CONT the $pid";
kill 'CONT', $pid;
}
elsif ($input == 3) {
close $fh_kid;
kill 'TERM', $pid;
say "Parent: TERM-ed the $pid";
}
}
my $gone = waitpid $pid, 0;
if ($gone > 0) { say "Child $gone exited with: $?" }
elsif ($gone < 0) { say "No such process ($gone), reaped already?" }
else { say "Still out there?" }
This requires more details, but it does show which things are involved.
Exit (with comments)
Parent: started 19628 1 # entered a few seconds after starting Parent: STOP-ed 19628 2 # after waiting for a minute Parent: CONT the 19628 3 # after waiting for a few more seconds Parent: TERM-ed the 19628 ^ C # quit STDIN
We let the kid print to the file for a few seconds (so a few times) STOP
and then wait for him (1) and then wait a bit and then CONT
inject the child (2) and let him print a few more times before killing him (3 ).
The output kidfile.out
has a few lines from the child, then a line from the parent, and then a few more lines from the child, confirming that the child has been paused, resumed, and stopped.
source to share