Process Management
The process
- Linux does not differentiate between threads and process.
- Processes provide two virtualizations, a virtualized processor and virtual memory.
- Execution Workflow:
- fork(): creates a new process by duplicating an existing one
- exec(): creates a new address space and loads a new program into it
- exit(): terminates the process and frees all its resources. When a process exits, it is placed into a special zombie state until the parent calls wait() or waitpid().
- wait4(): enables a process to wait for the termination of a specific process
Allocating the Process Descriptor
- The kernel stores the list of processes in a circular doubly linked list called the task list. Each element in the task list is a process descriptor of the type
struct task_struct. - The process descriptor (
struct task_struct) contains all the information about a specific process.
|
|
Manipulating the Current Process State
set_task_state(task, state)sets a process’s state.- On single-CPU systems, it’s just task->state = state.
- On multi-CPU (SMP) systems, it also includes a memory barrier to ensure other CPUs see previous memory writes before the state change.
The Process Family Tree
- All processes are descendants of the init process, whose PID is one.
- Every process on the system has exactly one parent. Likewise, every process has zero or more children.
Copy-on-write
-
Copy-on-write (or COW) is a technique to delay or altogether prevent copying of the data.
-
The duplication of resources occurs only when they are written.
-
The only overhead incurred by fork() is the duplication of the parent’s page tables and the creation of a unique process descriptor for the child.
vfork()
-
The vfork() system call has the same effect as fork(), except that the page table entries of the parent process are not copied.
-
vfork() temporarily allows the child to share the parent’s address space, but the parent’s address space is preserved intact because the parent is suspended and the child either calls exec() (creating a new address space) or _exit().
Kernel Threads
-
A kernel-level process that runs entirely in kernel mode.
-
No user address space (
mm_struct = NULL). -
Used for internal OS tasks like scheduling, memory management, and background work.
Process Termination
-
do_exit()kills the process and turns it into a zombie for the parent to collect -
release_task()is the final step that frees the task_struct and completely removes the process from the kernel.
Reference
- Love, Robert. Linux Kernel Development. 3rd ed., Addison-Wesley Professional, 2010.