具有多个客户端的套接字服务器杀死子进程

时间:2014-11-18 06:38:18

标签: c++ sockets fork server

我一直在编写一个服务器,它应该使用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);
                }
        }
}

1 个答案:

答案 0 :(得分:0)

挂起的子进程可能是僵尸进程。 例如,你可以做以避免问题:

  • 在父进程中使用waitpid() wait()来摆脱僵尸。
  • 使用'double fork'技巧将子进程置于init进程下。