Reap Zombie Processes (OOPS)

This commit is contained in:
Andrew Pamment 2016-03-27 21:53:46 +10:00
parent ade66d92f1
commit 5fafb6fee4

25
main.c
View File

@ -3,21 +3,44 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <errno.h>
#include "bbs.h" #include "bbs.h"
void sigchld_handler(int s)
{
// waitpid() might overwrite errno, so we save and restore it:
int saved_errno = errno;
while(waitpid(-1, NULL, WNOHANG) > 0);
errno = saved_errno;
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
int socket_desc, client_sock, c, *new_sock; int socket_desc, client_sock, c, *new_sock;
int pid; int pid;
struct sockaddr_in server, client; struct sockaddr_in server, client;
int port; int port;
struct sigaction sa;
if (argc < 3) { if (argc < 3) {
printf("Usage ./magicka config/bbs.ini port\n"); printf("Usage ./magicka config/bbs.ini port\n");
exit(1); exit(1);
} }
sa.sa_handler = sigchld_handler; // reap all dead processes
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}
socket_desc = socket(AF_INET, SOCK_STREAM, 0); socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1) { if (socket_desc == -1) {
printf("Couldn't create socket..\n"); printf("Couldn't create socket..\n");