On Mon, Mar 18, 2013 at 10:12 AM, Ken Paul <lists / ruby-forum.com> wrote:
> Is there a way to know how many child processes that have been forked by
> its parent process?

Yes, just record the PID in a Set or Array.

> The forked process may exit when it finishes, so if
> parent process needs to know the status, it has to check it regularly.

That's not how you typically do it.  Typically you will fork child
processes and then wait for them to terminate once you are doing with
your work in the main process.  If you want to limit the total number
of processes created you could store work items in a queue, start n
processes initially and then wait for one process to terminate.  As
long as there is work in the queue fork a new process.

> I'm guess this number could be used to control the total numbers
> processes to avert too many resources exhausted. Once the number is
> reduced, new child could be forked.

You just need to wait for termination (see above).
http://www.ruby-doc.org/core-1.9.3/Process.html

Example:

#!/usr/bin/ruby

$-w = true

require 'set'

N = 4

# work items are sleep seconds
queue = 20.times.map { 2 + rand(5) }

processes = Set.new

until queue.empty?
  if processes.size >= N
    pid = Process.wait
    processes.delete pid
  end

  sl = queue.shift

  pid = fork do
    printf  "PID %5d: Start\n", $$
    sleep sl
    printf  "PID %5d: Stop\n", $$
  end

  processes.add pid
end

puts "Waiting for remaining processes..."
Process.waitall
puts "Done"



-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/