Kqueue is a scalable event notification interface introduced inFreeBSD 4.1 in July 2000,[1][2] also supported inNetBSD,OpenBSD,DragonFly BSD, andmacOS. Kqueue was originally authored in 2000 by Jonathan Lemon,[1][2] then involved with theFreeBSD Core Team. Kqueue makes it possible for software likenginx to solve thec10k problem.[3][4] The term "kqueue" refers to its function as a "kernel event queue"[1][2]
Kqueue provides efficient input and output event pipelines between thekernel anduserland. Thus, it is possible to modify event filters as well as receive pending events while using only a singlesystem call tokevent(2) per mainevent loop iteration. This contrasts with older traditionalpolling system calls such aspoll(2) andselect(2) which are less efficient, especially when polling for events on numerous file descriptors.
Kqueue not only handlesfile descriptor events but is also used for various other notifications such asfile modification monitoring,signals,asynchronous I/O events (AIO),child process state change monitoring, andtimers which supportnanosecond resolution. Furthermore, kqueue provides a way to use user-defined events in addition to the ones provided by the kernel.
Some otheroperating systems which traditionally only supportedselect(2) andpoll(2) also currently provide more efficient polling alternatives, such asepoll onLinux andI/O completion ports onWindows andSolaris.
libkqueue is auser space implementation ofkqueue(2), which translates calls to an operating system's native backend event mechanism.[5]
This sectionrelies largely or entirely on asingle source. Relevant discussion may be found on thetalk page. Please helpimprove this article by introducingcitations to additional sources at this section. Unsourced material may be challenged and removed. Find sources: "Kqueue" – news ·newspapers ·books ·scholar ·JSTOR(April 2024) (Learn how and when to remove this message) |
The function prototypes and types are found in<sys/event.h>.[6]
intkqueue(void);
Creates a new kernel event queue and returns a descriptor.
intkevent(intkq,conststructkevent*changelist,intnchanges,structkevent*eventlist,intnevents,conststructtimespec*timeout);
Used to register events with the queue, then wait for and return any pending events to the user. In contrast toepoll, kqueue uses the same function to register and wait for events, and multiple event sources may be registered and modified using a single call. Thechangelist array can be used to pass modifications (changing the type of events to wait for, register new event sources, etc.) to the event queue, which are applied before waiting for events begins.nevents is the size of the user suppliedeventlist array that is used to receive events from the event queue.
EV_SET(kev,ident,filter,flags,fflags,data,udata);
A macro that is used for convenient initialization of astruct kevent object.
OS-independent libraries with support for kqueue:
Kqueue equivalent for other platforms:
kqueue can also notify when a file descriptor is ready to perform an I/O operation.kqueue(2) – FreeBSD System CallsManualkqueue(2) – FreeBSD System CallsManualkqueue() system callkqueue() system callkqueue() system callkqueue() system callkqueue() system call