Unis signal handling in parent child scenario.

Discussion in 'C Programming' started by gnutuxy@yahoo.co.in, Sep 30, 2004.

  1. Guest

    Hi,

    I am newbie in the Unix system programming and in learning phase.
    I usually read the libc manual and then try to implement small
    programs to test/check the learnt thing.

    I read the libc manual for signals and tried the program to check
    whether child
    inherits the signal handler intalled by parent befor fork.

    I am totally confused about it because the code won't work as per the
    the manual. Manual says child inherits signal action and signal mask.

    My situation is as follows:
    Question : Why I won't get message "Received the signal: #" on the
    terminal, when I signaled child process with SIGINT?

    ==================glibc manual extract==============
    * The set of pending signals (*note Delivery of Signal::) for the
    child process is cleared. (The child process inherits its mask of
    blocked signals and signal actions from the parent process.)
    ====================================================

    My try : I just want to check that the signal handler installed in
    Parent is
    inherited by child ( this is what I interpreted from the
    glibc manual,
    the extract of the manual is highlighted above. )

    Here is my code.
    =================my code============================
    #include <stdio.h>
    #include <signal.h>
    #include <unistd.h>
    #include <sys/types.h>

    void mysig_handler( int signum )
    {
    printf("Received the signal: %d", signum );
    }

    int main( int argc, char *argv[] )
    {

    pid_t chld;

    signal( SIGINT, mysig_handler );

    if( ( chld = fork() ) < 0 )
    {
    //error
    printf("Fork Error!\n");
    }
    else if( chld == 0 )
    {
    //child
    while(1)
    {
    }
    }
    else
    {
    //parent
    while(1)
    {
    }
    }
    return(0);
    }
    ====================================================
    my softwares are
    gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
    Kernel 2.2.14-12smp

    Regards,
    Vinay G.
     
    , Sep 30, 2004
    #1
    1. Advertising

  2. In article <>,
    wrote:

    > Hi,
    >
    > I am newbie in the Unix system programming and in learning phase.
    > I usually read the libc manual and then try to implement small
    > programs to test/check the learnt thing.
    >
    > I read the libc manual for signals and tried the program to check
    > whether child
    > inherits the signal handler intalled by parent befor fork.
    >
    > I am totally confused about it because the code won't work as per the
    > the manual. Manual says child inherits signal action and signal mask.
    >
    > My situation is as follows:
    > Question : Why I won't get message "Received the signal: #" on the
    > terminal, when I signaled child process with SIGINT?


    Well, comp.unix.programmer (cross-posted, followup-to: set) would
    probably be a better newsgroup, but your error is simple, and somewhat
    on-topic:

    > void mysig_handler( int signum )
    > {
    > printf("Received the signal: %d", signum );
    > }


    stdout is by default line-buffered. You either need to end your printf
    format with '\n':

    printf("Received the signal: %d\n", signum );

    Or add an fflush(stdout); after the printf:

    printf("Received the signal: %d", signum );
    fflush(stdout);

    if you want to get any output. You probably want the former. Back to
    c.l.c off-topic-ness:

    signal(3C) is a very old, crufty interface. You should look at the
    manpage for sigaction(3C), the preferred interface. You might consider
    picking up one of:
    _Advanced Programming in the Unix Environment_
    (Stevens, ISBN 0201563177)
    _Solaris Systems Programming_
    (Rich Teer, ISBN 0201750392)

    The latter, despite the title, contains general Unix programming tips,
    and is kind of an up-to-date APUE in parts, from what I hear.

    Cheers,
    - jonathan
     
    Jonathan Adams, Sep 30, 2004
    #2
    1. Advertising

  3. Guest

    Thanks Jonathan,

    It works!
    And thanks for suggesting the com.unix.programming.

    Regards,
    Vinay Gadekar.
     
    , Oct 4, 2004
    #3
    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. MattB
    Replies:
    0
    Views:
    496
    MattB
    Apr 10, 2006
  2. davout
    Replies:
    0
    Views:
    562
    davout
    Apr 18, 2004
  3. Jeff Rodriguez
    Replies:
    23
    Views:
    1,157
    David Schwartz
    Dec 9, 2003
  4. Noel Dolan
    Replies:
    0
    Views:
    249
    Noel Dolan
    Jul 18, 2004
  5. Bitswapper
    Replies:
    5
    Views:
    162
    Prasad, Ramit
    Aug 27, 2013
Loading...

Share This Page