Hi!
Perhaps this is more of a unix programming question but, since I am only
a newbie (in linux and ruby), I cannot think of a more satisfactory
method.
My question is: How would you kill a ruby thread and its child? I am
creating a ruby thread, and then using the Kernel.system method to call
an external program. But then, if I kill the thread, the external
program keeps running. I have ended up with some code like this:
######## Begin of code ########
class Player < Thread
attr_accessor( :child )
def initialize
@child = Array.new
super
end
def kill_tree
@child.each do |process|
Process.kill(‘KILL’, process)
end
kill
end
end
Test program
player = Player.new do
process = fork
if process.nil?
exec(‘while true; do echo “hi!”; sleep 1; done;’)
end
Thread.current.child << process
sleep(10)
puts(‘bye, parent!’)
end
sleep(5)
player.kill_tree
puts(‘bye!’)
######## End of code ########
How would you achieve the same?
Thanks in advance,
···
–
Fernando Arbeiza <URL: mailto:arbeizaf@ono.com>
Crea tu propio Linux: <URL: http://www.escomposlinux.org/lfs-es>