Forum: Ruby forked child processes

Posted by Ken Paul (kenfc)
on 2013-03-18 10:12
Hi,

Is there a way to know how many child processes that have been forked by
its parent process? The forked process may exit when it finishes, so if
parent process needs to know the status, it has to check it regularly.

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.

Thanks.
Posted by Joel Pearson (virtuoso)
on 2013-03-18 10:22
As far as I know you'd have to set up a counter within the parent and 
increment / decrement it within the threads.

However, before moving into multiple threads you should do a bit of 
benchmarking to ensure that it's the best way to proceed. You'll often 
find that multithreading isn't your best option as the performance 
bottleneck may be something that threads won't help with.
Posted by Robert Klemme (robert_k78)
on 2013-03-18 10:49
(Received via mailing list)
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"
Posted by Ken Paul (kenfc)
on 2013-03-18 14:13
Robert Klemme wrote in post #1102059:

Thanks for the detailed explanation with a great example.

>
> #!/usr/bin/ruby
>
> $-w = true
>

What does this special variable means "$-w"?
Posted by Joel Pearson (virtuoso)
on 2013-03-18 15:39
I think it's the -w command line argument for Ruby, it means show 
warnings.
Posted by Robert Klemme (robert_k78)
on 2013-03-18 15:47
(Received via mailing list)
On Mon, Mar 18, 2013 at 3:39 PM, Joel Pearson <lists@ruby-forum.com> 
wrote:
> I think it's the -w command line argument for Ruby, it means show
> warnings.

Exactly.

$ ruby -e 'p $-w'
false
$ ruby -w -e 'p $-w'
true

Kind regards

robert
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.