ability to kill a Thread "immediately"?

  • Thread starter Guillaume Cottenceau
  • Start date
G

Guillaume Cottenceau

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.
 
Y

Yohanes Santoso

Guillaume Cottenceau said:
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.
 
G

Guillaume Cottenceau

Hi Yohanes,

Thanks for your reply.
Use Thread.kill as you did.


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?
 
Y

Yohanes Santoso

Guillaume Cottenceau said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
ScottChare

Latest Threads

Top