Documentation
¶
Overview ¶
User demonstrates kqueue(2) send and receive a user event.
Synopsis:
kquser
Background:
BSD-like systems (*BSD, MacOS, etc.) provide kqueue(2) to monitor events from a wide range of systems: file system, processes, signals, jails, timer, user, etc. (MacOS does not include jails).
For the record, Linux has similar mechanism, implemented via select(2), poll(2), epoll(7), or inotify(7).
Kqueue(2) is a stateful mechanism: Kernel keeps track of subscriptions. It is important to close kqueue(2) when done to clean up resources.
Kqueue(2) provides a single function to manage and poll events. It guarantees to process manage-events first, and then update the poll-events. Even though it is possible to use both kinds of events in the same call to kevent(2), it is best to separate the two.
Example:
This example demonstrates the following aspects of working with kqueue(2):
- Create and cleanup kqueue(2) Kernel resources. - Subscribe to User events. - Send and receive user events.
Output:
2026/01/05 16:13:50 sent event event-id-two 2026/01/05 16:13:50 received event event-id-two