select()块而不是返回超时

时间:2012-03-30 15:44:26

标签: c++ linux select

作为背景,我正在编写一个多线程的Linux服务器应用程序。每个子进程都有一个与之关联的连接,并使用select()查看是否有数据等待在套接字上读取。

我做了一些搜索,一次我找不到任何帮助来解决这个问题。

第一次实际发布到Stack Overflow,所以我很抱歉,如果我的格式是垃圾。

//this first line switches my connection to non-blocking.
//select() still fails whether or not this line is in.
fcntl(ChildConnection -> newsockfd, F_SETFL, 0);    

struct timeval tv;
fd_set readfds; 

FD_ZERO(&readfds);
FD_SET(ChildConnection -> newsockfd, &readfds);

tv.tv_sec = 3; //3 seconds of waiting maximum. Changing this does nothing.
tv.tv_usec = 0;

printf("-DEBUG: Child, About to select() the newsockfd, which is %i. readfds is %i.\n", ChildConnection -> newsockfd, readfds);

//if I feed this a bad descriptor (-1 or something) on purpose, it DOES return -1 though.
int result = select(ChildConnection -> newsockfd + 1, &readfds, NULL, NULL, &tv);

//this commented out line below doesn't even time out.
//int result = select(0, NULL, NULL, NULL, &tv);

printf("-DEBUG: Child, Just  select()ed. result is %i. Hopefully that was >= 0.", result);

if (result < 0)
{
 DisplayError("ERROR using select() on read connection in MotherShip::HandleMessagesChild: ");
}
else if (result > 0) // > 0 means there is data waiting to be read
{
/* <--- Snipped Reading Stuff here ---> */      
}

//so if the code gets here without a result that means it timed out.

不幸的是,第二条打印线(说它已经选中)从未打印过。有谁知道发生了什么事,或者有建议我尝试调试这个?

1 个答案:

答案 0 :(得分:2)

您在其他地方遇到阻止情况。首先让您的select()代码在一个小型测试台中工作,然后移植它。您在代码中的注释“下面这条注释掉的行甚至没有超时”是可以证实的错误:

$ cat test.c
#include <stdio.h>
#include <sys/select.h>
int main()
{
    struct timeval tv;
    tv.tv_sec = 3;
    tv.tv_usec = 0;
    select(0, NULL, NULL, NULL, &tv);
    return 0;
}
$ gcc -o test test.c
$ time ./test

real    0m3.004s
user    0m0.000s
sys 0m0.000s

或者,尝试将调试程序附加到挂起进程并查看其被阻止的位置。或者在strace()等观看它......