HP Rp7410 BSD Sockets Interface Programmer's Guide - Page 51
signalSIGCLD, SIG_IGN
![]() |
View all HP Rp7410 manuals
Add to My Manuals
Save this manual to your list of manuals |
Page 51 highlights
Using Internet Stream Sockets Example Using Internet Stream Sockets fprintf(stderr, "%s: unable to fork daemon\n", argv[0]); exit(1); case 0: /* The child process (daemon) comes here. */ /* Close stdin and stderr so that they will not * be kept open. Stdout is assumed to have been * redirected to some logging file, or /dev/null. * From now on, the daemon will not report any * error messages. This daemon will loop forever, * waiting for connections and forking a child * server to handle each one. */ fclose(stdin); fclose(stderr); /* Set SIGCLD to SIG_IGN, in order to prevent * the accumulation of zombies as each child * terminates. This means the daemon does not * have to make wait calls to clean them up. */ signal(SIGCLD, SIG_IGN); for(;;) { /* Note that addrlen is passed as a pointer * so that the accept call can return the * size of the returned address. */ addrlen = sizeof(struct sockaddr_in); /* This call will block until a new * connection arrives. Then, it will * return the address of the connecting * peer, and a new socket descriptor, * s, for that connection. */ s = accept(ls, &peeraddr_in, &addrlen); if ( s == -1) exit(1); switch (fork()) { case -1: /* Can't fork, just continue. */ exit(1); case 0: /* Child process comes here. */ server(); exit(0); default: /* Daemon process comes here. */ /* The daemon needs to close the * the new accept socket after * forking the child. This * prevents daemon from running * out of file descriptors. * It also means that when the * server closes the socket, * that it will allow socket * to be destroyed since it * will be the last close. */ close(s); } } default: exit(0); } /* Parent process comes here. */ Chapter 2 51
![](/manual_guide/products/hewlettpackard-rp5405-bsd-sockets-interface-programmers-guide-5276e7d/51.png)