waiting for multiple child processes to finish

Discussion in 'Ruby' started by Martin DeMello, Nov 13, 2006.

  1. Why does this not work (it waits for each process to finish before
    launching the next):
    -------------------------------------------------------------
    $stdout.sync = true

    10.times {|i|
    pid = Process.fork {
    puts "launching process #{i}"
    3.times {
    sleep 0.5
    puts "hello from process #{i}"
    }
    }
    Thread.new { Process.waitpid(pid) }.join
    }

    puts "done!"
    -------------------------------------------------------------

    but this does:
    -------------------------------------------------------------
    $stdout.sync = true

    pids = []
    10.times {|i|
    pids << Process.fork {
    puts "launching process #{i}"
    3.times {
    sleep 0.5
    puts "hello from process #{i}"
    }
    }
    }

    pids.each {|pid| Thread.new { Process.waitpid(pid) }.join }
    puts "done!"
    -------------------------------------------------------------
    Martin DeMello, Nov 13, 2006
    #1
    1. Advertising

  2. On 13/11/06, Martin DeMello <> wrote:
    > Why does this not work (it waits for each process to finish before
    > launching the next):
    > -------------------------------------------------------------
    > $stdout.sync = true
    >
    > 10.times {|i|
    > pid = Process.fork {
    > puts "launching process #{i}"
    > 3.times {
    > sleep 0.5
    > puts "hello from process #{i}"
    > }
    > }
    > Thread.new { Process.waitpid(pid) }.join
    > }
    >
    > puts "done!"
    > -------------------------------------------------------------
    >
    > but this does:
    > -------------------------------------------------------------
    > $stdout.sync = true
    >
    > pids = []
    > 10.times {|i|
    > pids << Process.fork {
    > puts "launching process #{i}"
    > 3.times {
    > sleep 0.5
    > puts "hello from process #{i}"
    > }
    > }
    > }
    >
    > pids.each {|pid| Thread.new { Process.waitpid(pid) }.join }
    > puts "done!"
    > -------------------------------------------------------------


    The join in the first solution forces the thread (and forked process)
    to finish before continuing onto starting the next fork. In the second
    solution all the processes are forked first before waiting. You can
    probably rewrite that last loop simply as:
    pids.each{|pid| Process.waitpid(pid)}

    Farrel
    Farrel Lifson, Nov 13, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    14,111
    Matt Humphrey
    Apr 22, 2005
  2. Jeff Rodriguez
    Replies:
    23
    Views:
    1,093
    David Schwartz
    Dec 9, 2003
  3. Behrang Dadsetan

    Waiting for processes to finish under Solaris

    Behrang Dadsetan, Jul 15, 2003, in forum: Python
    Replies:
    1
    Views:
    523
    Behrang Dadsetan
    Jul 15, 2003
  4. Brett

    Waiting for a site to finish loading

    Brett, May 28, 2005, in forum: Javascript
    Replies:
    3
    Views:
    110
    Randy Webb
    May 28, 2005
  5. matej
    Replies:
    1
    Views:
    106
    Bjoern Hoehrmann
    Jun 25, 2008
Loading...

Share This Page