Ruby threads and the system call

Discussion in 'Ruby' started by Vincent Fourmond, Aug 27, 2006.

  1. --------------090005050106010103080805
    Content-Type: text/plain; charset=ISO-8859-1
    Content-Transfer-Encoding: 7bit


    Hello !

    I've recently had quite a fair bit of trouble with Ruby threads (on a
    Linux box). I wrote three different programs (attached) that write
    numbers from within threads:

    * one with a standard puts
    * one with a system "echo ..."
    * one with a fork do system "echo ..." end

    The first one behaves as expected (although the numbers are perfectly
    ordered, which looks suspicious). In the second one, ruby never manages
    to make a second system call in a thread (and finishes before the
    subprograms are terminated). The third behaves a little better, but
    crashes after, say, 3 to 4 system calls in a thread...

    Is this true on other boxes ? Is it a flaw of my understanding of
    threads, or a limitation inherent to the way threads are coded in Ruby ?
    If that is so, it's really annoying - no way to delegate heavy tasks to
    other well-written programs...

    Thoughts ?

    Vince

    --------------090005050106010103080805
    Content-Type: text/plain;
    name="test_threads_no_system.rb"
    Content-Transfer-Encoding: 7bit
    Content-Disposition: inline;
    filename="test_threads_no_system.rb"

    #!/usr/bin/ruby

    10.times do |i|
    Thread.start(i) do |i|
    10.times do |j|
    puts "#{i}.#{j}"
    end
    end
    end


    --------------090005050106010103080805
    Content-Type: text/plain;
    name="test_threads_system.rb"
    Content-Transfer-Encoding: 7bit
    Content-Disposition: inline;
    filename="test_threads_system.rb"

    #!/usr/bin/ruby

    10.times do |i|
    Thread.start(i) do |i|
    10.times do |j|
    system "echo #{i}.#{j} "
    end
    end
    end


    --------------090005050106010103080805
    Content-Type: text/plain;
    name="test_threads_system_fork.rb"
    Content-Transfer-Encoding: 7bit
    Content-Disposition: inline;
    filename="test_threads_system_fork.rb"

    #!/usr/bin/ruby

    10.times do |i|
    Thread.start(i) do |i|
    10.times do |j|
    fork do
    system "echo #{i}.#{j} "
    end
    end
    end
    end


    --------------090005050106010103080805--
    Vincent Fourmond, Aug 27, 2006
    #1
    1. Advertising

  2. Hello !

    >
    > 10.times { |i|
    > x = Thread.new(i) {
    > 10.times { |j|
    > fork {
    > system "echo #{i}, #{j} >> temp.txt"
    > }
    > Process::wait
    > }
    > }
    > x.join


    Great thanks, I had completely forgotten to call join for the
    threads... By the way, the version with fork is around 4 times faster on
    my box:

    ruby test_threads_system.rb > /dev/null 0.04s user 0.10s system 21% cpu
    0.639 total
    ruby test_threads_system_fork.rb > /dev/null 0.00s user 0.04s system
    25% cpu 0.171 total

    Cheers and thanks !!

    Vince
    Vincent Fourmond, Aug 27, 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. system call and library call

    , Aug 22, 2005, in forum: C Programming
    Replies:
    7
    Views:
    2,997
    Jonathan Bartlett
    Aug 23, 2005
  2. leoman730

    System call and library call

    leoman730, May 3, 2007, in forum: C++
    Replies:
    6
    Views:
    905
    Jim Langston
    May 6, 2007
  3. David Pratt
    Replies:
    0
    Views:
    250
    David Pratt
    Apr 7, 2008
  4. Une bévue
    Replies:
    0
    Views:
    131
    Une bévue
    Jun 14, 2006
  5. vhaerun vh
    Replies:
    16
    Views:
    272
    Eleanor McHugh
    Sep 8, 2009
Loading...

Share This Page