Socket Server с несколькими клиентами. Убийство дочерних процессов

0

Я писал сервер, который должен работать с несколькими клиентами, используя fork(). Я закрываю сокеты и выхожу из дочерних процессов, но после обработки всех клиентов я получаю массу дочерних процессов (проверили его с помощью ps -ef).

Я что-то упустил?

#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include "sbb_socket.h"
#include "bond_container.h"
#include <iostream>

#define SBB_ANY

void do_process(int sd_current);

int main(int argc, char* argv[])
{
        /*
         * get the number of clients from argument
         */
        long client_count = 1;
        if (argc > 1 && strtol(argv[1], NULL, 10) > 0) {
                client_count = strtol(argv[1], NULL, 10);
        }

        pid_t pid;
        int sd_current;

        /* 
         * get an internet domain socket 
         */
        int sd;
        if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
                perror("socket");
                exit(1);
        }
        /* 
         * set up the socket structure 
         */
        struct sockaddr_in sock_addr;

        memset(&sock_addr, 0, sizeof(sock_addr));
        sock_addr.sin_family = AF_INET;

#ifdef SBB_ANY
        /* set to INADDR_ANY if want server to be open to any client on any machine */
        sock_addr.sin_addr.s_addr = INADDR_ANY;
#else
        char hostname[128];
        /*
         *  we'll default to this host and call a section 3 func to get this host
         */
        if( gethostname(hostname,sizeof(hostname)) ){
                fprintf(stderr," SBB gethostname(...) failed errno: %d\n", errno);
                exit(1);
        }
        //printf("SBB gethostname() local hostname: \"%s\"\n", hostname);

        /*
         * set up socket structure for our host machine
         */
        struct hostent *hp;
        if ((hp = gethostbyname(hostname)) == 0) {
                fprintf(stderr,"SBB gethostbyname(...) failed errno: %d exiting...\n", errno);
                exit(1);
        }
        sock_addr.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
#endif
        sock_addr.sin_port = htons(PORT);

        /* 
         * bind the socket to the port number 
         */
        if (bind(sd, (struct sockaddr *) &sock_addr, sizeof(sock_addr)) == -1) {
                perror("bind");
                exit(1);
        }

        /* 
         * advertise we are available on this socket/port
         */
        if (listen(sd, 5) == -1) {
                perror("listen");
                exit(1);
        }

        while(1)
        {
                /* 
                 * wait for a client to connect 
                */

                struct sockaddr_in sock_addr_from_client;
                socklen_t addrlen = sizeof(sock_addr_from_client);
                if ((sd_current = accept(sd, (struct sockaddr *)  &sock_addr_from_client, &addrlen)) == -1) {
                        fprintf(stderr,"SBB accept(...) failed errno: %d  exiting...\n", errno);
                        exit(1);
                }

                /*
                 * block on socket waiting for client message
                 */

                if ((pid = fork()) < 0) {
                        printf("Error on fork");
                        exit(1);
                }

                if (pid == 0) {

                        close(sd);
                        do_process(sd_current);
                        exit(0);
                }
                else {
                        close(sd_current);
                }
        }
}
Теги:
sockets
server
fork

1 ответ

0

Висячие дочерние процессы, вероятно, являются зомбическими процессами. Например, вы можете сделать следующее, чтобы избежать проблемы:

  • Используйте waitpid() wait() в родительском процессе, чтобы избавиться от зомби.
  • Используйте трюк "двойной вилки", чтобы поместить дочерние процессы в процесс init.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню