What does poll() do with a timeout of 0?

匿名 (未验证) 提交于 2019-12-03 08:36:05

问题:

I'm looking at the poll() man page, and it tells me the behaviour of poll() when positive and negative values are passed in for the timeout parameter. It doesn't doesn't tell me what happens if timeout is 0. Any ideas?

Looking at the epoll_wait() man page, it tells me that with a timeout value of 0, it will return right away, even if there are no events available. Is it safe to assume that poll() would behave the same way?

回答1:

It will return immediately:

If timeout is greater than zero, it specifies a maximum interval (in milliseconds) to wait for any file descriptor to become ready. If timeout is zero, then poll() will return without blocking. If the value of timeout is -1, the poll blocks indefinitely.

, as of Mac OS X 10.5;

Maximum interval to wait for the poll to complete, in milliseconds. If this value is 0, poll() will return immediately. If this value is INFTIM (-1), poll() will block indefinitely until a condition is found.

, as of OpenBSD 3.8



回答2:

As I see it, waiting for a timeout means "having" a timeout. This way I would expect that poll() actually checks the file descriptors, and then waits if no one is ready to a timeout of 0 milliseconds (no wait at all). But the case is that it will just signal if a fd is available.

I also checked linux source code and to my knowledge, this is the way it works: first calculates the "future" waiting point, then checks the file descriptors, then if none available, waits for the timeout specified time.

Regards,



回答3:

From the Ubuntu man pages:

The timeout argument specifies an upper limit on the time for which poll() will block, in milliseconds. Specifying a negative value in timeout means an infinite timeout.

Because there is no special case for 0, I would assume that poll() will block for 0 milliseconds.



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