A Linux kernel module that creates multiple processes/threads in a binary tree structure and displays their task names, states, and process IDs in hierarchical format. Demonstrates kernel-level thread management, process tree visualization and memory management for Operating Systems project.
This project was developed as part of an Operating Systems course to demonstrate understanding of Linux kernel programming concepts. The kernel module creates a binary tree of kernel threads, where each node represents a process/thread with its associated metadata.
Problem Statement: Create a Linux kernel module that spawns multiple processes/threads (children and siblings) and outputs their task names, states, and process IDs in a tree structure while the task is executing.
- π³ Binary Tree Creation: Generates a hierarchical structure of kernel threads
- π Process Information Display: Shows task names, PIDs, and parent-child relationships
- π Recursive Thread Management: Implements recursive thread creation and cleanup
- π§ Memory Management: Proper kernel memory allocation and deallocation
- π Kernel Logging: Comprehensive logging using
printk()
for debugging - π Signal Handling: Graceful thread termination with signal management
- ποΈ Data Structures: Custom tree implementation using Linux kernel linked lists
- C: Core kernel module implementation
- Makefile: Build configuration and compilation
linux/init.h
- Module initialization/cleanuplinux/module.h
- Module metadata and macroslinux/kernel.h
- Kernel utility functionslinux/sched.h
- Process/task managementlinux/kthread.h
- Kernel threading APIlinux/signal.h
- Signal handlinglinux/slab.h
- Memory allocation (kmalloc
,kfree
)linux/list.h
- Kernel linked list implementation
- GCC: GNU Compiler Collection for kernel compilation
- Make: Build automation tool
- Linux Kernel Build System: For module compilation
- dmesg: Kernel message logging and debugging
- Linux Kernel: Version 4.x or higher
- Architecture: x86_64 (tested on Ubuntu/Debian)
- Permissions: Root/sudo access required
- Linux distribution (Ubuntu 20.04+ recommended)
- Kernel headers installed
- GCC compiler
- Make utility
- Root/sudo privileges
Ubuntu/Debian:
sudo apt update
sudo apt install build-essential linux-headers-$(uname -r)
CentOS/RHEL/Fedora:
sudo yum groupinstall "Development Tools"
sudo yum install kernel-devel kernel-headers
Arch Linux:
sudo pacman -S base-devel linux-headers
git clone https://github.com/yourusername/linux-kernel-process-tree-module.git
cd linux-kernel-process-tree-module
make
Expected Output:
make -C /lib/modules/6.5.0-25-generic/build M=/home/user/project modules
make[1]: Entering directory '/usr/src/linux-headers-6.5.0-25-generic'
CC [M] /home/user/project/my_kernel_module12.o
MODPOST /home/user/project/Module.symvers
CC [M] /home/user/project/my_kernel_module12.mod.o
LD [M] /home/user/project/my_kernel_module12.ko
make[1]: Leaving directory '/usr/src/linux-headers-6.5.0-25-generic'
sudo insmod my_kernel_module.ko
sudo dmesg
sudo rmmod my_kernel_module
make clean
linux-kernel-process-tree-module/
βββ my_kernel_module.c # Main kernel module source code
βββ Makefile # Build configuration
βββ README.md # Project documentation
βββ .gitignore # Git ignore file (optional)
my_kernel_module.c
: Core implementation containing thread creation, tree management, and cleanup logicMakefile
: Defines build rules for kernel module compilationREADME.md
: Comprehensive project documentation
- Module Initialization: Creates a root kernel thread
- Binary Tree Construction: Recursively spawns child threads (2 per level)
- Tree Traversal: Displays the complete hierarchy
- Memory Management: Proper allocation/deallocation of tree nodes
- Module Cleanup: Graceful termination of all threads
struct tree_node {
int pid; // Process ID
char name[16]; // Thread name
struct list_head children; // List of child nodes
struct list_head sibling; // Sibling node link
};
- Each thread runs
child_function()
- Handles signals for graceful termination
- Maintains interruptible sleep state
- Levels: Configurable depth (default: 3 levels)
- Branching: Binary tree (2 children per node)
- Naming: Pattern
thread_<level>_<index>
- Uses
kmalloc()
for node allocation - Implements proper cleanup with
kfree()
- Handles allocation failures gracefully
[INFO] Binary Tree Logger Module: Initialization
[INFO] Created root thread: PID=1234
[INFO] Created thread: PID=1235, Parent PID=1234, Level=1
[INFO] Created thread: PID=1236, Parent PID=1234, Level=1
[INFO] Created thread: PID=1237, Parent PID=1235, Level=2
[INFO] Created thread: PID=1238, Parent PID=1235, Level=2
[INFO] Created thread: PID=1239, Parent PID=1236, Level=2
[INFO] Created thread: PID=1240, Parent PID=1236, Level=2
[INFO] Process Tree Structure:
βββ root_thread(1234)
βββ thread_1_0(1235)
βββ thread_2_0(1237)
βββ thread_2_1(1238)
βββ thread_1_1(1236)
βββ thread_2_0(1239)
βββ thread_2_1(1240)
Problem: fatal error: linux/module.h: No such file or directory
Solution:
sudo apt install linux-headers-$(uname -r)
Problem: Operation not permitted
when loading module
Solution: Ensure you're using sudo
:
sudo insmod my_kernel_module.ko
Problem: File exists
error
Solution: Remove existing module first:
sudo rmmod my_kernel_module
sudo insmod my_kernel_module.ko
Problem: Missing kernel build directory Solution: Install matching kernel headers:
sudo apt install linux-headers-$(uname -r)
- Check kernel logs:
sudo dmesg
- Verify module loading:
lsmod | grep my_kernel_module
- Check system resources:
free -h
andps aux
- Monitor system: Use
htop
ortop
while module runs
This project demonstrates proficiency in:
- Kernel Programming: Understanding of Linux kernel APIs and development practices
- Process Management: Creation and management of kernel threads
- Data Structures: Implementation of tree structures using kernel linked lists
- Memory Management: Proper allocation and deallocation in kernel space
- System Programming: Low-level programming concepts and debugging
- Concurrency: Thread synchronization and signal handling
- Build Systems: Makefile creation and kernel build process
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request