gpt4 book ai didi

ruby - Ruby 中的陷阱处理程序是否存在一些并发限制?

转载 作者:太空宇宙 更新时间:2023-11-04 11:39:27 26 4
gpt4 key购买 nike

我运行以下代码,(一个或多个)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 revision 24175) [x86_64-linux]

谢谢...

最佳答案

我已经解决了 Python 中的类似问题。根据 ruby​​ 如何实现其信号处理,如果子进程终止而陷阱处理程序的先前调用正在运行,则可能不会再次调用陷阱处理程序。

因此,一个安全的设计是使用一个循环,并在一次陷阱处理程序运行中收集所有可能的 child 。要做到这一点,假设您已经有了 child 的列表 - 这就是您的情况。

--陷阱( child ) 对于每个 child_pid in children rc = waitpid(pid, WNOHANG) 如果收获则从列表中删除条目

关于ruby - Ruby 中的陷阱处理程序是否存在一些并发限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4892208/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com