Master Linux Process Management: From Fork to Scheduling and Memory
An in‑depth guide to Linux process management covering process concepts, lifecycle, threads, priority, context switching, interrupt handling, process states, memory layout, and the O(1) scheduler, illustrated with diagrams to help readers understand kernel behavior and performance implications.
1.1 Linux Process Management
Process management is one of the most important functions of an operating system. Efficient process management ensures that a program runs smoothly and efficiently.
Linux process management is similar to UNIX and includes scheduling, interrupt handling, signals, priorities, context switches, process states, and memory management.
1.1.1 What Is a Process?
A process is an instance of a program running on a processor, using any resources the Linux kernel can provide to complete its task.
All processes in Linux are managed by a task_struct structure, also called a process descriptor. It contains identifiers, attributes, and resources needed for the process.
1.1.2 Process Lifecycle
Each process has a lifecycle: creation, execution, termination, and cleanup. These stages repeat countless times during system operation, making lifecycle analysis crucial for performance.
When a process creates a new process, the parent calls the fork() system call, which creates a new process descriptor and assigns a new PID. The address space is not copied; the parent and child share the same address space.
The exec() system call loads a new program into the child’s address space. Because the address space is shared, writing a new program triggers a page‑fault, and the kernel allocates new physical pages for the child (copy‑on‑write).
When the program finishes, the child calls exit(), releasing most of its resources and sending a signal to the parent. The child then becomes a zombie process until the parent calls wait() to reap it.
1.1.3 Threads
A thread is an execution unit created by a process that runs in parallel with other threads of the same process, sharing memory, address space, and open files.
Threads are also called Light‑Weight Processes (LWP). Because they share resources, concurrent modification must be synchronized by the programmer using mutexes, locks, or serialization.
Creating a thread incurs less overhead than creating a process because resources are not duplicated. The kernel schedules threads similarly to processes.
Linux supports several thread libraries, including LinuxThreads (default before kernel 2.0), Native POSIX Thread Library (NPTL), and IBM’s Next Generation POSIX Thread (NGPT). The LD_ASSUME_KERNEL environment variable can select the desired library.
1.1.4 Process Priority and Nice Value
Process priority determines the order in which the CPU schedules processes. It consists of a dynamic component and a static component.
Users can influence static priority via the process’s nice value. Linux supports nice values from -20 (highest priority) to 19 (lowest), with a default of 0. Decreasing the nice value requires root privileges.
1.1.5 Context Switches
During execution, a process’s state (registers, cache) is saved as its context. To switch to another process, the kernel saves the current context and restores the next process’s context.
Excessive context switches are undesirable because they flush registers and caches, potentially causing performance problems.
1.1.6 Interrupt Handling
Interrupt handling has the highest priority. Hardware interrupts are generated by I/O devices; the kernel must respond quickly to maintain system stability.
Linux distinguishes hard interrupts (device‑generated) and soft interrupts (deferred tasks such as TCP/IP processing). In SMP systems, interrupts can be bound to specific CPUs to improve performance.
1.1.7 Process States
Each process can be in one of several states, indicating its current activity:
TASK_RUNNING : Running on the CPU or ready in the run queue.
TASK_STOPPED : Stopped by a signal (e.g., SIGSTOP) and waiting for SIGCONT.
TASK_INTERRUPTIBLE : Sleeping, waiting for a condition; can be awakened by a signal.
TASK_UNINTERRUPTIBLE : Sleeping without responding to signals (e.g., waiting for I/O).
TASK_ZOMBIE : Exited but not yet reaped by the parent.
1.1.8 Process Memory Segments
A process’s address space consists of several segments:
Text : Stores executable code.
Data : Includes initialized data, BSS (zero‑initialized data), and the heap (dynamic memory allocated via malloc()).
Stack : Stores local variables, function arguments, and return addresses; grows downward.
1.1.9 Linux CPU Scheduling
Linux uses an O(1) scheduler (introduced by Ingo Molnar) that selects a process in constant time, regardless of the number of processes.
The scheduler maintains two priority arrays: active and expired . When a process exhausts its time slice, it moves to the expired array; when the active array is empty, the two arrays are swapped.
This design scales well on systems with many processes and CPUs, supports NUMA architectures, and works with symmetric multithreading (e.g., Intel Hyper‑Threading).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
