Ruby 中的陷阱处理程序是否存在并发限制?
我运行以下代码,(一个或多个)CLD 陷阱丢失,从而留下一个已失效(僵尸)进程,该进程尚未使用 Process.wait 收集其退出状态
require 'pp'
children = []
trap("CLD") do
cpid = Process.wait
puts "CLD from pid #{cpid} at #{Time.now}"
6.times {|i| puts " ... Waiting[#{i}] in CLD trap for pid #{cpid}"; sleep 0.5}
puts "OK, finished slow trap at #{Time.now} for pid #{cpid}"
children.delete cpid
end
4.times {|i|
if child_pid = fork # parent
puts "In parent, child_pid[#{i}] = #{child_pid}"
children.push child_pid
else
puts "In child[#{i}], PID=#{$$}"
sleep 0.2
puts "In child[#{i}], PID=#{$$} ... exiting"
exit!
end
}
while true
sleep 2
exit if children.length == 0
puts "[#{Time.now}] ... Parent still waiting for \n"
pp children
sleep 8
end
这是一个示例运行输出:
[admin@jcmsa pe2]# ruby multitrap.rb
In parent, child_pid[0] = 9285
In child[0], PID=9285
In parent, child_pid[1] = 9289
In child[1], PID=9289
In parent, child_pid[2] = 9293
In child[2], PID=9293
In parent, child_pid[3] = 9297
In child[3], PID=9297
In child[0], PID=9285 ... exiting
In child[3], PID=9297 ... exiting
In child[1], PID=9289 ... exiting
In child[2], PID=9293 ... exiting
CLD from pid 9285 at 2011-02-03 13:31:20 -0800
... Waiting[0] in CLD trap for pid 9285
CLD from pid 9289 at 2011-02-03 13:31:20 -0800
... Waiting[0] in CLD trap for pid 9289
CLD from pid 9293 at 2011-02-03 13:31:20 -0800
... Waiting[0] in CLD trap for pid 9293
... Waiting[1] in CLD trap for pid 9293
... Waiting[2] in CLD trap for pid 9293
... Waiting[3] in CLD trap for pid 9293
... Waiting[4] in CLD trap for pid 9293
... Waiting[5] in CLD trap for pid 9293
OK, finished slow trap at 2011-02-03 13:31:23 -0800 for pid 9293
... Waiting[1] in CLD trap for pid 9289
... Waiting[2] in CLD trap for pid 9289
... Waiting[3] in CLD trap for pid 9289
... Waiting[4] in CLD trap for pid 9289
... Waiting[5] in CLD trap for pid 9289
OK, finished slow trap at 2011-02-03 13:31:25 -0800 for pid 9289
... Waiting[1] in CLD trap for pid 9285
... Waiting[2] in CLD trap for pid 9285
... Waiting[3] in CLD trap for pid 9285
... Waiting[4] in CLD trap for pid 9285
... Waiting[5] in CLD trap for pid 9285
OK, finished slow trap at 2011-02-03 13:31:28 -0800 for pid 9285
[2011-02-03 13:31:28 -0800] ... Parent still waiting for
[9297]
[2011-02-03 13:31:38 -0800] ... Parent still waiting for
[9297]
[2011-02-03 13:31:48 -0800] ... Parent still waiting for
[9297]
[2011-02-03 13:31:58 -0800] ... Parent still waiting for
[9297]
...等等...
然后'ps axf'显示
9283 pts/2 Sl+ 0:00 | | \_ ruby multitrap.rb
9297 pts/2 Z+ 0:00 | | \_ [ruby] <defunct>
在我的实验中
3 children .... sometimes gets a zombie
4 children .... more often gets a zombie
5 children .... always gets a zombie, sometimes more than one
这里有什么限制?
如何设置 CLD 陷阱处理程序来处理所需数量的并发子出口?
ruby版本是ruby 1.9.1p243(2009-07-16修订版24175)[x86_64-linux]
谢谢...
I run the following code and (one or more) of the CLD traps gets lost thus leaving a defunct (zombie) process which has not had its exit status collect by use of Process.wait
require 'pp'
children = []
trap("CLD") do
cpid = Process.wait
puts "CLD from pid #{cpid} at #{Time.now}"
6.times {|i| puts " ... Waiting[#{i}] in CLD trap for pid #{cpid}"; sleep 0.5}
puts "OK, finished slow trap at #{Time.now} for pid #{cpid}"
children.delete cpid
end
4.times {|i|
if child_pid = fork # parent
puts "In parent, child_pid[#{i}] = #{child_pid}"
children.push child_pid
else
puts "In child[#{i}], PID=#{$}"
sleep 0.2
puts "In child[#{i}], PID=#{$} ... exiting"
exit!
end
}
while true
sleep 2
exit if children.length == 0
puts "[#{Time.now}] ... Parent still waiting for \n"
pp children
sleep 8
end
Here is a sample run output:
[admin@jcmsa pe2]# ruby multitrap.rb
In parent, child_pid[0] = 9285
In child[0], PID=9285
In parent, child_pid[1] = 9289
In child[1], PID=9289
In parent, child_pid[2] = 9293
In child[2], PID=9293
In parent, child_pid[3] = 9297
In child[3], PID=9297
In child[0], PID=9285 ... exiting
In child[3], PID=9297 ... exiting
In child[1], PID=9289 ... exiting
In child[2], PID=9293 ... exiting
CLD from pid 9285 at 2011-02-03 13:31:20 -0800
... Waiting[0] in CLD trap for pid 9285
CLD from pid 9289 at 2011-02-03 13:31:20 -0800
... Waiting[0] in CLD trap for pid 9289
CLD from pid 9293 at 2011-02-03 13:31:20 -0800
... Waiting[0] in CLD trap for pid 9293
... Waiting[1] in CLD trap for pid 9293
... Waiting[2] in CLD trap for pid 9293
... Waiting[3] in CLD trap for pid 9293
... Waiting[4] in CLD trap for pid 9293
... Waiting[5] in CLD trap for pid 9293
OK, finished slow trap at 2011-02-03 13:31:23 -0800 for pid 9293
... Waiting[1] in CLD trap for pid 9289
... Waiting[2] in CLD trap for pid 9289
... Waiting[3] in CLD trap for pid 9289
... Waiting[4] in CLD trap for pid 9289
... Waiting[5] in CLD trap for pid 9289
OK, finished slow trap at 2011-02-03 13:31:25 -0800 for pid 9289
... Waiting[1] in CLD trap for pid 9285
... Waiting[2] in CLD trap for pid 9285
... Waiting[3] in CLD trap for pid 9285
... Waiting[4] in CLD trap for pid 9285
... Waiting[5] in CLD trap for pid 9285
OK, finished slow trap at 2011-02-03 13:31:28 -0800 for pid 9285
[2011-02-03 13:31:28 -0800] ... Parent still waiting for
[9297]
[2011-02-03 13:31:38 -0800] ... Parent still waiting for
[9297]
[2011-02-03 13:31:48 -0800] ... Parent still waiting for
[9297]
[2011-02-03 13:31:58 -0800] ... Parent still waiting for
[9297]
... and so on ...
Then 'ps axf' shows
9283 pts/2 Sl+ 0:00 | | \_ ruby multitrap.rb
9297 pts/2 Z+ 0:00 | | \_ [ruby] <defunct>
In my experiments
3 children .... sometimes gets a zombie
4 children .... more often gets a zombie
5 children .... always gets a zombie, sometimes more than one
What is the limitation here?
How can I set up a CLD trap handler to handle as many concurrent child exits as I need?
The ruby version is ruby 1.9.1p243 (2009-07-16 revision 24175) [x86_64-linux]
Thanks ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经在 Python 中解决了类似的问题。根据 ruby 实现其信号处理的方式,如果子进程在陷阱处理程序的先前调用正在运行时终止,则陷阱处理程序可能不会再次被调用。
因此,安全的设计是使用循环并在单个陷阱处理程序运行中获取所有可能的子级。要做到这一点,假设您已经有了孩子的名单 - 这就是您的情况。
--
陷阱(儿童)
对于children中的每个child_pid
rc = waitpid(pid, WNOHANG)
如果收获则从列表中删除条目
I have addressed a similar issue in Python. Depending on how ruby implements its signal handling, it is likely that the trap handler will not be called again if a child terminates while a previous invocation of the trap handler is running.
So a safe design is to use a loop and reap all the children possible in a single trap handler run. To do this the assumption is that you already have the list of children - which is the case for you.
--
trap(chld)
for each child_pid in children
rc = waitpid(pid, WNOHANG)
if reaped then remove entry from list
惯用的 Ruby 提示:
4.times {|i|
通常是4.times do |i|
。pp Children
可以替换为"waiting for #{children.inspect}"
除非您希望将这两件事放在不同的行上。Idiomatic Ruby hints:
4.times {|i|
is usually4.times do |i|
when multiline.pp children
can be replaced with"waiting for #{children.inspect}"
unless you meant the two things to be on separate lines.