FtpServer处理SIG_CHLD

利用WNOHANG的waitpid来处理同时多个child终止,但unix只投递一个SIG_CHLD的情况。

当accept慢速系统调用被EINTR中断后应重启。


void sig_chld(int signo){
   pid_t pid;
   int stat;
   while((pid = waitpid(-1,&stat,WNOHANG)) >0 ){
      printf("child %d process terminated.\n",pid);
   }
   return;
}

int main(void){
   ...
   signal(SIGCHLD,sig_chld);
   while(1)
  {
     if((connfd = accept(listenfd,(struct sockaddr*)&cliaddr,&clilen)) < 0)
     {
         if(errno == EINTR)
             continue;
         else
             printf("accept error! %s",strerror(errno));
             return errno;
      }
      printf("get a connection\n");
      if((handlerpid = fork()) == 0)
      {
          handler(connfd);
          exit(0);
      }
      close(connfd);
  }
  ...
}
转载请注明来源:Leoncom-《FtpServer处理SIG_CHLD》
Trackback

only 1 comment untill now

  1. That’s quite a conclusion to make based on the omission of one song, but hey, opinions! Thanks for reading it!

Add your comment now