Queues Explained with Real Examples from the Linux Kernel
In the last post we looked at the stack, where the last thing in is the first thing out. This time we look at its opposite, the queue. It is just as simple, and the Linux kernel leans on it all the time. Let's start with the idea, write a small one in C, and then see where the kernel uses it. What is a queue? Think of the line at a ticket counter. A new person joins at the back. The person at the…
A queue is similar to a line at a ticket counter, where the first person in line gets their ticket first. In computing, a queue is a data structure with two main operations: enqueue (adding an item to the back) and dequeue (removing an item from the front). Unlike a stack, which serves the most recently added item first, a queue always serves items in the order they arrived, following the first-in, first-out (FIFO) rule.
The article provides a simple implementation of a queue using an array and two pointers (head and tail) to keep track of the front and back of the queue. When the array is full, the tail pointer wraps around to the beginning, creating a circular buffer. The Linux kernel extensively uses queues for various purposes, such as scheduling processes, managing semaphores, and implementing pipes.
The concepts covered in the article can be extended to create more complex data structures, like linked lists, which are also used in the kernel.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.