Kqueue is a scalable event notification interface introduced in
FreeBSD
FreeBSD is a free and open-source Unix-like operating system descended from the Berkeley Software Distribution (BSD), which was based on Research Unix. The first version of FreeBSD was released in 1993. In 2005, FreeBSD was the most popular ...
4.1 in July 2000, also supported in
NetBSD
NetBSD is a free and open-source Unix operating system based on the Berkeley Software Distribution (BSD). It was the first open-source BSD descendant officially released after 386BSD was forked. It continues to be actively developed and is a ...
,
OpenBSD
OpenBSD is a security-focused, free and open-source, Unix-like operating system based on the Berkeley Software Distribution (BSD). Theo de Raadt created OpenBSD in 1995 by forking NetBSD 1.0. According to the website, the OpenBSD project em ...
,
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 Ju ...
, and
macOS
macOS (; previously OS X and originally Mac OS X) is a Unix operating system developed and marketed by Apple Inc. since 2001. It is the primary operating system for Apple's Mac computers. Within the market of desktop and lapt ...
. Kqueue was originally authored in 2000 by Jonathan Lemon, then involved with the
FreeBSD Core Team The FreeBSD Project is run by FreeBSD committers, or developers who have direct commit access to the master Git repository. The FreeBSD Core Team exists to provide direction and is responsible for setting goals for the FreeBSD Project and to provi ...
. Kqueue makes it possible for software like
nginx
Nginx (pronounced "engine x" ) is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. The software was created by Igor Sysoev and publicly released in 2004. Nginx is free and open-source software ...
to solve the
c10k problem
The C10k problem is the problem of optimizing network sockets to handle a large number of clients at the same time. The name C10k is a numeronym for concurrently handling ten thousand connections. Handling many concurrent connections is a differe ...
.
Kqueue provides efficient input and output event pipelines between the
kernel
Kernel may refer to:
Computing
* Kernel (operating system), the central component of most operating systems
* Kernel (image processing), a matrix used for image convolution
* Compute kernel, in GPGPU programming
* Kernel method, in machine learnin ...
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 (commonly abbreviated to 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, acc ...
to
kevent(2)
per main
event loop In computer science, the event loop is a programming construct or design pattern that waits for and dispatches events or messages in a program. The event loop works by making a request to some internal or external "event provider" (that generally ...
iteration. This contrasts with older traditional
polling
Poll, polled, or polling may refer to:
Figurative head counts
* Poll, a formal election
** Election verification exit poll, a survey taken to verify election counts
** Polling, voting to make decisions or determine opinions
** Polling places o ...
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 have ...
events but is also used for various other notifications such as
file modification monitoring,
signals
In signal processing, a signal is a function that conveys information about a phenomenon. Any quantity that can vary over space or time can be used as a signal to share messages between observers. The ''IEEE Transactions on Signal Processing'' ...
,
asynchronous I/O
In computer science, asynchronous I/O (also non-sequential I/O) is a form of input/output processing that permits other processing to continue before the transmission has finished. A name used for asynchronous I/O in the Windows API is overlappe ...
events (AIO),
child process
A child process 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 procedures ...
state change monitoring, and
timers
A timer is a specialized type of clock used for measuring specific time intervals.
Timers can be categorized into two main types.
The word "timer" is usually reserved for devices that counts down from a specified time interval, while devices th ...
which support
nanosecond
A nanosecond (ns) is a unit of time in the International System of Units (SI) equal to one billionth of a second, that is, of a second, or 10 seconds.
The term combines the SI prefix ''nano-'' indicating a 1 billionth submultiple of an SI unit ( ...
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, software resources, and provides common services for computer programs.
Time-sharing operating systems schedule tasks for efficient use of the system and may also in ...
s which traditionally only supported
select(2)
and
poll(2)
also currently provide more efficient polling alternatives, such as
epoll
epoll is a Linux kernel system call for a scalable I/O event notification mechanism, first introduced in version 2.5.44 of the Linux kernel. Its function is to monitor multiple file descriptors to see whether I/O is possible on any of them. It i ...
on
Linux
Linux ( or ) is a family of open-source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically packaged as a Linux distribution, which ...
and
I/O completion ports on
Windows
Windows is a group of several proprietary graphical operating system families developed and marketed by Microsoft. Each family caters to a certain sector of the computing industry. For example, Windows NT for consumers, Windows Server for serv ...
and
Solaris
Solaris may refer to:
Arts and entertainment Literature, television and film
* ''Solaris'' (novel), a 1961 science fiction novel by Stanisław Lem
** ''Solaris'' (1968 film), directed by Boris Nirenburg
** ''Solaris'' (1972 film), directed by ...
.
libkqueue
is a
user space
A modern computer operating system usually segregates virtual memory into user space and kernel space. Primarily, this separation serves to provide memory protection and hardware protection from malicious or errant software behaviour.
Kernel ...
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
epoll is a Linux kernel system call for a scalable I/O event notification mechanism, first introduced in version 2.5.44 of the Linux kernel. Its function is to monitor multiple file descriptors to see whether I/O is possible on any of them. It i ...
, 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
libevent is a software library that provides asynchronous event notification. The libevent API provides a mechanism to execute a callback function when a specific event occurs on a file descriptor or after a timeout has been reached. libevent als ...
*
libuv
libuv is a multi-platform C library that provides support for asynchronous I/O based on event loops. It supports epoll(4), kqueue(2), Windows IOCP, and Solaris event ports. It is primarily designed for use in Node.js but it is also used by ...
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
epoll is a Linux kernel system call for a scalable I/O event notification mechanism, first introduced in version 2.5.44 of the Linux kernel. Its function is to monitor multiple file descriptors to see whether I/O is possible on any of them. It i ...
system call has similar but not identical semantics.
**
inotify
inotify (inode notify) is a Linux kernel subsystem created by John McCutchan, which monitors changes to the filesystem, and reports those changes to applications. It can be used to automatically update directory views, reload configuration files, ...
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