Threads and daemons

Discussion in 'Ruby' started by Cd Cd, Sep 12, 2007.

  1. Cd Cd

    Cd Cd Guest

    The getty is a daeamon

    When I do soemthing like

    #!/usr/local/bin/ruby

    threads=[]

    4.times do |i|
    threads=Thread.new do
    %x{/usr/libexec/getty std.19200 tty00}
    end
    end


    I see the following
    -bash-3.2$ ps -j
    cd 1877 10367 1877 d6bd16c0 1 I ph 0:00.01
    /usr/local/bin/rub
    cd 29342 1877 1877 d6bd16c0 1 I ph 0:00.00
    /usr/libexec/getty
    cd 2773 1877 1877 d6bd16c0 1 I ph 0:00.00 /usr/libexec/getty
    cd 30538 1877 1877 d6bd16c0 1 I ph 0:00.01
    /usr/libexec/getty
    cd 6442 1877 1877 d6bd16c0 1 I ph 0:00.01
    /usr/libexec/getty

    Each thread is creating a new process.

    However, when I change to the same code to a program that isnt a unix
    daemon

    like

    #!/usr/local/bin/ruby

    threads=[]

    4.times do |i|
    threads=Thread.new do
    %x{/usr/local/bin/party
    }
    end
    end

    I see the following
    -bash-3.2$ ps -j
    cd 29735 15232 29735 d6bd1d80 0 Is+ ph 0:00.06 -bash (bash)
    cd 12524 29735 12524 d6bd1d80 1 T ph 0:00.02
    /usr/local/bin/rub


    In this case, the thread isn't creating a process. Can someone shed some
    light on this behavior or discrepancy.
    --
    Posted via http://www.ruby-forum.com/.
    Cd Cd, Sep 12, 2007
    #1
    1. Advertising

  2. Cd Cd

    MenTaLguY Guest

    --=-k2V859oTR9eGflf1LDux
    Content-Type: text/plain
    Content-Transfer-Encoding: quoted-printable

    On Wed, 2007-09-12 at 10:15 +0900, Cd Cd wrote:
    > In this case, the thread isn't creating a process. Can someone shed some
    > light on this behavior or discrepancy.


    It sounds like in the latter case, most of the threads aren't living
    long enough to create the process. If you don't want your program to
    exit early and kill some or all of the threads, you need your main
    thread to call Thread#join on each one of them.

    -mental

    --=-k2V859oTR9eGflf1LDux
    Content-Type: application/pgp-signature; name=signature.asc
    Content-Description: This is a digitally signed message part

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.6 (GNU/Linux)

    iD4DBQBG50UHSuZBmZzm14ERAiXKAKCAwIptd6avHT8s3dsP4U3OcGAHewCYpRC8
    YHgcn/Go7WgdbLsR4YJUBA==
    =eU/V
    -----END PGP SIGNATURE-----

    --=-k2V859oTR9eGflf1LDux--
    MenTaLguY, Sep 12, 2007
    #2
    1. Advertising

  3. Cd Cd

    Cd Cd Guest

    Mental Guy wrote:
    > On Wed, 2007-09-12 at 10:15 +0900, Cd Cd wrote:
    >> In this case, the thread isn't creating a process. Can someone shed some
    >> light on this behavior or discrepancy.

    >
    > It sounds like in the latter case, most of the threads aren't living
    > long enough to create the process. If you don't want your program to
    > exit early and kill some or all of the threads, you need your main
    > thread to call Thread#join on each one of them.
    >
    > -mental


    Okay, bear with this. When I copied the program, I forgot part of it -(
    Here is the entire thing.

    #!/usr/local/bin/ruby

    threads=[]

    4.times do |i|
    threads=Thread.new do
    %x{/usr/local/bin/party}
    end
    end

    threads.each{|thr| thr.join}

    So do I move join into the do/end block?
    --
    Posted via http://www.ruby-forum.com/.
    Cd Cd, Sep 12, 2007
    #3
  4. Cd Cd

    Bob Proulx Guest

    Cd Cd wrote:
    > Okay, bear with this. When I copied the program, I forgot part of it -(
    > Here is the entire thing.


    I think your "party" process is exiting very quickly. I think there
    is a problem when it is run like this and since the output from the
    program is being discarded you are not seeing it.

    > #!/usr/local/bin/ruby
    >
    > threads=[]
    >
    > 4.times do |i|
    > threads=Thread.new do
    > %x{/usr/local/bin/party}


    Ouch. Please think about indention here. That line should be
    indented one more level than the previous line. And an indention of
    eight is pretty big for one level of indention.

    > end
    > end
    >
    > threads.each{|thr| thr.join}


    The %x{} is executing the party program but any output is being
    discarded. Personally I think using 'system' is better in that case
    because it does not need to collect the output at all. Using %x{...}
    or using `...` means that ruby needs to wait until the process has
    terminated and then do something with the string of output collected.
    In this case the do something is nothing and it is garbage collected
    but then in that case I think it is better not to collect it at all.

    Whenever I see `...` or %x{...} without it being an assignment it
    triggers me to question it.

    > So do I move join into the do/end block?


    Try this modification to your program:

    #!/usr/bin/env ruby
    threads = []
    4.times do
    threads << Thread.new do
    output = %x{/usr/local/bin/party}
    puts "party output: " + output
    end
    end
    threads.each{|thr| thr.join}
    exit 0

    Is there any output from your "party" program? I am hoping there will
    be errors shown there that were not displayed before that will lead
    you to the problem.

    When I create a simply /tmp/testdaemon script like this:

    #!/usr/bin/env ruby
    puts "Hello from testdaemon."
    exit 0

    And then run your example like this:

    #!/usr/bin/env ruby
    threads = []
    4.times do
    threads << Thread.new do
    output = %x{/tmp/testdaemon}
    puts "party output: " + output
    end
    end
    threads.each{|thr| thr.join}
    exit 0

    Then I see this output:

    party output: Hello from testdaemon.
    party output: Hello from testdaemon.
    party output: Hello from testdaemon.
    party output: Hello from testdaemon.

    Bob
    Bob Proulx, Sep 12, 2007
    #4
  5. Cd Cd

    Cd Cd Guest

    >
    > Whenever I see `...` or %x{...} without it being an assignment it
    > triggers me to question it.
    >
    >> So do I move join into the do/end block?

    >
    > Try this modification to your program:
    >
    > #!/usr/bin/env ruby
    > threads = []
    > 4.times do
    > threads << Thread.new do
    > output = %x{/usr/local/bin/party}
    > puts "party output: " + output
    > end
    > end
    > threads.each{|thr| thr.join}
    > exit 0
    >
    > Is there any output from your "party" program? I am hoping there will
    > be errors shown there that were not displayed before that will lead
    > you to the problem.
    >


    I see
    Welcome to PARTY! Type '?' for help:

    When I step through the program via the ruby debugger. Sometimes ill see
    the same thing when I run the program. It varies from run to run
    --
    Posted via http://www.ruby-forum.com/.
    Cd Cd, Sep 12, 2007
    #5
  6. Cd Cd

    Cd Cd Guest

    Cd Cd wrote:
    >>
    >> Whenever I see `...` or %x{...} without it being an assignment it
    >> triggers me to question it.
    >>
    >>> So do I move join into the do/end block?

    >>
    >> Try this modification to your program:
    >>
    >> #!/usr/bin/env ruby
    >> threads = []
    >> 4.times do
    >> threads << Thread.new do
    >> output = %x{/usr/local/bin/party}
    >> puts "party output: " + output
    >> end
    >> end
    >> threads.each{|thr| thr.join}
    >> exit 0
    >>
    >> Is there any output from your "party" program? I am hoping there will
    >> be errors shown there that were not displayed before that will lead
    >> you to the problem.
    >>

    >
    > I see
    > Welcome to PARTY! Type '?' for help:
    >
    > When I step through the program via the ruby debugger. Sometimes ill see
    > the same thing when I run the program. It varies from run to run


    And on an interesting side note, the status of the program is sleeping
    when I run your version
    10123 0.0 0.4 1076 1900 pb S+ 12:39AM 0:00.01 ruby ./party.rb

    However, when I run my version, the status of the program show that it
    has been stopped.
    --
    Posted via http://www.ruby-forum.com/.
    Cd Cd, Sep 12, 2007
    #6
  7. Cd Cd

    Cd Cd Guest

    Cd Cd wrote:
    > Cd Cd wrote:
    >>>
    >>> Whenever I see `...` or %x{...} without it being an assignment it
    >>> triggers me to question it.
    >>>
    >>>> So do I move join into the do/end block?
    >>>
    >>> Try this modification to your program:
    >>>
    >>> #!/usr/bin/env ruby
    >>> threads = []
    >>> 4.times do
    >>> threads << Thread.new do
    >>> output = %x{/usr/local/bin/party}
    >>> puts "party output: " + output
    >>> end
    >>> end
    >>> threads.each{|thr| thr.join}
    >>> exit 0
    >>>
    >>> Is there any output from your "party" program? I am hoping there will
    >>> be errors shown there that were not displayed before that will lead
    >>> you to the problem.
    >>>

    >>


    There are two errors that I haven't before.

    1)When I hit ctrl-c, I see the following
    /party.rb:9:in `join': Interrupt
    from ./party.rb:9
    from ./party.rb:9:in `each'
    from ./party.rb:9

    2)Then, when I'm at the bash prompt, anything I type doesn't echo back.
    --
    Posted via http://www.ruby-forum.com/.
    Cd Cd, Sep 12, 2007
    #7
  8. Cd Cd

    Bob Proulx Guest

    Cd Cd wrote:
    > I see
    > Welcome to PARTY! Type '?' for help:


    You said it was a daemon? But the above looks like an interactive
    program to me.

    The %x{...} will wait for the program to exit and take the output that
    it collected and assign it or garbage collect it. But if your program
    is interactive and does not exit then the %x{...} can't either since
    it needs to wait for it to exit.

    If you want your "party" program to be a daemon then it needs to be
    modified to be a non-interactive daemon program.

    > When I step through the program via the ruby debugger. Sometimes ill see
    > the same thing when I run the program. It varies from run to run


    Varying run to run does not sound good either.

    Bob
    Bob Proulx, Sep 12, 2007
    #8
    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. about daemons and IPC

    , Aug 27, 2006, in forum: Python
    Replies:
    4
    Views:
    320
    Irmen de Jong
    Aug 29, 2006
  2. PurpleServerMonkey

    Python, Daemons and D-Bus

    PurpleServerMonkey, May 24, 2008, in forum: Python
    Replies:
    3
    Views:
    341
    Diez B. Roggisch
    May 25, 2008
  3. Fernando M. Maresca

    logging and daemons

    Fernando M. Maresca, Feb 16, 2009, in forum: Python
    Replies:
    1
    Views:
    293
    Diez B. Roggisch
    Feb 16, 2009
  4. Fernando M. Maresca

    Re: logging and daemons

    Fernando M. Maresca, Feb 16, 2009, in forum: Python
    Replies:
    2
    Views:
    319
    Fernando M. Maresca
    Feb 16, 2009
  5. Davertron

    Daemons and Paths

    Davertron, Mar 14, 2009, in forum: Ruby
    Replies:
    3
    Views:
    131
    Davertron
    Mar 16, 2009
Loading...

Share This Page