Forked child processes

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.

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.

On Mon, Mar 18, 2013 at 10:12 AM, Ken P. [email protected] 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).

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”

I think it’s the -w command line argument for Ruby, it means show
warnings.

Robert K. 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”?

On Mon, Mar 18, 2013 at 3:39 PM, Joel P. [email protected]
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