In kqueue it's possible to submit a kevent with EV_ONESHOT or EV_DISPATCH flags. The first deletes the kevent after first event delivery, and the latter disables the kevent after first event delivery.
The observable effect with both flags is that subsequent events will not be delivered unless the kevent is re-added or re-enabled, respectively.
Are there other observable effects between EV_ONESHOT and EV_DISPATCH and how to choose one over the other?
The manual page for disabling a kevent using EV_DISABLE mentions that The filter itself is not disabled. Then, if I choose to use EV_DISPATCH, the filter itself is not disabled. I don’t understand what effect that has. Could someone explain?
I'll answer my own question after having done more research.
I recommend reading the 2001 USENIX paper "Kqueue: A generic and scalable event notification facility” at http://people.freebsd.org/~jlemon/papers/kqueue.pdf.
When kernel receives a
keventfrom user, it creates aknoteto represent the subscription and a filter to deal with the event source. The filter is invoked on event source activity such as packet arrival or signal delivery. Theknoteis added to an active list for event delivery to user if the filter indicates so and theknoteis enabled. This is enough to understand the difference betweenEV_ONESHOTandEV_DISPATCH.EV_ONESHOTcauses theknoteand the filter to be deleted after first event delivery. Thus the filter can no longer be called on event source activity.EV_DISPATCHcauses theknoteto be marked disabled after first event delivery, but the filter will remain to process event source activity. Whether this is a meaningful difference depends on the filter being used. Additionally, enable/disable is a faster operation than add/delete. The USENIX paper has some graphs on the performance.I wrote a program (tested on macOS) to demonstrate the difference using
EVFILT_SIGNAL. Maybe someone will find this useful in the future.