Independent processes

Discussion in 'Ruby' started by Tasos Laskos, Dec 18, 2010.

  1. Tasos Laskos

    Tasos Laskos Guest

    Hi guys,

    I'm having a bit of a problem...
    Is there a way to completely detach child processes?

    What I mean is, if I Kernel.fork() a piece of code and then the parent
    gets killed I want the child to continue running.

    Does anyone have an idea of how to do that?

    Cheers,
    Tasos L.

    --
    Posted via http://www.ruby-forum.com/.
    Tasos Laskos, Dec 18, 2010
    #1
    1. Advertising

  2. On Dec 18, 2010, at 12:10 PM, Tasos Laskos wrote:

    > I'm having a bit of a problem...
    > Is there a way to completely detach child processes?
    >
    > What I mean is, if I Kernel.fork() a piece of code and then the parent
    > gets killed I want the child to continue running.
    >
    > Does anyone have an idea of how to do that?


    That's how processes work, with no special action required:

    $ cat parent_and_child.rb
    fork do
    5.times do
    puts "Child still running..."
    sleep 1
    end
    puts "Child exiting."
    end

    2.times do
    puts "Parent still running..."
    sleep 1
    end
    puts "Parent exiting."
    $ ruby parent_and_child.rb
    Parent still running...
    Child still running...
    Parent still running...
    Child still running...
    Parent exiting.
    Child still running...
    $ Child still running...
    Child still running...
    Child exiting.

    James Edward Gray II
    James Edward Gray II, Dec 18, 2010
    #2
    1. Advertising

  3. Tasos Laskos

    Tasos Laskos Guest

    Thanks for the response,

    And that's why I chose to work with Processes and not Threads.
    I recalled that that's their default behaviour but for some reason when
    the parent XMLRPC server dies his children die too.
    (The children are also XMLRPC servers serving a different purpose.)

    That's what's bugging me...

    *Lightbulb*
    The children are listening on stdin too so they probably catch the
    Ctrl+C interrupt and die as well.

    But I don't want that either...is there a way to separate them
    completely or will I have to settle?

    --
    Posted via http://www.ruby-forum.com/.
    Tasos Laskos, Dec 18, 2010
    #3
  4. On Dec 18, 2010, at 12:34 PM, Tasos Laskos wrote:

    > *Lightbulb*
    > The children are listening on stdin too so they probably catch the=20
    > Ctrl+C interrupt and die as well.


    That's correct. By default they share STDIN.

    > But I don't want that either...is there a way to separate them=20
    > completely or will I have to settle?


    Sure. You could redirect STDIN (plus STDOUT and STDERR, if desired) in =
    the children:

    $stdin.reopen("/dev/null", "r")

    or you could change the signal handling in the children:

    trap("INT", "IGNORE")

    I hope that helps.

    James Edward Gray II
    James Edward Gray II, Dec 18, 2010
    #4
  5. Tasos Laskos

    Tasos Laskos Guest

    Tasos Laskos, Dec 18, 2010
    #5
  6. On Sun, 19 Dec 2010, James Edward Gray II wrote:

    > On Dec 18, 2010, at 12:34 PM, Tasos Laskos wrote:
    >
    >> *Lightbulb*
    >> The children are listening on stdin too so they probably catch the
    >> Ctrl+C interrupt and die as well.

    >
    > That's correct. By default they share STDIN.
    >
    >> But I don't want that either...is there a way to separate them
    >> completely or will I have to settle?

    >
    > Sure. You could redirect STDIN (plus STDOUT and STDERR, if desired) in the children:
    >
    > $stdin.reopen("/dev/null", "r")
    >
    > or you could change the signal handling in the children:
    >
    > trap("INT", "IGNORE")
    >
    > I hope that helps.


    Or maybe this is what you want: http://www.ruby-forum.com/topic/185221

    -- Matt
    It's not what I know that counts.
    It's what I can remember in time to use.
    Matt Lawrence, Dec 18, 2010
    #6
  7. Tasos Laskos

    Tasos Laskos Guest

    No that wouldn't work for me...

    One last question, if I pass a singleton to each of the children do they
    get a copy or will it be shared somehow?

    During my tests there's no overwriting of any values but I'd prefer not
    to find out the hard way.

    Any ideas?

    Thanks for your help guys.

    --
    Posted via http://www.ruby-forum.com/.
    Tasos Laskos, Dec 18, 2010
    #7
  8. On Dec 18, 2010, at 12:54 PM, Tasos Laskos wrote:

    > One last question, if I pass a singleton to each of the children do they
    > get a copy or will it be shared somehow?


    Memory isn't shared between processes, unless you take steps to make it so.

    James Edward Gray II
    James Edward Gray II, Dec 18, 2010
    #8
  9. Tasos Laskos

    Tasos Laskos Guest

    Tasos Laskos, Dec 18, 2010
    #9
    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. john

    Independent processes

    john, Jan 9, 2006, in forum: VHDL
    Replies:
    0
    Views:
    468
  2. john

    Independent processes

    john, Jan 9, 2006, in forum: VHDL
    Replies:
    3
    Views:
    467
  3. Hagay Lupesko

    Running exe as an independent process

    Hagay Lupesko, Oct 26, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    410
    Joshua Flanagan
    Oct 26, 2003
  4. Jeff Rodriguez
    Replies:
    23
    Views:
    1,088
    David Schwartz
    Dec 9, 2003
  5. Marc Heiler
    Replies:
    1
    Views:
    163
    Robert Klemme
    May 24, 2009
Loading...

Share This Page