- 01-hello/ - Simple module (5 min)
- 02-chardev/ - Character device with read/write (15 min)
-
03-ioctl/ - Device control with ioctl (20 min)
- ioctl command encoding
- Userspace test program
- Configuration structures
-
04-memory/ - Memory allocation (10 min)
- kmalloc, vmalloc, slab caches
- Different allocation strategies
- Memory size information
-
05-locks/ - Synchronization (20 min)
- Race condition demonstration
- Spinlocks, mutexes, atomics
- Multi-threaded testing
-
06-timer/ - Kernel timers (15 min)
- Periodic execution
- Jiffies and time conversion
- Module parameters
# 1. Hello World (easiest)
cd 01-hello && make && sudo insmod hello.ko
dmesg | tail
sudo rmmod hello
# 2. Character Device
cd ../02-chardev && make && sudo insmod simple_char.ko
echo "test" > /dev/simple_char && cat /dev/simple_char
sudo rmmod simple_char
# 3. ioctl Control
cd ../03-ioctl && make && sudo insmod ioctl_device.ko
./test_ioctl
sudo rmmod ioctl_device
# 4. Memory Management
cd ../04-memory && make && sudo insmod memory_demo.ko
dmesg | tail -30
sudo rmmod memory_demo
# 5. Locking (most interesting!)
cd ../05-locks && make && sudo insmod lock_demo.ko
dmesg | tail -50
sudo rmmod lock_demo
# 6. Timers
cd ../06-timer && make && sudo insmod timer_demo.ko
sleep 12 # Watch it fire 10 times
sudo rmmod timer_demo| Example | Difficulty | Time | Key Concepts |
|---|---|---|---|
| 01-hello | ⭐ Easy | 5 min | Module basics |
| 02-chardev | ⭐⭐ Medium | 15 min | Device files, file ops |
| 03-ioctl | ⭐⭐ Medium | 20 min | Device control, ioctl |
| 04-memory | ⭐⭐ Medium | 10 min | Memory allocation |
| 05-locks | ⭐⭐⭐ Advanced | 20 min | Race conditions, locks |
| 06-timer | ⭐⭐ Medium | 15 min | Timers, periodic tasks |
All examples require:
sudo apt-get install linux-headers-$(uname -r) build-essential-
Always use dmesg to see kernel output:
# Watch in real-time dmesg -w # Or check after dmesg | tail -20
-
Check if module loaded:
lsmod | grep module_name -
Module information:
modinfo module_name.ko
-
If something goes wrong:
# Force remove module sudo rmmod -f module_name # Check for errors dmesg | grep -i error
- Day 1-2: 01-hello (understand module structure)
- Day 3-4: 02-chardev (learn device files)
- Day 5-7: 03-ioctl (device control)
- Day 1-3: 04-memory (memory management)
- Day 4-5: 05-locks (critical - understand locking!)
- Day 6-7: 06-timer (timers and scheduling)
# Check dmesg for specific error
dmesg | tail
# Verify kernel headers
ls /lib/modules/$(uname -r)/build# Check if device class created
ls /sys/class/
# Verify udev is running
systemctl status systemd-udevd# Clean and rebuild
make clean && make
# Check kernel version match
uname -rAfter completing these examples:
- Modify them to understand how they work
- Combine concepts (e.g., chardev + ioctl + timer)
- Read the corresponding chapter in the tutorial
- Try writing your own driver
- Kernel Documentation
- Tutorial chapters in parent directory
- Code comments in each example
Happy learning! 🚀