简体   繁体   English

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

[英]Is there some concurrency limitation with trap handlers in Ruby?

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 我运行以下代码,并且(一个或多个)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

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 然后“ ps axf”显示

 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? 如何设置CLD陷阱处理程序来处理所需的并发子出口数量?

The ruby version is ruby 1.9.1p243 (2009-07-16 revision 24175) [x86_64-linux] ruby版本是ruby 1.9.1p243 (2009-07-16修订版24175)[x86_64-linux]

Thanks ... 谢谢 ...

I have addressed a similar issue in Python. 我已经在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. 根据ruby实现其信号处理的方式,如果子进程在先前调用陷阱处理程序的过程中终止时终止,则可能不会再次调用陷阱处理程序。

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 -子级中每个child_pid的trap(chld)rc = waitpid(pid,WNOHANG)(如果已获得,则从列表中删除条目)

Idiomatic Ruby hints: 惯用的Ruby提示:

  1. 4.times {|i| is usually 4.times do |i| 通常是4 4.times do |i| when multiline. 多行时。
  2. pp children can be replaced with "waiting for #{children.inspect}" unless you meant the two things to be on separate lines. 可以用"waiting for #{children.inspect}"代替pp children除非您要把这两件事放在单独的行上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM