非阻塞呼吁阅读描述 [英] Non-blocking call for reading descriptor

查看:72
本文介绍了非阻塞呼吁阅读描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个FD描述符,我可以通过调用使用来自读取阅读(FD,缓冲,...)。现在,我要检查是否有什么实际进行通话,由于呼叫阻塞之前阅读。我该怎么做呢?


解决方案

  INT标志=的fcntl(FD,F_GETFL,0);
使用fcntl(fd,F_SETFL,旗帜| O_NONBLOCK);

上面的code片段将配置非阻塞访问这样的描述符。如果数据是,当你调用read不可用,则系统调用将失败,-1,并将errno的返回值设置为EAGAIN。请参阅 fnctl 手册页了解更多信息。

另外,你可以使用了一个可配置的超时选择检查和/或等待指定的时间间隔更多的数据。这种方法可能是你想要什么,可以更加高效。

I have a fd descriptor, which I can use to read from by calling read(fd, buffer,...). Now, I want to check if there is anything to read before actually making the call, because the call is blocking. How do I do this?

解决方案

int flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);

The code snippet above will configure such a descriptor for non-blocking access. If data is not available when you call read, then the system call will fail with a return value of -1 and errno is set to EAGAIN. See the fnctl man pages for more information.

Alternatively, you can use select with a configurable timeout to check and/or wait a specified time interval for more data. This method is probably what you want and can be much more efficient.

这篇关于非阻塞呼吁阅读描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆