How to recognize which child process has just ended?

Discussion in 'Ruby' started by Lukasz Muziol, Aug 7, 2006.

  1. Hi!

    The piece of software I'm working on now needs to fork child processes
    and execute different programs (which terminate themselves). Also, some
    child processes need to be killed/stopped/resumed and action needs to be
    taken when a child is terminated. When the parent receives & traps the
    SIGCLD signal, how does it know which child it is regarding and whether
    it has been ended, terminated, stopped or resumed?

    In brief, I need the information, which in C signal handling is
    accessible via the siginfo_t structure.

    Thanks.

    --
    Posted via http://www.ruby-forum.com/.
     
    Lukasz Muziol, Aug 7, 2006
    #1
    1. Advertising

  2. Lukasz Muziol wrote:
    > Hi!
    >
    > The piece of software I'm working on now needs to fork child processes
    > and execute different programs (which terminate themselves). Also, some
    > child processes need to be killed/stopped/resumed and action needs to be
    > taken when a child is terminated. When the parent receives & traps the
    > SIGCLD signal, how does it know which child it is regarding and whether
    > it has been ended, terminated, stopped or resumed?
    >
    > In brief, I need the information, which in C signal handling is
    > accessible via the siginfo_t structure.
    >
    > Thanks.


    Look at Process#wait2 and Process#waitpid2.

    --
    Posted via http://www.ruby-forum.com/.
     
    Francis Cianfrocca, Aug 7, 2006
    #2
    1. Advertising

  3. Lukasz Muziol

    Guest

    On Tue, 8 Aug 2006, Francis Cianfrocca wrote:

    > Lukasz Muziol wrote:
    >> Hi!
    >>
    >> The piece of software I'm working on now needs to fork child processes
    >> and execute different programs (which terminate themselves). Also, some
    >> child processes need to be killed/stopped/resumed and action needs to be
    >> taken when a child is terminated. When the parent receives & traps the
    >> SIGCLD signal, how does it know which child it is regarding and whether
    >> it has been ended, terminated, stopped or resumed?
    >>
    >> In brief, I need the information, which in C signal handling is
    >> accessible via the siginfo_t structure.
    >>
    >> Thanks.

    >
    > Look at Process#wait2 and Process#waitpid2.


    i don't think that's sufficient - one would still need a sig handler,
    something along the lines of:


    harp:~ > cat a.rb
    require 'thread'

    q = Queue.new

    trap('SIGCHLD'){ q.push :go }

    reaper = Thread.new do
    Thread.abort_on_exception = true

    loop do
    q.pop
    pid, status = Process.wait2
    p [pid, status]
    end
    end


    fork{ exit 42 }
    sleep 1

    fork{ exit 2 }

    puts '...'
    sleep


    harp:~ > ruby a.rb
    [26939, #<Process::Status: pid=26939,exited(42)>]
    [26940, #<Process::Status: pid=26940,exited(2)>]
    ...


    regards.

    -a
    --
    to foster inner awareness, introspection, and reasoning is more efficient than
    meditation and prayer.
    - h.h. the 14th dali lama
     
    , Aug 7, 2006
    #3
  4. unknown wrote:

    > i don't think that's sufficient - one would still need a sig handler,
    > something along the lines of:
    >


    That's a surprise, what made you think so? Is that true on Windows,
    perhaps? On Unix no sig handler is needed. SIGCHLD is ignored by
    default. The following works:

    #----------------------

    fork {exit 42}
    fork {exit 2}

    Process.wait2
    Process.wait2

    #----------------------

    You can also call #wait and its variants with the flag Process::WNOHANG
    if you want to poll. Use a SIGCHLD handler if you need to know instantly
    when a child process finishes, but as always observe the standard
    guidelines for coding signal handlers.

    --
    Posted via http://www.ruby-forum.com/.
     
    Francis Cianfrocca, Aug 7, 2006
    #4
  5. Lukasz Muziol

    Guest

    On Tue, 8 Aug 2006, Francis Cianfrocca wrote:

    > Use a SIGCHLD handler if you need to know instantly
    > when a child process finishes, but as always observe the standard
    > guidelines for coding signal handlers.


    sorry i wasn't more explicit - it's this that i interpreted the OP had wanted
    since he asked to know __when__ the child exited not to do something whenever
    it happened to. i may have been mistaken in my interpretation though...

    in any case we are in total agreement.

    -a
    --
    to foster inner awareness, introspection, and reasoning is more efficient than
    meditation and prayer.
    - h.h. the 14th dali lama
     
    , Aug 7, 2006
    #5
  6. Thank you both.

    As a matter of fact I do need to use a signal handler (the application
    has an ncurses interface, so most of the time it's locked on getch calls
    and action needs to be taken on child death), but the answer to my
    problem is indeed the Process#wait2 method, which I somehow managed to
    overlook.

    --
    Posted via http://www.ruby-forum.com/.
     
    Lukasz Muziol, Aug 10, 2006
    #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. tshad

    Session has ended message

    tshad, Mar 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    929
    Jc Morin
    Mar 5, 2005
  2. news.amnet.net.au

    Determine when a process has ended?

    news.amnet.net.au, Feb 20, 2004, in forum: Java
    Replies:
    2
    Views:
    664
    Chris Smith
    Feb 20, 2004
  3. Jeff Rodriguez
    Replies:
    23
    Views:
    1,132
    David Schwartz
    Dec 9, 2003
  4. Jan C. Faerber
    Replies:
    0
    Views:
    519
    Jan C. Faerber
    Nov 14, 2009
  5. Z W
    Replies:
    0
    Views:
    177
Loading...

Share This Page