Placement of signal handler question - TCP-IP

This is a discussion on Placement of signal handler question - TCP-IP ; In figure 5.12 on pages 138 - 139 in the book "Unix Network Programming: The Socket Networking API" by Stevens, Fenner, and Rudoff, they have the following 1 #include "unp.h" 2 int 3 main(int argc, char **argv) 4 { 5 ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: Placement of signal handler question

  1. Placement of signal handler question

    In figure 5.12 on pages 138 - 139 in the book "Unix Network
    Programming: The Socket Networking API" by Stevens, Fenner, and
    Rudoff, they have the following

    1 #include "unp.h"
    2 int
    3 main(int argc, char **argv)
    4 {
    5 int listenfd, connfd;
    6 pid_t childpid;
    7 socklen_t clilen;
    8 struct sockaddr_in cliaddr, servaddr;
    9 void sig_chld(int);

    10 listenfd = Socket (AF_INET, SOCK_STREAM, 0);

    11 bzero (&servaddr, sizeof(servaddr));
    12 servaddr.sin_family = AF_INET;
    13 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    14 servaddr.sin_port = htons(SERV_PORT);

    15 Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));

    16 Listen(listenfd, LISTENQ);

    17 Signal (SIGCHLD, sig_chld); /* must call waitpid() */

    18 for ( ; ; ) {
    19 clilen = sizeof(cliaddr);
    20 if ( (connfd = accept (listenfd, (SA *) &cliaddr, &clilen))
    < 0) {
    21 if (errno == EINTR)
    22 continue; /* back to for() */
    23 else
    24 err_sys("accept error");
    25 }

    26 if ( (childpid = Fork()) == 0) { /* child process */
    27 Close(listenfd); /* close listening socket */
    28 str_echo(connfd); /* process the request */
    29 exit(0);
    30 }
    31 Close (connfd); /* parent closes connected socket
    */
    32 }
    33 }


    On ine 17, why do they place Singal before fork()? Ie, what would
    happen if they stuck Singal near the bottom, after the fork() has
    completed and the child has terminated. I have a few other questions.
    However, I'm going to try to think them through before I ask.


  2. Re: Placement of signal handler question

    In article
    <9957d594-029d-44ba-9178-100306f4ea5d@v1g2000pra.googlegroups.com>,
    grocery_stocker wrote:

    > On ine 17, why do they place Singal before fork()? Ie, what would
    > happen if they stuck Singal near the bottom, after the fork() has
    > completed and the child has terminated. I have a few other questions.
    > However, I'm going to try to think them through before I ask.


    If you do it after fork(), and the child runs very quickly, the child
    could exit before you establish the handler, and your handler won't be
    run.

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE don't copy me on replies, I'll read them in the group ***

  3. Re: Placement of signal handler question

    On Jun 27, 7:28*pm, Barry Margolin wrote:
    > In article
    > <9957d594-029d-44ba-9178-100306f4e...@v1g2000pra.googlegroups.com>,
    >
    > *grocery_stocker wrote:
    > > On ine 17, why do they place Singal before fork()? Ie, what would
    > > happen if they stuck Singal near the bottom, after the fork() has
    > > completed and the child has terminated. I have a few other questions.
    > > However, I'm going to try to think them through before I ask.

    >
    > If you do it after fork(), and the child runs very quickly, the child
    > could exit before you establish the handler, and your handler won't be
    > run.
    >


    I see. Thanks for the clarification.

    Chad


  4. Re: Placement of signal handler question

    On Jun 27, 7:40*pm, K-mart Cashier wrote:
    > On Jun 27, 7:28*pm, Barry Margolin wrote:
    >
    > > In article
    > > <9957d594-029d-44ba-9178-100306f4e...@v1g2000pra.googlegroups.com>,

    >
    > > *grocery_stocker wrote:
    > > > On ine 17, why do they place Singal before fork()? Ie, what would
    > > > happen if they stuck Singal near the bottom, after the fork() has
    > > > completed and the child has terminated. I have a few other questions.
    > > > However, I'm going to try to think them through before I ask.

    >
    > > If you do it after fork(), and the child runs very quickly, the child
    > > could exit before you establish the handler, and your handler won't be
    > > run.

    >
    > I see. Thanks for the clarification.
    >
    > Chad


    Wait. I take that back. How does placing the signal handler before the
    fork() ensure that the handler will be established before the child
    exits?

  5. Re: Placement of signal handler question

    In article
    ,
    K-mart Cashier wrote:

    > On Jun 27, 7:40*pm, K-mart Cashier wrote:
    > > On Jun 27, 7:28*pm, Barry Margolin wrote:
    > >
    > > > In article
    > > > <9957d594-029d-44ba-9178-100306f4e...@v1g2000pra.googlegroups.com>,

    > >
    > > > *grocery_stocker wrote:
    > > > > On ine 17, why do they place Singal before fork()? Ie, what would
    > > > > happen if they stuck Singal near the bottom, after the fork() has
    > > > > completed and the child has terminated. I have a few other questions.
    > > > > However, I'm going to try to think them through before I ask.

    > >
    > > > If you do it after fork(), and the child runs very quickly, the child
    > > > could exit before you establish the handler, and your handler won't be
    > > > run.

    > >
    > > I see. Thanks for the clarification.
    > >
    > > Chad

    >
    > Wait. I take that back. How does placing the signal handler before the
    > fork() ensure that the handler will be established before the child
    > exits?


    How can the child exit before it's started?

    I have a feeling I'm missing some subtle misunderstanding you're having.

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE don't copy me on replies, I'll read them in the group ***

+ Reply to Thread