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