Why does poll keep returning although there is no input?

你离开我真会死。 提交于 2019-11-29 07:25:25

An EOF condition in a regular file is still readable. In other words, your read() won't block. Here's a nice list of how different implementations of poll() react to EOF in different sorts of file descriptors: http://www.greenend.org.uk/rjk/tech/poll.html

Note that regular files always return POLLIN. So you need to test for EOF separately. In fact, poll on a regular file doesn't do anything for you. You'll need sockets or pipes or something to test your code.

Other notes: you probably want to check for other results in .revents. POLLERR, POLLHUP, and POLLNVAL all signal different error conditions, and need special handling.

Once you reach the end of a file, it remains readable so that poll will return immediately, and calling read will immediately return zero. You need to handle this condition, perhaps by closing it and removing it from the set of polls, where you're currently printing Strange.

The local file descriptors are always ready to perform I/O (unlike sockets, because they are depend on the kernel internal buffer for I/O)). In your case file descriptors are always ready for reading, even if they are empty actually.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!