Ability to kill a Thread "immediately"?

Hi,

Say the following code example:

        backend = Thread.new {
            puts "in backend"
            system("sleep 50")
            puts "done backend"
        }

        system("sleep 3")
        Thread.kill(backend)
        puts "killed backend"
        system("sleep 20")

When this program "kills" the "backend" thread, the external process
is not terminated "immediately" (the "sleep 50").

Do any of you have any idea if it would be possible to kill the thread
"immediately"? I have a real-world program in which I have a very
costly external program I'd like to interrupt immediately, under some
circumstances.

···

--
Guillaume Cottenceau - http://zarb.org/~gc/

Guillaume Cottenceau <gcottenc@gmail.com> writes:

When this program "kills" the "backend" thread, the external process
is not terminated "immediately" (the "sleep 50").

That is as expected, i.e.: a thread and a child process are two
different things.

Do any of you have any idea if it would be possible to kill the thread
"immediately"?

Use Thread.kill as you did.

I have a real-world program in which I have a very
costly external program I'd like to interrupt immediately, under some
circumstances.

And then send a SIGTERM to that external process.

YS.

Hi Yohanes,

Thanks for your reply.

> Do any of you have any idea if it would be possible to kill the thread
> "immediately"?

Use Thread.kill as you did.

> I have a real-world program in which I have a very
> costly external program I'd like to interrupt immediately, under some
> circumstances.

And then send a SIGTERM to that external process.

This may be a dummy question, but how can I know the PID of the process?

···

--
Guillaume Cottenceau - http://zarb.org/~gc/

Guillaume Cottenceau <gcottenc@gmail.com> writes:

And then send a SIGTERM to that external process.

This may be a dummy question, but how can I know the PID of the process?

rb(main):011:0> pipe = IO.popen("/bin/echo hi")
=> #<IO:0x401ac498>
irb(main):012:0> pipe.pid
=> 9113
irb(main):013:0> ps_pipe = IO.popen("ps aux|grep 9113")
=> #<IO:0x401a1a34>
irb(main):014:0> ps_pipe.gets
=> "ysantoso 9113 0.0 0.0 0 0 pts/6 Z+ 07:31 0:00
[echo] <defunct>\n"
irb(main):015:0> Process.waitall
=> [[9113, #<Process::Status: pid=9113,exited(0)>], [9114,
#<Process::Status: pid=9114,exited(0)>]]

pid 9113 is the echo process, and pid 9114 is the ps aux process.

Process.waitall/waitpid/waitpid2 is used to collect dead, uncollected
children (those with <defunct> in its process name, aka zombies).

You don't have to collect them if you don't want to. When the parent
process dies, the dead uncollected children will be collected by
process id 1 (init).

YS.