0

I have a list of a bunch of file descriptors that I have created kevents for, and I'm trying to figure out if there's any way to get the number of them that are ready for read or write access.

Is there any way to get a list of "ready" file descriptors, like what epoll_wait provides?

clee
  • 10,943
  • 6
  • 36
  • 28

2 Answers2

2

Events that occurred are placed into the eventlist buffer passed to the kevent call. So making this buffer sufficiently large will give you the list you are seeking. The return value of the kevent call will tell you have many events are in the eventlist buffer.

If using a large buffer is not feasible for some reason, you can always do a loop calling kevent with a zero timeout and a smaller buffer, until you get zero events in the eventlist.

Grrrr
  • 2,477
  • 20
  • 21
  • Don't I need to pass a file descriptor in with the kevent call, though? I guess I'll need to play with this some more. – clee Sep 27 '10 at 17:55
  • You use the `kevent` call for both registering your interest in events of a certain kind, and for retrieving the events that actually occur. You can combine it in a single call, but you don't have to. Once you register you interest in a file descriptor, `kevent` will report relevant events for this descriptor, unless you used `EV_ONESHOT`, or deleted the event, or closed the descriptor. – Grrrr Sep 27 '10 at 21:32
0

To give a little more context...

One of the expected scenarios with kevent() is that you will thread pool calls to it. If you had 3 thread pools all asking for 4 events, the OS would like to be able to pool and dispatch the actual events as it sees fit.

If 7 events are available the OS might want to dispatch to 3 threads, or it might want to dispatch to all 3 threads if it believed it had empty cores and less overhead.

I'm not saying your scenario is invalid at all; just that the system is more or less designed to keep that information away from you so it doesn't get into scenarios of saying 'well, 12 descriptors are ready. Oh, hrm, I just told you that but 3 of them got surfaced before you had a chance to do anything'.

Grrr pretty much nailed the scenario. You register/deregister your descriptors once and the relevent descriptor will be provided back to you with the event when the event triggers.

Joe
  • 2,946
  • 18
  • 17