非阻塞打开设备文件,如果1秒read不到数据,想让read退出如何做?

besthyq 2006-12-23 07:40:44
现在是这样,从一个设备读数据,读不到数据的时候,read会一直阻塞住,如何中止read呢?

请大家稍微提示下把。顶贴有分
...全文
436 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
besthyq 2006-12-24
  • 打赏
  • 举报
回复
FD_SET(0, &rfds);

这个是指定设备么?
besthyq 2006-12-24
  • 打赏
  • 举报
回复
楼上说的是,是阻塞打开文件

我是直接用open来打开设备文件的。

fd = open()

这个时候,fd是谁被描述符吧?

还要用ioctl来设置描述符?
无知者无谓 2006-12-24
  • 打赏
  • 举报
回复
非阻塞打开设备文件?什么意思?
既然它是非阻塞的为什么又“一直阻塞住”,请明示
不知道你怎么打开的设备文件,一般是用ioctl来设置文件描述符的属性的
一般select是使用在阻塞式上的
gangjh 2006-12-23
  • 打赏
  • 举报
回复
select不够用?
还是楼主想知道回子有多少种写法?
那就试用epoll吧
besthyq 2006-12-23
  • 打赏
  • 举报
回复
明白了。select的用法了

除了select还有其他用法么
besthyq 2006-12-23
  • 打赏
  • 举报
回复
有点弄不明白。select不需要指定设备文件吗?
gangjh 2006-12-23
  • 打赏
  • 举报
回复
select 监听fd_set的设备,如果某个设备有数据可读或可写,返回fileno,
超时返回0, 出错-1 ,可以根据返回值决定是否去read
man select
楼主可以google一下.
besthyq 2006-12-23
  • 打赏
  • 举报
回复
我是问,select本身的运行时间是长还是短~。:)
gangjh 2006-12-23
  • 打赏
  • 举报
回复
select可以设定timeout哦,时间自己设定
int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
besthyq 2006-12-23
  • 打赏
  • 举报
回复
谢谢楼上的。select可以用在设备文件上吧。

select运行时间很短吧。

还有其他方法么。
gangjh 2006-12-23
  • 打赏
  • 举报
回复
用select 吧
man select
里面有个范例.
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

int
main(void) {
fd_set rfds;
struct timeval tv;
int retval;

/* Watch stdin (fd 0) to see when it has input. */
FD_ZERO(&rfds);
FD_SET(0, &rfds);

/* Wait up to five seconds. */
tv.tv_sec = 5;
tv.tv_usec = 0;

retval = select(1, &rfds, NULL, NULL, &tv);
/* Don’t rely on the value of tv now! */

if (retval == -1)
perror("select()");
else if (retval)
printf("Data is available now.\n");
/* FD_ISSET(0, &rfds) will be true. */
else
printf("No data within five seconds.\n");

return 0;
}

23,121

社区成员

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

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