OS Notes
Linux process management — process creation (fork/exec), process states, signals, job control, /proc filesystem, process monitoring tools, and managing daemons with systemd.
Introduction
Process management is where operating system theory becomes tangible practice. In Linux, processes are the fundamental unit of execution — every command you run, every application you start, and every system service creates one or more processes. Understanding how to create, monitor, control, and terminate processes is essential for system administration, debugging, and writing robust software.
Linux provides rich tools for process management, from the kernel-level system calls (fork, exec, wait) to user-space utilities (ps, top, kill) and modern service management with systemd.
Process Creation: fork() and exec()
Linux creates processes using a two-step mechanism inherited from Unix:
fork() — Create a Child Process
fork() creates a new process by duplicating the calling process. The child is nearly identical to the parent — same code, same data, same open files.
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid = fork();
if (pid < 0) {
perror("fork failed");
return 1;
} else if (pid == 0) {
// Child process
printf("Child: My PID is %d, parent is %d\n", getpid(), getppid());
} else {
// Parent process
printf("Parent: Created child with PID %d\n", pid);
wait(NULL); // Wait for child to finish
}
return 0;
}Modern Linux uses Copy-on-Write (COW): after fork, parent and child share the same physical memory pages. Pages are only copied when one process tries to modify them — making fork very fast even for processes with large memory.
exec() — Replace Process Image
exec() replaces the current process's code and data with a new program. The PID remains the same — it is the same process running different code.
// Child process runs "ls -la" command
if (pid == 0) {
execlp("ls", "ls", "-la", "/home", NULL);
// If exec returns, it failed
perror("exec failed");
exit(1);
}fork + exec is how every command is executed:
- Shell calls
fork()→ creates child - Child calls
exec()→ replaces itself with the command - Parent calls
wait()→ blocks until child finishes - Child terminates → parent resumes
Process States in Linux
# View process states
ps aux
# STAT column shows state:
# R Running or runnable (on CPU or in run queue)
# S Sleeping (waiting for event — I/O, timer, signal)
# D Uninterruptible sleep (usually disk I/O — cannot be killed!)
# T Stopped (by signal or debugger)
# Z Zombie (terminated but parent hasn't called wait())Why 'D' state matters: A process in uninterruptible sleep cannot be killed even with kill -9. It is waiting for a hardware operation (typically disk I/O) to complete. If many processes are in D state, it usually indicates a storage problem (failed disk, NFS mount timeout).
Signals
Signals are software interrupts delivered to processes. They are Linux's mechanism for asynchronous process control.
# Common signals
kill -l # List all signals
# Key signals:
# SIGTERM (15) — Polite request to terminate (default for kill)
# SIGKILL (9) — Force kill (cannot be caught or ignored)
# SIGSTOP (19) — Pause process (cannot be caught)
# SIGCONT (18) — Resume stopped process
# SIGHUP (1) — Hangup (daemons: reload configuration)
# SIGINT (2) — Interrupt (Ctrl+C)
# SIGTSTP (20) — Terminal stop (Ctrl+Z)
# SIGSEGV (11) — Segmentation fault (invalid memory access)
# Send signals
kill PID # Send SIGTERM
kill -9 PID # Send SIGKILL (last resort)
kill -HUP PID # Send SIGHUP (daemon reload)
killall process_name # Kill all processes by name
pkill -f "pattern" # Kill processes matching patternSignal Handling in Code
#include <signal.h>
void handler(int signum) {
printf("Caught signal %d, cleaning up...\n", signum);
// Cleanup code here
exit(0);
}
int main() {
signal(SIGTERM, handler); // Register custom handler
signal(SIGINT, handler); // Handle Ctrl+C gracefully
// SIGKILL and SIGSTOP cannot be caught
while (1) {
// Do work...
sleep(1);
}
}Job Control
The shell provides job control for managing multiple processes:
The /proc Filesystem
Every process has a directory under /proc/[PID]/ containing runtime information:
# Process information for PID 1234
ls /proc/1234/
# Key files:
cat /proc/1234/status # Process status (name, state, memory, threads)
cat /proc/1234/cmdline # Full command line
cat /proc/1234/environ # Environment variables
ls -la /proc/1234/fd/ # Open file descriptors
cat /proc/1234/maps # Memory mappings (virtual memory layout)
cat /proc/1234/io # I/O statistics (bytes read/written)
# System-wide information
cat /proc/cpuinfo # CPU details
cat /proc/meminfo # Memory statistics
cat /proc/loadavg # System load average
cat /proc/uptime # System uptimeProcess Monitoring Tools
# ps — snapshot of current processes
ps aux # All processes, detailed
ps -ef --forest # Process tree (parent-child hierarchy)
ps -eo pid,ppid,%cpu,%mem,cmd --sort=-%cpu | head # Custom format, sorted
# top — real-time monitoring
top # Press 'M' for memory sort, 'P' for CPU sort
# Key top columns: PID, USER, %CPU, %MEM, COMMAND, STATE
# htop — better interactive monitor (install: apt install htop)
htop # Color-coded, scrollable, mouse support
# Watch specific process
pidstat -p PID 1 # CPU/memory stats every 1 second
# Track system calls of a process
strace -p PID # See what system calls a process makes
strace -c command # Count system calls made by commandSystemd — Service Management
Modern Linux uses systemd to manage system services (daemons):
# Service control
systemctl start nginx # Start service
systemctl stop nginx # Stop service
systemctl restart nginx # Restart
systemctl reload nginx # Reload config without restart
systemctl status nginx # View service status
# Enable/disable at boot
systemctl enable nginx # Start automatically at boot
systemctl disable nginx # Do not start at boot
# View all services
systemctl list-units --type=service
systemctl list-units --type=service --state=running
# View service logs
journalctl -u nginx # Logs for specific service
journalctl -u nginx --since "1 hour ago"
journalctl -f # Follow all logs in real-timeCreating a Systemd Service
systemctl daemon-reload # Reload after creating/editing service files
systemctl start myapp
systemctl enable myappKey Takeaways
- Linux creates processes via fork (copy) + exec (replace) — understanding this explains shells, pipes, and daemons
- Process states (R, S, D, T, Z) reveal what a process is doing and why it might be stuck
- Signals provide asynchronous process control — SIGTERM for polite, SIGKILL for force
- /proc filesystem exposes live kernel information about every process
- systemd manages service lifecycle (start, stop, restart, enable at boot)
- Monitoring tools (ps, top, htop, strace) are essential for debugging and performance analysis
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Process Management in Linux - Process Control.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Operating Systems topic.
Search Terms
operating-systems, operating systems, operating, systems, linux, and, unix, process
Related Operating Systems Topics