vadviktor
7/18/2015 - 10:57 AM

Wait for all of the forked child processes to finish

Wait for all of the forked child processes to finish

pids = []
(1..(ENV['CPUCORES'] || 3)).each do
  pids << fork do

    # do your stuff

  end
end

# hold your breath until all children finish
until pids.all? { |p|
  !system("ps --no-headers -p #{p} > /dev/null 2>&1") ||
      system("ps --no-headers -p #{p} | grep defunct> /dev/null 2>&1")
}
  sleep 1
end