signal HANDLER in perl

Discussion in 'Perl Misc' started by gaurava@lycos.com, Apr 25, 2007.

  1. Guest

    Hi,
    I am trying to write a program where the main process registers a
    SIGNAL handler for SIGCHLD, forks a bunch of processes and then reaps
    the dead children. I have some control over when to register the
    signal handler (before or after forking the child processes but not a
    whole lot). I am seeing some weird behaviour. Basically I see a valid
    child exit, handle it, and then start seeing a slew of SIGCHLD signals
    from a process with PID 0. I was wondering if someone could tell me
    the right way to handle signals, or has seen this problem before and
    knows how to fix it. I am copying pseudo code for my script below.

    Thanks,
    Gaurav


    sub handler {
    while(($kid = waitpid(-1, WNOHANG)) != -1) {
    if ($kid == $imp_pid) {
    exit();
    }
    }
    }

    $imp_pid = fork_important_child();
    $SIG{CHLD} = 'handler;

    $other_pid = fork_normal_process();
    $yet_another_pid = fork_normal_process();
    :
    :
    snip
     
    , Apr 25, 2007
    #1
    1. Advertising

  2. Guest

    wrote:
    > Hi,
    > I am trying to write a program where the main process registers a
    > SIGNAL handler for SIGCHLD, forks a bunch of processes and then reaps
    > the dead children. I have some control over when to register the
    > signal handler (before or after forking the child processes but not a
    > whole lot). I am seeing some weird behaviour. Basically I see a valid
    > child exit, handle it, and then start seeing a slew of SIGCHLD signals
    > from a process with PID 0.


    Perhaps you see something you interpret as that, but you are probably
    wrong. Since you didn't show us what you actually do see, it is hard to
    point out how you are interpreting it wrongly. We can make a guess, maybe
    it will be right, maybe not.


    > I was wondering if someone could tell me
    > the right way to handle signals, or has seen this problem before and
    > knows how to fix it. I am copying pseudo code for my script below.


    Please post real code. Otherwise, it is hard to give you real answers.

    >
    > Thanks,
    > Gaurav
    >
    > sub handler {
    > while(($kid = waitpid(-1, WNOHANG)) != -1) {



    from perldoc -f waitpid:

    Waits for a particular child process to terminate
    and returns the pid of the deceased process, or
    "-1" if there is no such child process. On some
    systems, a value of 0 indicates that there are
    processes still running.

    So if waitpid is returning 0, then you are probably on a system in which
    waitpid returns 0 when there are still processes running.

    -1 means "There are no processes for me to wait for", while 0 means
    "There are processes to wait for, but you said WNOHANG so I am not going
    to actually wait for them." So maybe you should test for $kid being
    >0 rather than !=-1.



    > if ($kid == $imp_pid) {
    > exit();
    > }


    If you are only interested in the case in which $kid==$imp_pid, you should
    probably specify $imp_pid rather than -1 as the first argument to waitpid.


    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Apr 25, 2007
    #2
    1. Advertising

  3. Guest

    wrote:
    >
    > > if ($kid == $imp_pid) {
    > > exit();
    > > }

    >
    > If you are only interested in the case in which $kid==$imp_pid, you
    > should probably specify $imp_pid rather than -1 as the first argument to
    > waitpid.


    Scratch that. Perhaps you should use $imp_pid as the first argument to
    waitpid, but perhaps not, depending on how long-running your job is and
    thus the risk of accumulating zombies.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Apr 25, 2007
    #3
  4. Guest

    thanks that helps. I will try it out.

    On Apr 25, 12:22 pm, wrote:
    > wrote:
    >
    > > > if ($kid == $imp_pid) {
    > > > exit();
    > > > }

    >
    > > If you are only interested in the case in which $kid==$imp_pid, you
    > > should probably specify $imp_pid rather than -1 as the first argument to
    > > waitpid.

    >
    > Scratch that. Perhaps you should use $imp_pid as the first argument to
    > waitpid, but perhaps not, depending on how long-running your job is and
    > thus the risk of accumulating zombies.
    >
    > Xho
    >
    > --
    > --------------------http://NewsReader.Com/--------------------
    > Usenet Newsgroup Service $9.95/Month 30GB
     
    , Apr 27, 2007
    #4
    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. Michael Pronath
    Replies:
    1
    Views:
    1,237
    Diez B. Roggisch
    Jan 3, 2005
  2. Jack Orenstein

    threading.Thread vs. signal.signal

    Jack Orenstein, Sep 18, 2005, in forum: Python
    Replies:
    0
    Views:
    499
    Jack Orenstein
    Sep 18, 2005
  3. Weng Tianxiang
    Replies:
    2
    Views:
    689
    Jonathan Bromley
    Jan 30, 2007
  4. Nicolas Moreau
    Replies:
    9
    Views:
    3,358
  5. dibacco73
    Replies:
    1
    Views:
    697
    joris
    Feb 12, 2009
Loading...

Share This Page