Linux Process Management: Concepts, Commands, and Programming Interfaces
Today we move on to the next phase of learning: processes and threads. Today's focus is on Linux process concepts, management commands, and related system function interfaces.
1. Core Concepts
- Program: A file stored in external memory containing a set of instructions and data.
- Process: The dynamic execution lifecycle of a program, including creation, scheduling, and termination.
2. Linux Process Management Commands
top: Dynamically view all system processes, sorted by CPU usage. Key process attributes to note:
pid: Unique process identifier (always greater than 0)
priority: In Linux, scheduling priority is inversely related to the nice value (range: -20 to 19; -20 = highest priority, 19 = lowest).
Process States:
R: Running / Ready
S: Sleeping (interruptible wait)
D: Uninterruptible sleep
T: Stopped
Z: Zombie
X: Terminated
nice: Run a process with a specified nice value (priority). Example:nice -n <priority_value> <process_command>renice: Adjust the nice value of a running process.kill: Terminate a process by PID.killall: Terminate all processes matching a given name.ps -f: View a snapshot of all processes at the current time.pstree: Display hierarchical parent-child process relationships.ps -aux: View detailed snapshot of all system processes../a.out &: Execute thea.outprogram in the background.jobs: List all background jobs in the current terminal.fg: Bring a background job to the foreground for execution.
3. Process Creation
3.1 Core Concepts
When a process runs, the OS allocates a 0-4GB virtual memory space divided into three main segments:
- Text Segment: Stores program instructions and read-only data.
- Data Segment: Stores global and static variables, split into:
- String constant area
- BSS segment (uninitialized global/static variables)
- Initialized global/static variables
- System Data Segment: Includes heap (dynamic memory allocation) and stack (function calls, local variables).
4. Virtual vs Physical Memory Mapping
- All processes share the same 0-4GB virtual address space range.
- Each process has an independent physical memory space allocated by the OS.
- The Memory Management Unit (MMU) handles translation between virtual and physical addresses, loading physical data into the process's virtual space during execution.
5. Process Scheduling
Process scheduling determines which process gets CPU time. Common algorithms:
- First-Come, First-Served (FCFS)
- Priority-based scheduling
- Round-Robin scheduling (most widely used)
- Multilevel Feedback Queue scheduling
- Load-balanced scheduling
Note: Processes appear to run in parallel macroscopically, but execute serially on a single CPU core microscopically.
6. Process-related System Functions
6.1 Process Creation with fork()
Prototype: pid_t fork(void);
Function: Creates a child process. The original process becomes the parent, and the new process is the child.
Return Values:
- 0 returned to the child process on success
- Child's PID returned to the parent process on success
- -1 returned on failure
Key Notes: The child copies the parent's text, data, and system segments, and starts execution immediately after the
fork()call line.
6.2 PID Retrieval Functions
// Get current process ID
pid_t getpid(void);
/*
Function:
Returns the PID of the calling process.
*/
// Get parent process ID
pid_t getppid(void);
/*
Function:
Returns the PID of the calling process's parent.
*/
6.3 Example: Create Two Child Processes
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main() {
pid_t child1, child2;
// Create first child process
child1 = fork();
if (child1 == -1) {
perror("Failed to create first child");
exit(EXIT_FAILURE);
} else if (child1 == 0) {
// First child execution path
printf("First Child - PID: %d, Parent PID: %d\n", getpid(), getppid());
} else {
// Parent creates second child
child2 = fork();
if (child2 == -1) {
perror("Failed to create second child");
exit(EXIT_FAILURE);
} else if (child2 == 0) {
// Second child execution path
printf("Second Child - PID: %d, Parent PID: %d\n", getpid(), getppid());
} else {
// Parent process output
printf("Parent Process - PID: %d, Child 1 PID: %d, Child 2 PID: %d\n",
getpid(), child1, child2);
}
}
// Keep processes running to view status
while (1) {
sleep(1); // Reduce CPU usage
}
return 0;
}
6.4 Process Termination Functions
exit(int status): Terminates the process and flushes all buffered data before exiting._exit(int status): Terminates the process immediately without flushing buffers.
7. Process Termination and Zombie Processes
- Zombie Process: A process that has finished execution but whose resources (like PID) are not reclaimed by its parenet.
- Prevention: Ensure the parent reclaims child resources using functions like
wait(), or let the parent exit first (init process will adopt and clean up the child). wait()Function: Reclaims terminated child resources, blocks the parent until a child exits, enabling process synchronization.