Child processes live after parent process is killed

Discussion in 'Ruby' started by Uwe Kubosch, Feb 1, 2008.

  1. Uwe Kubosch

    Uwe Kubosch Guest

    Hi all!

    I have a Rails app that starts a child process with

    IO.popen 'script/backgroundrb' do |io|
    loop do
    output = io.gets
    break if output.nil?
    print output
    end
    end

    I would expect the child process to die when the Rails app exits or is
    killed. This however does not happen. I do this on Linux.

    Can anybody explain to me why child processes continue to live after
    their parent process has ended?

    I realize this is Linux specific, so feel free to redirect me to
    relevant documentation. Also if you know how this works on w$ndoze,
    please share.

    Any reply is much appreciated.


    Uwe Kubosch
    http://kubosch.no/
    Norway
    Uwe Kubosch, Feb 1, 2008
    #1
    1. Advertising

  2. Uwe Kubosch

    ara howard Guest

    On Feb 1, 2008, at 3:21 PM, Uwe Kubosch wrote:

    > Can anybody explain to me why child processes continue to live after
    > their parent process has ended?


    google 'zombie process'

    it's up to you to make sure that children do not outlive their
    parent. under many circumstances ruby will take of this before you
    but there are certain situations which no process cannot deal with...
    my slave lib addresses this in a full proof way, but a solid
    understanding of process management is required to use it.

    regards.

    a @ http://codeforpeople.com/
    --
    share your knowledge. it's a way to achieve immortality.
    h.h. the 14th dalai lama
    ara howard, Feb 1, 2008
    #2
    1. Advertising

  3. Uwe Kubosch

    Uwe Kubosch Guest

    On Sat, 2008-02-02 at 07:41 +0900, ara howard wrote:
    > On Feb 1, 2008, at 3:21 PM, Uwe Kubosch wrote:
    >
    > > Can anybody explain to me why child processes continue to live after
    > > their parent process has ended?

    >
    > it's up to you to make sure that children do not outlive their
    > parent. under many circumstances ruby will take of this before you
    > but there are certain situations which no process cannot deal with...
    > my slave lib addresses this in a full proof way, but a solid
    > understanding of process management is required to use it.


    Thank you for your reply.

    It seems slave-1.2.1 does what I want: Start a background worker
    process, leave it alone, and be sure it stops when the main process
    ends. I have inserted the following in my config/environment.rb:

    gem 'slave'
    require 'slave'
    require 'lib/workers/my_worker'
    my_worker_thread = Slave.object:)async=>true) {MyWorker.new.run}

    The method MyWorker#run never returns. Does this look OK? Is
    Slave::eek:bject designed for usage such as this?


    Uwe
    Uwe Kubosch, Feb 1, 2008
    #3
  4. Uwe Kubosch

    Gary Wright Guest

    On Feb 1, 2008, at 5:41 PM, ara howard wrote:
    > On Feb 1, 2008, at 3:21 PM, Uwe Kubosch wrote:
    >
    >> Can anybody explain to me why child processes continue to live after
    >> their parent process has ended?

    >
    > google 'zombie process'
    >


    The OP was not really describing zombies but instead orphaned processes.

    I have no doubt that Ara understands the differences but for other
    folks:

    A zombie process is a child process that has terminated but is being
    ignored by its parent. The process hangs around in the zombie state
    until the parent waits for it in order to 'reap' its termination status.

    A process that gets orphaned when its parent dies will get inherited
    by the init process, which will take care of reaping the termination
    status of an adopted processes when/if it terminates. An orphaned
    process is not notified that it has been orphaned but in theory it
    could see that its parent process id has changed (Process.ppid) or
    that its parent is init (process id 1).

    So while a parent process needs to pay attention to the status of its
    children, a child process doesn't really have to know anything about
    its parent.

    Ara Howard wrote:
    > it's up to you to make sure that children do not outlive their parent.


    Ara, are you talking more about making sure that you are reaping
    child processes? The idea that your child process will outlive you
    is quite common, for example when a daemon is started. It will often
    outlive whatever started it.


    The typical way to explicitly orphan a process (so that it gets
    adopted by init) is a double fork:

    fork { fork { do_work }; exit }

    The intermediary process forks a child process to do the work and
    then exits. The original process waits for the intermediary and the
    'grandchild' is orphaned and inherited by init. There are more
    considerations though to make a true 'daemon' process.

    A good source of information for this type of programming is Advanced
    Programming in the Unix Environment, by Rich Stevens and Stephen Rago
    (2nd Edition).


    Gary Wright
    Gary Wright, Feb 1, 2008
    #4
  5. Uwe Kubosch

    ara howard Guest

    On Feb 1, 2008, at 4:45 PM, Gary Wright wrote:

    snip helpful explaintion...

    > Ara, are you talking more about making sure that you are reaping
    > child processes? The idea that your child process will outlive you
    > is quite common, for example when a daemon is started. It will often
    > outlive whatever started it.


    well - i wasn't really talking about reaping, but killing ;-)

    the only way i've found to *really* prevent a child from outliving
    it's parent is what slave.rb does internally which connect a lifeline
    from child to parent - that lifeline is a pipe. the basic concept is
    (with syntax error i'm sure)

    r, w = IO.pipe

    fork do
    w.close

    Thread.new do
    begin
    r.read
    rescue Exception
    Kernel.exit
    end
    end

    child_stuff()

    exit
    end

    r.close


    which is to say open a pipe, spawn a child, ensure the child exist if
    the pipe ever goes stale. this makes a child die even if the parent
    gets 'kill -9'ed

    cheers.

    a @ http://codeforpeople.com/
    --
    we can deny everything, except that we have the possibility of being
    better. simply reflect on that.
    h.h. the 14th dalai lama
    ara howard, Feb 2, 2008
    #5
  6. Uwe Kubosch

    Gary Wright Guest

    On Feb 1, 2008, at 11:22 PM, ara howard wrote:
    > well - i wasn't really talking about reaping, but killing ;-)
    >
    > the only way i've found to *really* prevent a child from outliving
    > it's parent is what slave.rb does internally which connect a
    > lifeline from child to parent - that lifeline is a pipe. the basic
    > concept is (with syntax error i'm sure)


    I see. That makes sense and avoids any sort of ugly polling mechanism.
    Gary Wright, Feb 2, 2008
    #6
    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. Jeff Rodriguez
    Replies:
    23
    Views:
    1,111
    David Schwartz
    Dec 9, 2003
  2. Noel Dolan
    Replies:
    0
    Views:
    226
    Noel Dolan
    Jul 18, 2004
  3. earthwormgaz

    Perl killed after child spawn

    earthwormgaz, Mar 23, 2009, in forum: Perl Misc
    Replies:
    8
    Views:
    178
    Peter J. Holzer
    Mar 23, 2009
  4. Z W
    Replies:
    0
    Views:
    171
  5. Bitswapper
    Replies:
    5
    Views:
    130
    Prasad, Ramit
    Aug 27, 2013
Loading...

Share This Page