Kqueue queries

Cole cole at opteqint.net
Tue Jun 12 20:45:25 UTC 2007


Hi.

I just have a few queries regarding kqueue. Ive read through the tutorial
given on the netbsd page, as well as the pdf detailing kqueue.

What I want to do is write a single threaded server for listening for
connections, and then check if they are ready to be read from for each
connection.

Ive written the code for this already, and got it handling multiple
connections, the problem comes in when the sockets close. 

This is my kevent call:
nev = kevent(kq, events, number_events, changes, number_events, NULL);

I wanted to know, what must be done when the sockets/file descriptors close.
Do I need to decrease number_events and resort the events array so that all
the active kevents are sequential without any closed sockets still in that
array?

Or, is it possible, to just keep increasing kevents and leave the closed
socket/file descriptors in the events array, and just mark them as
EV_DISABLED | EV_DELETE?

When I detect a closed socket, I have tried the following, but to no evail:
close(changes[i].ident);
EV_SET(&changes[i],changes[i].ident,EVFILT_READ,EV_DISABLE | EV_DELETE
,0,0,0);
kevent(kq, &changes[i], 1, NULL, 0, NULL);

If I do the above, and just keep increasing number_events and just mark the
kevent as EV_DISABLED or EV_DELETE then all it does is return that event as
soon as I call kevent() with the following values:
ident : 7, filter : -1, flags : 16384

Any suggestions/help would be appreciated, as well as pointing out if im
doing something stupid here.

Regards
/Cole




More information about the freebsd-hackers mailing list