Skip to content

Latest commit

 

History

History
169 lines (129 loc) · 3.75 KB

File metadata and controls

169 lines (129 loc) · 3.75 KB

Examples Directory - Updated

Available Examples

Basic Examples

  1. 01-hello/ - Simple module (5 min)
  2. 02-chardev/ - Character device with read/write (15 min)

Intermediate Examples

  1. 03-ioctl/ - Device control with ioctl (20 min)

    • ioctl command encoding
    • Userspace test program
    • Configuration structures
  2. 04-memory/ - Memory allocation (10 min)

    • kmalloc, vmalloc, slab caches
    • Different allocation strategies
    • Memory size information
  3. 05-locks/ - Synchronization (20 min)

    • Race condition demonstration
    • Spinlocks, mutexes, atomics
    • Multi-threaded testing
  4. 06-timer/ - Kernel timers (15 min)

    • Periodic execution
    • Jiffies and time conversion
    • Module parameters

Quick Start Guide

Try Them in Order

# 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

Difficulty Levels

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

Prerequisites

All examples require:

sudo apt-get install linux-headers-$(uname -r) build-essential

Testing Tips

  1. Always use dmesg to see kernel output:

    # Watch in real-time
    dmesg -w
    
    # Or check after
    dmesg | tail -20
  2. Check if module loaded:

    lsmod | grep module_name
  3. Module information:

    modinfo module_name.ko
  4. If something goes wrong:

    # Force remove module
    sudo rmmod -f module_name
    
    # Check for errors
    dmesg | grep -i error

Learning Path

Week 1: Basics

  • Day 1-2: 01-hello (understand module structure)
  • Day 3-4: 02-chardev (learn device files)
  • Day 5-7: 03-ioctl (device control)

Week 2: Advanced

  • Day 1-3: 04-memory (memory management)
  • Day 4-5: 05-locks (critical - understand locking!)
  • Day 6-7: 06-timer (timers and scheduling)

Common Issues

Module won't load

# Check dmesg for specific error
dmesg | tail

# Verify kernel headers
ls /lib/modules/$(uname -r)/build

Device not created

# Check if device class created
ls /sys/class/

# Verify udev is running
systemctl status systemd-udevd

Compilation errors

# Clean and rebuild
make clean && make

# Check kernel version match
uname -r

Next Steps

After completing these examples:

  1. Modify them to understand how they work
  2. Combine concepts (e.g., chardev + ioctl + timer)
  3. Read the corresponding chapter in the tutorial
  4. Try writing your own driver

Additional Resources

Happy learning! 🚀