Kqueue is a scalable event notification interface introduced in
FreeBSD
FreeBSD is a free-software Unix-like operating system descended from the Berkeley Software Distribution (BSD). The first version was released in 1993 developed from 386BSD, one of the first fully functional and free Unix clones on affordable ...
4.1 in July 2000, also supported in
NetBSD
NetBSD is a free and open-source Unix-like operating system based on the Berkeley Software Distribution (BSD). It was the first open-source BSD descendant officially released after 386BSD was fork (software development), forked. It continues to ...
,
OpenBSD
OpenBSD is a security-focused operating system, security-focused, free software, Unix-like operating system based on the Berkeley Software Distribution (BSD). Theo de Raadt created OpenBSD in 1995 by fork (software development), forking NetBSD ...
,
DragonFly BSD
DragonFly BSD is a free and open-source Unix-like operating system forked from FreeBSD 4.8. Matthew Dillon, an Amiga developer in the late 1980s and early 1990s and FreeBSD developer between 1994 and 2003, began working on DragonFly BSD in ...
, and
macOS
macOS, previously OS X and originally Mac OS X, is a Unix, Unix-based operating system developed and marketed by Apple Inc., Apple since 2001. It is the current operating system for Apple's Mac (computer), Mac computers. With ...
. Kqueue was originally authored in 2000 by Jonathan Lemon, then involved with the
FreeBSD Core Team. Kqueue makes it possible for software like
nginx to solve the
c10k problem. The term "kqueue" refers to its function as a "kernel event queue"
Kqueue provides efficient input and output event pipelines between the
kernel and
userland. Thus, it is possible to modify event filters as well as receive pending events while using only a single
system call
In computing, a system call (syscall) is the programmatic way in which a computer program requests a service from the operating system on which it is executed. This may include hardware-related services (for example, accessing a hard disk drive ...
to
kevent(2)
per main
event loop iteration. This contrasts with older traditional
polling system calls such as
poll(2)
and
select(2)
which are less efficient, especially when polling for events on numerous file descriptors.
Kqueue not only handles
file descriptor
In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier (handle) for a file or other input/output resource, such as a pipe or network socket.
File descriptors typically h ...
events but is also used for various other notifications such as
file modification monitoring,
signals,
asynchronous I/O events (AIO),
child process
A child process (CP) in computing is a process created by another process (the parent process). This technique pertains to multitasking operating systems, and is sometimes called a subprocess or traditionally a subtask.
There are two major proce ...
state change monitoring, and
timers which support
nanosecond resolution. Furthermore, kqueue provides a way to use user-defined events in addition to the ones provided by the kernel.
Some other
operating system
An operating system (OS) is system software that manages computer hardware and software resources, and provides common daemon (computing), services for computer programs.
Time-sharing operating systems scheduler (computing), schedule tasks for ...
s which traditionally only supported
select(2)
and
poll(2)
also currently provide more efficient polling alternatives, such as
epoll on
Linux
Linux ( ) is a family of open source Unix-like operating systems based on the Linux kernel, an kernel (operating system), operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically package manager, pac ...
and
I/O completion ports on
Windows
Windows is a Product lining, product line of Proprietary software, proprietary graphical user interface, graphical operating systems developed and marketed by Microsoft. It is grouped into families and subfamilies that cater to particular sec ...
and
Solaris.
libkqueue
is a
user space
A modern computer operating system usually uses virtual memory to provide separate address spaces or regions of a single address space, called user space and kernel space. This separation primarily provides memory protection and hardware prote ...
implementation of
kqueue(2)
, which translates calls to an operating system's native backend event mechanism.
API
The function prototypes and types are found in
sys/event.h
.
int kqueue(void);
Creates a new kernel event queue and returns a descriptor.
int kevent(int kq, const struct kevent *changelist, int nchanges, struct kevent *eventlist, int nevents, const struct timespec *timeout);
Used to register events with the queue, then wait for and return any pending events to the user. In contrast to
epoll, kqueue uses the same function to register and wait for events, and multiple event sources may be registered and modified using a single call. The
changelist
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 supplied
eventlist
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 a
struct kevent
object.
See also
OS-independent libraries with support for kqueue:
*
libevent
*
libuv
Kqueue equivalent for other platforms:
* on Solaris, Windows and AIX:
I/O completion ports. Note that completion ports notify when a requested operation has completed, whereas
kqueue
can also notify when a file descriptor is ready to perform an I/O operation.
* on Linux:
**
epoll system call has similar but not identical semantics.
**
inotify is a Linux kernel subsystem that notices changes to the filesystem and reports those to applications.
References
External links
*
libbrb_core implements an abstraction for an event-oriented base, using kqueue()
system callFreeBSD source code of the kqueue()
system callOpenBSD source code of the kqueue()
system callNetBSD source code of the kqueue()
system callDragonFly BSD source code of the kqueue()
system call
{{macOS
Events (computing)
BSD software
FreeBSD
OpenBSD
NetBSD
DragonFly BSD
MacOS
Operating system APIs
Operating system technology
System calls