Linux drivers and event queues
Model how a driver buffers incoming events before a consumer handles them.
- Distinguish kernel code from a user-space C program
- Trace head, tail and count in a bounded ring queue
- Handle full and empty states without overwriting data
The Linux kernel uses C for core services and device drivers. A driver may receive device data faster than a consumer can process it, so it buffers events. The kernel circular buffer guide describes a fixed-size queue with producer and consumer indices; platform driver documentation explains how drivers attach to devices. A ring queue is a useful first model before studying kernel APIs and concurrency.
Try it
Up to 15 XPBe the driver and consumer
Produce A and B, then consume them in order. Observe head and tail as they move around the fixed slots. Try consuming from an empty queue.
Mission 1: Queue event A.
Score 0/4A single-threaded model of a driver event queue. Real kernel producer and consumer code needs the documented synchronization and memory-ordering rules.
head=0 · tail=0 · count=0/4
Producer adds an event; consumer handles the oldest event.
1#include <stdio.h>
2#define CAPACITY 4
3int main(void) {
4 int slots[CAPACITY] = {0};
5 int head = 0, tail = 0, count = 0;
6 slots[head] = 42;
7 head = (head + 1) % CAPACITY;
8 count++;
9 printf("queued=%d head=%d tail=%d count=%d\n", slots[tail], head, tail, count);
10 return 0;
11}queued=42 head=1 tail=0 count=1
This browser example is user-space C, not a kernel module. Real kernel drivers use kernel headers, build tools, device APIs and synchronization rules; they cannot use this runner’s stdio program directly. For concurrent producer and consumer code, use the kernel’s documented circular buffer and memory-ordering rules. The exercise below intentionally models a single-threaded queue so you can first master the index and full/empty logic.
Key takeaways
A ring queue keeps fixed storage and advances indices modulo capacity.
Check full before pushing and empty before popping.
Kernel code has different APIs and synchronization requirements from a portable user-space example.
Lesson quiz
4 questions · pass with 3 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write C
These run for real. Write the program, press Run tests, and read what the compiler tells you - learning to read compiler messages is half of learning C.
Process a bounded event stream
Implement a four-slot integer queue. Read commands until EOF: P n pushes n, C consumes and prints the oldest n. Print FULL if a push cannot fit and EMPTY if a consume has nothing. Use head, tail and count; do not overwrite unread values.
- FIFO order
- Full and wrap
Your code is compiled with gcc 14 (-Wall -Wextra) and run on Compiler Explorer (godbolt.org), a free public service.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…