问题描述
我们已经在我们的生产code长期存在的BUG。这本质上是基于套接字守护进程。它侦听一堆使用选择filedescriptors的。
We have a long standing bug in our production code. This is essentially a socket based daemon. It listens to a bunch of filedescriptors using select.
偶尔(每天一次左右),选择将与EBADF回来。
Occasionally (once a day or so), select will return with EBADF.
我已经写了code搜索坏文件描述符,即遍历每个FD并呼吁将其选中。这些调用不返回EBADF。我也试过FSTAT。他们也不会返回EBADF。
I have written code to search for the bad filedescriptor, that loops over each fd and calls select on it. These calls never return EBADF. I also tried fstat. They also never return EBADF.
我还改写了后台程序使用轮询。这并没有帮助。
I also rewrote the daemon to use poll. This did not help.
有没有人有一些其他的想法? (除了我犯了一个愚蠢的错误,这是所有容易选择做)。
Does anyone have some other ideas ? (apart from i made a dumb mistake, which is all to easy to do with select).
推荐答案
我同意詹姆斯。随着民调(),你必须每它可以轻松地进行检查FD revents中。
I agree with James. With poll(), you have revents per fd which can easily be checked.
即
struct pollfd fds[NUM_FDS];
int ret, i;
...
ret = poll(fds, NUM_FDS, POLL_TIMEOUT);
for (i = 0; i < NUM_FDS; i++)
if (fds[i].revents & POLLHUP || fds[i].revents & POLLNVAL)
... do something ...
当然,你也不会实现它在现实世界的方式,它只是一个例子。我停止使用select()很久以前,民意调查()是一个更好的界面。你是正确的,它只是太容易搬起石头砸自己的脚用select()。
Of course you would not implement it that way in the real world, its just an example. I stopped using select() a long time ago, poll() is a much better interface. You're correct, its just too easy to shoot yourself in the foot with select().
这篇关于选择EBADF:哪些FD是坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!