R
Robert Manea
Hello everyone,
I'm trying to solve a quite simply looking job but can't really come up
with an elegant working solution.
The why:
--------
I wrote a frontend to a DVB (digital video broadcasting) card in
Gtk2 using Perl. So I came up with the need to fork some processes away
and execute a couple of external programs via 'exec()'; these programs
are linked together via pipes.
The DVB-card is built in a remote machine so I first have to fork away a
new process and do:
exec('ssh remote-machine start-the-dvb-stuff-and-multicast-it')
after that fork away another process and:
exec('grab-the-stream | demultiplex-it | videoplayer').
The problem:
------------
As exec normally never returns and spwans a shell when it finds any
metachars in the command to be executed there is no means to get the PID
of those or the spawned shell.
But unfortunatelly I have to quit those processes every time the user
wants to switch channels via the parent.
The question:
-------------
How do I kill the forked away programms without knowing their PID?
Or better, how to kill them based on their SID?
What I tried:
-------------
(the commands where replaced by some simpler ones to make testing
easier)
#v+
#!/usr/bin/perl
#
use strict;
use warnings;
use POSIX;
$SIG{'CHLD'} = \&reaper;
sub reaper {
$SIG{'CHLD'} = \&reaper;
#my $kid = waitpid(-1, WNOHANG);
my $kid = wait();
}
my $command='tail -f foo|grep grep';
my $cpid = fork();
if ( $cpid == 0) {
print "Child starting\n";
my $s_id = POSIX::setsid(); # Put the stuff into an own session
exec $command;
} elsif ($cpid < 0) {
warn "Cannot fork: $!";
}
sleep 5; # Actually the parent runs forever
kill 9, $cpid;
sleep 5;
return ;
__END__
#v-
This doesn't seem to work as the 'exec()'ed commands exist further on
after killing the child process.
The work around:
----------------
Simply remember the 'exec()'ed command names and do a
system('pkill -9 grab-the-stream') and so on..
This looks more than ugly and I'm sure there are way nicer methods.
Greets & TIA, Rob
I'm trying to solve a quite simply looking job but can't really come up
with an elegant working solution.
The why:
--------
I wrote a frontend to a DVB (digital video broadcasting) card in
Gtk2 using Perl. So I came up with the need to fork some processes away
and execute a couple of external programs via 'exec()'; these programs
are linked together via pipes.
The DVB-card is built in a remote machine so I first have to fork away a
new process and do:
exec('ssh remote-machine start-the-dvb-stuff-and-multicast-it')
after that fork away another process and:
exec('grab-the-stream | demultiplex-it | videoplayer').
The problem:
------------
As exec normally never returns and spwans a shell when it finds any
metachars in the command to be executed there is no means to get the PID
of those or the spawned shell.
But unfortunatelly I have to quit those processes every time the user
wants to switch channels via the parent.
The question:
-------------
How do I kill the forked away programms without knowing their PID?
Or better, how to kill them based on their SID?
What I tried:
-------------
(the commands where replaced by some simpler ones to make testing
easier)
#v+
#!/usr/bin/perl
#
use strict;
use warnings;
use POSIX;
$SIG{'CHLD'} = \&reaper;
sub reaper {
$SIG{'CHLD'} = \&reaper;
#my $kid = waitpid(-1, WNOHANG);
my $kid = wait();
}
my $command='tail -f foo|grep grep';
my $cpid = fork();
if ( $cpid == 0) {
print "Child starting\n";
my $s_id = POSIX::setsid(); # Put the stuff into an own session
exec $command;
} elsif ($cpid < 0) {
warn "Cannot fork: $!";
}
sleep 5; # Actually the parent runs forever
kill 9, $cpid;
sleep 5;
return ;
__END__
#v-
This doesn't seem to work as the 'exec()'ed commands exist further on
after killing the child process.
The work around:
----------------
Simply remember the 'exec()'ed command names and do a
system('pkill -9 grab-the-stream') and so on..
This looks more than ugly and I'm sure there are way nicer methods.
Greets & TIA, Rob