非阻塞读取永不返回

问题描述 投票:0回答:2

我正在尝试进行非阻塞读取,但该函数永远不会返回。有人可以提出建议吗?这是我设置非阻塞 fd 的代码。

from_ap = open(FFS_GBEMU_OUT, O_RDWR|O_NONBLOCK);
if (from_ap < 0)
    return from_ap;

我也尝试过,结果类似

from_ap = open(FFS_GBEMU_OUT, O_RDWR);
int status = fcntl(from_ap, F_SETFL, fcntl(from_ap, F_GETFL, 0) | O_NONBLOCK);

if (status == -1){
perror("calling fcntl");

这是我调用读取函数的地方:

rsize = read(from_ap, cport_rbuf, ES1_MSG_SIZE);
if (rsize < 0) {
    printf("error %zd receiving from AP\n", rsize);
    return NULL;
}

我也尝试过,得到类似的结果:

fd_set readset;
struct timeval tv;
FD_ZERO(&readset);
FD_SET(from_ap, &readset);
tv.tv_sec = 0;
tv.tv_usec = 100;

result = select(from_ap+1, &readset, NULL, NULL, &tv);

if (result > 0 && FD_ISSET(from_ap, &readset)){
    printf("there was something to read\n");
    rsize=read(from_ap,cport_rbuf,ES1_MSG_SIZE);
}

收到的最后一条消息是“有东西要读”,并且代码没有进一步进展。我做错了什么?这不是一个多线程程序,所以没有人可以更改标志,但无论如何我在阅读之前通过打印回标志来确认它们。

c linux nonblocking file-descriptor ubuntu-15.04
2个回答
1
投票

设备是否支持O_NONBLOCK?这似乎是来自 GitHub 的 gbsim 代码。仔细阅读 gbsim,驱动程序完全有可能不支持非阻塞调用。


0
投票

O_NONBLOCK 对常规文件路径没有影响(并且您正在使用

open()
,所以我认为这就是您正在做的事情)。

© www.soinside.com 2019 - 2024. All rights reserved.