2014-11-18 3 views
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); 
       } 
     } 
} 

답변

0

걸려있는 하위 프로세스는 아마도 좀비 프로세스입니다. 당신은 피하는 문제에 대한 다음과 같은 예를 들어 할 수있는 :

  • 부모 프로세스에서 wait()의 사용 waitpid() 좀비 없애.
  • 'double fork' 트릭을 사용하면 하위 프로세스를 초기화 프로세스에 넣을 수 있습니다.
관련 문제