Zombie Processes Appearing When I Spawn Processes
Asked Answered
G

2

6

I have a pieces of code where i spawn off children processes to make it more efficient. However, they appear to create all sorts of zombie processes which block sockets and bring down the site.

spawn(:method => :thread) do
   if @login_user.suggested_group_info.new_record?
       xxx
   end
end

1) Why is this creating zombie processes? 2) How could i write the code such that i make sure i kill the process before it becomes a zombie?

Glinys answered 10/3, 2010 at 23:29 Comment(0)
S
4

You have to save the PID of the spawned process and execute the waitpid(2) system call upon it after it dies. (I don't know how Ruby does this.)

Snowfall answered 10/3, 2010 at 23:58 Comment(1)
Ruby does this pretty much as you'd expect. spawn returns a pid; a call to Process.waitpid(pid) waits for the process to exit and then reaps the zombie.Mouth
S
2

You can also trap for the child shutdown which will clean up the zombie process

trap("CLD") {
  pid = Process.wait
  puts "Child pid #{pid}: terminated"
}
Salable answered 1/8, 2012 at 20:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.