Linux Kernel Process Management
Linux Kernel Process Management
1. Introduction to Process Management in Linux
Process management is one of the core responsibilities of the Linux kernel. It involves:
-
Creating processes
-
Scheduling them on the CPU
-
Managing their execution
-
Handling their termination
Linux follows a simple yet powerful design philosophy:
The kernel uses one unified abstraction—the task—to represent both processes and threads.
2. The Process Abstraction in Linux
In Linux:
-
A process is an instance of a program in execution
-
A thread is simply a process that shares resources
Both are represented by the same kernel data structure:
This unified design simplifies kernel logic and improves scalability.
3. The task_struct: Process Descriptor
Every process in Linux is described by a process descriptor, the task_struct.
Key Information Stored in task_struct
-
Process state
-
Process ID (PID)
-
Scheduling information
-
Parent and child relationships
-
Virtual memory information
-
Open files
-
Signal handlers
-
Kernel stack pointer
📌 Each running task in the system has exactly one task_struct.
4. Process States in Linux
Linux defines several process states (more detailed than the classic 3-state model).
| State | Meaning |
|---|---|
TASK_RUNNING | Running or ready to run |
TASK_INTERRUPTIBLE | Sleeping, can be woken by signals |
TASK_UNINTERRUPTIBLE | Sleeping, cannot be interrupted |
TASK_STOPPED | Execution stopped (e.g., via signal) |
TASK_TRACED | Being traced by debugger |
EXIT_ZOMBIE | Process has exited but not reaped |
EXIT_DEAD | Process is being cleaned up |
5. Process Identification: PID and TGID
PID (Process Identifier)
-
Unique identifier for each task
-
Assigned when the process is created
TGID (Thread Group ID)
-
Used to group threads belonging to the same process
-
All threads in a process share the same TGID
-
The thread group leader’s PID = TGID
6. Process Creation in Linux
fork() – Creating a Process
Linux implements fork() efficiently using Copy-On-Write (COW):
-
Parent and child share memory pages
-
Pages are copied only when modified
Steps:
-
Allocate new
task_struct -
Duplicate parent context
-
Assign new PID
-
Share address space initially
vfork() – Special Case
-
Child shares address space with parent
-
Parent is blocked until child exits or execs
-
Faster but dangerous if misused
clone() – Generalized Creation Mechanism
clone() is the most powerful creation system call:
-
Used internally by
fork() -
Used to implement threads
The behavior depends on flags specifying what is shared:
-
Memory
-
Files
-
Signals
-
Filesystem context
7. Process Termination
Normal Termination
-
Process calls
exit() -
Kernel releases resources
-
Exit code is stored
Zombie State
-
Process becomes a zombie
-
task_structremains to hold exit status -
Parent must call
wait()
Orphan Processes
-
If parent exits first
-
Child is adopted by
init(PID 1)
8. Parent–Child Relationships
Linux maintains:
-
Parent pointer
-
Children list
-
Sibling links
This forms a process hierarchy rooted at init.
9. Context Switching
A context switch occurs when:
-
One process stops running
-
Another process starts
During a context switch:
-
CPU registers are saved
-
Kernel stack is switched
-
Scheduler selects next task
Linux minimizes overhead by:
-
Using per-task kernel stacks
-
Sharing address spaces for threads
10. Kernel Threads
What Are Kernel Threads?
-
Run entirely in kernel space
-
No user address space
-
Used for background tasks
Examples:
-
Memory management
-
I/O handling
-
Scheduler helpers
Kernel threads are created using:
11. Scheduling and Process Management
Although scheduling is covered in later chapters, Chapter 3 establishes:
-
Linux schedules tasks, not processes
-
Each task has scheduling attributes
-
Supports SMP systems efficiently
12. Signals and Process Control
Signals are used to:
-
Control process execution
-
Notify events
-
Handle errors
Examples:
-
SIGKILL -
SIGSTOP -
SIGCHLD
Signals are handled via structures referenced from task_struct.
13. Why Linux Process Management Is Efficient
Robert Love emphasizes:
-
Unified process/thread model
-
Minimal abstractions
-
Strong SMP support
-
Clean separation of concerns
Linux avoids:
-
Heavyweight process duplication
-
Separate thread schedulers
14. Summary Table
| Aspect | Linux Design |
|---|---|
| Process abstraction | task_struct |
| Thread distinction | None (resource sharing) |
| Creation mechanisms | fork(), vfork(), clone() |
| Scheduling unit | Task |
| Thread model | One-to-one |
| Parent–child model | Hierarchical |
15. Key Takeaways
-
Linux uses a single task abstraction
-
Threads are processes that share resources
-
Process creation is optimized via COW
-
clone()underpins both processes and threads -
Efficient process management is central to Linux’s performance
Comments
Post a Comment