select返回0的原因大致有哪些

nalisaki 2012-04-02 08:47:55
对文件(非socket)操作时,select返回0,即超时,原因都有哪些呢,多谢!
...全文
882 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq120848369 2012-04-03
  • 打赏
  • 举报
回复
On Linux, the function select modifies timeout to reflect the amount of time not slept; most other implementations do not do this. This causes problems both
when Linux code which reads timeout is ported to other operating systems, and when code is ported to Linux that reuses a struct timeval for multiple selects
in a loop without reinitializing it. Consider timeout to be undefined after select returns.

这个代码的意思是如果是超时返回,那么说明没数据,就再睡2秒的意思,可惜太依赖于实现了,没人保证超时返回timeout还是原值,所以这个代码准确的说是有BUG的,隐患很大。
mymtom 2012-04-03
  • 打赏
  • 举报
回复
int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *errorfds, struct timeval *timeout);

需要特别注意的是最后一个参数(超时)不是const的。3楼的写法有问题,虽然有的系统不会修改timeout,但是标准里特别提到:

On successful completion, the object pointed to by the timeout argument may be modified.

所以在每次select之前,timeout 需要重新赋值。
楼主遇到的问题可能就在这里。
mymtom 2012-04-03
  • 打赏
  • 举报
回复
计算机不会出现“大致”的问题。
select返回0的唯一确切原因:超时

RETURN VALUES
The select() system call returns the number of ready descriptors that are
contained in the descriptor sets, or -1 if an error occurred. If the
time limit expires, select() returns 0. If select() returns with an
error, including one due to an interrupted system call, the descriptor
sets will be unmodified.
nalisaki 2012-04-03
  • 打赏
  • 举报
回复
具体程序就是如下了,如果大家对linux下网络摄像头的数据采集感兴趣,可以google capture.c
while(fdManager[ind].running)
{
if(fdManager[ind].exitFlag)
{
writeLog(fd,"the main loop break now",__FILE__,__LINE__);
break;
}
ind = getFdPos(fd);
FD_ZERO (&fds);
FD_SET (fd, &fds);

/* Timeout. */
tv.tv_sec = 2;
tv.tv_usec = 0;
r = select(fd + 1, &fds, NULL, NULL, &tv);
if (-1 == r)
{
if (EINTR == errno)
writeLog(fd,"select error: -1",__FILE__,__LINE__);
}
else if (0 == r)
{
writeLog(fd,"select error: 0",__FILE__,__LINE__);
sleep(tv.tv_sec);
}
else
{
readFrame(fd,&fmt1,&bmpInfo1,p);
}
}
justkk 2012-04-03
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

就是只对一个摄像头进行读取.select(fd+1,readfds,NULL,NULL,timeval),readfds只有一个,参数只有这些。
[/Quote]
timeval 的值是多少?
root_jli 2012-04-02
  • 打赏
  • 举报
回复
如果该套接口正处于监听listen()状态,则若有连接请求到达,该套接口便被标识为可读。
如果虚电路被“优雅地”中止,则recv()不读取数据立即返回;
如果虚电路被强制复位,则recv()将以WSAECONNRESET错误立即返回。
nalisaki 2012-04-02
  • 打赏
  • 举报
回复
就是只对一个摄像头进行读取.select(fd+1,readfds,NULL,NULL,timeval),readfds只有一个,参数只有这些。
qq120848369 2012-04-02
  • 打赏
  • 举报
回复
超时返回0,没有为什么.
root_jli 2012-04-02
  • 打赏
  • 举报
回复
int select(
int nfds,
fd_set* readfds,
fd_set* writefds,
fd_set* exceptfds,
const struct timeval* timeout
);
本函数用于确定一个或多个套接口的状态,非socket不知怎么用?
select(sock+1,&readfds,0,0,&timeout) 返回0 套接口可读性超时
select(sock+1,0,&writefds,0,&timeout) 返回0 套接口可写性超时
select(sock+1,0,0,&exceptfds,&timeout) 等待带外数据存在性检查超时
select(sock+1,&readfds,&writefds,&exceptfds,&timeout) 返回0 套接口不可用
昵称很不好取 2012-04-02
  • 打赏
  • 举报
回复
到了文件尾、输入输出有问题之类都会让select返回0,看看程序吧
justkk 2012-04-02
  • 打赏
  • 举报
回复
你的select是如何调用的,相关的参数都是什么?

23,215

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧