我在处理僵尸进程时遇到了一些麻烦。我写了一个简单的服务器,它可以在玩家之间创建tic tac toe匹配。我使用select()在多个连接的客户端之间进行多路复用。每当有两个客户端时,服务器将分叉另一个执行匹配仲裁程序的进程。
问题是select()阻塞。因此,如果有一个匹配仲裁程序作为子进程运行并且它退出,如果没有传入连接,父进程将永远不会等待子进程因为select()阻塞。
我的代码在这里,道歉,因为它非常混乱。
while(1) {
if (terminate)
terminate_program();
FD_ZERO(&rset);
FD_SET(tcp_listenfd, &rset);
FD_SET(udpfd, &rset);
maxfd = max(tcp_listenfd, udpfd);
/* add child connections to set */
for (i = 0; i < MAXCLIENTS; i++) {
sd = tcp_confd_lst[i];
if (sd > 0)
FD_SET(sd, &rset);
if (sd > maxfd)
maxfd = sd;
}
/* Here select blocks */
if ((nready = select(maxfd + 1, &rset, NULL, NULL, NULL)) < 0) {
if (errno == EINTR)
continue;
else
perror("select error");
}
/* Handles incoming TCP connections */
if (FD_ISSET(tcp_listenfd, &rset)) {
len = sizeof(cliaddr);
if ((new_confd = accept(tcp_listenfd, (struct sockaddr *) &cliaddr, &len)) < 0) {
perror("accept");
exit(1);
}
/* Send connection message asking for handle */
writen(new_confd, handle_msg, strlen(handle_msg));
/* adds new_confd to array of connected fd's */
for (i = 0; i < MAXCLIENTS; i++) {
if (tcp_confd_lst[i] == 0) {
tcp_confd_lst[i] = new_confd;
break;
}
}
}
/* Handles incoming UDP connections */
if (FD_ISSET(udpfd, &rset)) {
}
/* Handles receiving client handles */
/* If client disconnects without entering their handle, their values in the arrays will be set to 0 and can be reused. */
for (i = 0; i < MAXCLIENTS; i++) {
sd = tcp_confd_lst[i];
if (FD_ISSET(sd, &rset)) {
if ((valread = read(sd, confd_handle, MAXHANDLESZ)) == 0) {
printf("Someone disconnected: %s\n", usr_handles[i]);
close(sd);
tcp_confd_lst[i] = 0;
usr_in_game[i] = 0;
} else {
confd_handle[valread] = '\0';
printf("%s\n", confd_handle); /* For testing */
fflush(stdout);
strncpy(usr_handles[i], confd_handle, sizeof(usr_handles[i]));
for (j = i - 1; j >= 0; j--) {
if (tcp_confd_lst[j] != 0 && usr_in_game[j] == 0) {
usr_in_game[i] = 1; usr_in_game[j] = 1;
if ((child_pid = fork()) == 0) {
close(tcp_listenfd);
snprintf(fd_args[0], sizeof(fd_args[0]), "%d", tcp_confd_lst[i]);
snprintf(fd_args[1], sizeof(fd_args[1]), "%d", tcp_confd_lst[j]);
execl("nim_match_server", "nim_match_server", usr_handles[i], fd_args[0], usr_handles[j], fd_args[1], (char *) 0);
}
close(tcp_confd_lst[i]); close(tcp_confd_lst[j]);
tcp_confd_lst[i] = 0; tcp_confd_lst[j] = 0;
usr_in_game[i] = 0; usr_in_game[j] = 0;
}
}
}
}
}
}
是否有一种方法允许等待即使在select()阻塞时运行?最好没有信号处理,因为它们是异步的。
编辑:实际上,我发现select有一个timeval数据结构,我们可以指定超时。使用这个是个好主意吗?
答案 0 :(得分:3)
我认为你的选择是:
将所有子描述符保存在全局数组中,并从信号处理程序中调用wait()。如果您不需要主循环中孩子的退出状态,我认为这是最简单的。
使用pselect代替select,它将在收到指定的(一组)信号时返回,在您的情况下,SIGCHLD。然后在所有子PID上调用wait / WNOHANG。您需要在pselect()之前/之后的适当时刻阻止/取消阻止SIGCHLD,请参阅此处:http://pubs.opengroup.org/onlinepubs/9699919799/functions/pselect.html
等待/清除辅助线程中的子PID。我认为这是最复杂的解决方案(重新确定线程之间的同步),但是既然你问过,它在技术上是可行的。
答案 1 :(得分:2)
如果您只想阻止僵尸进程,可以设置SIGCHLD
信号处理程序。如果您想要实际等待返回状态,可以从信号处理程序将字节写入管道(非阻塞,以防万一),然后在select
循环中读取这些字节。
有关如何处理SIGCHLD
,请参阅http://www.microhowto.info/howto/reap_zombie_processes_using_a_sigchld_handler.html - 您希望执行while (waitpid((pid_t)(-1), 0, WNOHANG) > 0) {}
也许最好的方法是从SIGCHLD
信号处理程序向主select
循环发送单个字节(非阻塞,以防万一)并在waitpid
循环中执行当可以从管道读取字节时,select
循环。
您也可以使用signalfd
文件描述符来读取SIGCHLD
信号,尽管这仅适用于Linux。