views:

33

answers:

3

I want to limit the subprocesses count to 3. Once it hits 3 i wait until one of the processes stops and then execute a new one. I'm using Kernel.fork to start the process.

How do i get the number of running subprocesses? or is there a better way to do this?

+1  A: 

A good question, but I don't think there's such a method in Ruby, at least not in the standard library. There's lots of gems out there....

This problem though sounds like a job for the Mutex class. Look up the section Condition Variables here on how to use Ruby's mutexes.

Jesse J
Thank you, your link pointed me to the right answer.
giancarlo
A: 

I usually have a Queue of tasks to be done, and then have a couple of threads consuming tasks until they receive an item indicating the end of work. There's an example in "Programming Ruby" under the Thread library. (I'm not sure if I should copy and paste the example to Stack Overflow - sorry)

Andrew Grimm
A: 

My solution was to use trap("CLD"), to trap SIGCLD whenever a child process ended and decrease the counter (a global variable) of processes running.

giancarlo