Why does poll keep returning although there is no input?

后端 未结 3 1104
南旧
南旧 2020-12-18 07:12

I wrote a small test program to figure out how to talk to poll. I created three files testa,testb,testc and wrote the string hel

相关标签:
3条回答
  • 2020-12-18 07:34

    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.

    0 讨论(0)
  • 2020-12-18 07:38

    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.

    0 讨论(0)
  • 2020-12-18 07:44

    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.

    0 讨论(0)
提交回复
热议问题