From: grocery_stocker on
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.

From: Barry Margolin on
In article
<9957d594-029d-44ba-9178-100306f4ea5d(a)v1g2000pra.googlegroups.com>,
grocery_stocker <cdalten(a)gmail.com> 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(a)alum.mit.edu
Arlington, MA
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: K-mart Cashier on
On Jun 27, 7:28 pm, Barry Margolin <bar...(a)alum.mit.edu> wrote:
> In article
> <9957d594-029d-44ba-9178-100306f4e...(a)v1g2000pra.googlegroups.com>,
>
>  grocery_stocker <cdal...(a)gmail.com> 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

From: K-mart Cashier on
On Jun 27, 7:40 pm, K-mart Cashier <cdal...(a)gmail.com> wrote:
> On Jun 27, 7:28 pm, Barry Margolin <bar...(a)alum.mit.edu> wrote:
>
> > In article
> > <9957d594-029d-44ba-9178-100306f4e...(a)v1g2000pra.googlegroups.com>,
>
> >  grocery_stocker <cdal...(a)gmail.com> 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?
From: Barry Margolin on
In article
<e07df15e-9099-42e6-a756-8d10068fbb73(a)t12g2000prg.googlegroups.com>,
K-mart Cashier <cdalten(a)gmail.com> wrote:

> On Jun 27, 7:40�pm, K-mart Cashier <cdal...(a)gmail.com> wrote:
> > On Jun 27, 7:28�pm, Barry Margolin <bar...(a)alum.mit.edu> wrote:
> >
> > > In article
> > > <9957d594-029d-44ba-9178-100306f4e...(a)v1g2000pra.googlegroups.com>,
> >
> > > �grocery_stocker <cdal...(a)gmail.com> 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(a)alum.mit.edu
Arlington, MA
*** PLEASE don't copy me on replies, I'll read them in the group ***