最近在发现select(fd + 1, &readfd, &writefd, &expfd, NULL); 此时select已经进入等待状态, 然而 fd 被其他线程关闭时, select并不能及时发现,而依旧是处于等待中。
FD_ZERO(&readfds);
FD_ZERO(&errfds);
FD_SET(*sock_fd, &readfds);
FD_SET(*sock_fd, &errfds);
printf ("go to select/n");
int ret = select(*sock_fd + 1, NULL, NULL, &errfds, NULL);
printf ("select end ret=%d/n", ret);
printf ("thread end/n");
}
int main(void)
{
int sock_fd, new_fd; // listen on sock_fd, new connection on new_fd
struct sockaddr_in server_addr; // server address information
struct sockaddr_in client_addr; // connector's address information
socklen_t sin_size;
int yes = 1;
char buf[BUF_SIZE];
int ret;
int i;
if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
pthread_t tid;
pthread_create(&tid, NULL, _ThreadEntry, &sock_fd);
sleep(1);
printf("close sock/n");
close(sock_fd);
getchar();
getchar();
exit(0);
}
运行 结果 :
[root@localhost hellolinux]# ./hello
go to select
close sock