Process Concepts, Principles, and Management in Operating Systems
This article explains the definition, characteristics, and structure of processes, describes process creation, termination, and state transitions, outlines various scheduling algorithms, and introduces inter‑process communication mechanisms such as pipes, sockets, shared memory, and message queues, with example C code for Linux.
1. Concept
A process is the basic unit of resource allocation and execution in a computer system, representing a running instance of a program.
From a theoretical view, it abstracts the execution of a program; from an implementation view, it is a data structure that captures the dynamic behavior of the system.
What is a process?
Narrow definition: a process is the execution of a program segment.
Broad definition: a process is an independent execution activity of a program on a data set, serving as the fundamental unit of dynamic execution in traditional operating systems.
Process features:
Dynamic: created and destroyed during execution.
Concurrent: can run alongside other processes.
Independent: a distinct resource‑allocation and scheduling unit.
Asynchronous: may be blocked waiting for I/O or synchronization.
Structured: consists of program code, data, and a Process Control Block (PCB).
Multiple processes can share the same program but operate on different data sets.
Linux process structure
Linux processes consist of three parts: code segment, data segment, and stack segment, together forming the PCB (Process Control Block). The PCB uniquely identifies a process; the kernel stores PCBs in an array of task_struct structures defined in include/linux/sched.h .
2. Process Principles
2.1 Process
(1) Process Overview
A process is a program in execution; after loading into memory, the program becomes a process (process = program + execution).
(2) Process Model
Each process occupies its own memory region, has its own program counter, and progresses forward in time. Processes may never terminate (e.g., system daemons).
(3) Benefits of Multiprogramming
Multiprogramming improves CPU utilization and system throughput, and reduces user‑perceived response time.
(4) Process Creation and Termination
Creation events: system boot, explicit creation calls, user requests. Termination events: normal exit, error exit, being killed by another process, or forced termination.
(5) Process Hierarchy
Parent processes can create child processes, forming a process tree. Unix groups all processes in a tree into a process group; Windows treats all processes as peers.
(6) Process States
Typical states: Ready (resources allocated, waiting for CPU), Running (occupying CPU), Blocked (waiting for I/O or synchronization). State transitions are limited to those that the scheduler can act upon.
(7) Process and Address Space
Processes share physical memory efficiently and safely through mechanisms such as paging and virtual memory.
2.2 Process Management
Process Control Block (PCB)
The PCB stores registers, program counter, status flags, stack pointer, priority, PID, signals, creation time, CPU usage, and handles. Implementations use structures, linked lists, or arrays.
Process Creation Steps
Allocate a PCB.
Initialize registers.
Initialize page tables.
Load program code from disk into memory.
Set processor mode to user.
Jump to the program’s entry point.
Unix uses fork() to duplicate a process and exec() to replace its address space; Windows uses CreateProcess to create a new process with a fresh address space.
2.3 Process Scheduling
Scheduling decides which ready process obtains the CPU. Goals include minimizing average response time, maximizing throughput, keeping the system busy, and providing fairness.
Typical algorithms:
First‑Come‑First‑Serve (FCFS): simple queue, suffers from long‑job delay.
Round‑Robin: time‑slice rotation, balances response time and overhead.
Shortest‑Job‑First (SJF) / Shortest‑Remaining‑Time‑First (STCF): favors short jobs, may cause starvation.
Priority scheduling: higher‑priority processes run first; may need dynamic priority adjustment.
Multilevel queue: groups processes by class, each class uses its own algorithm (often RR).
Lottery scheduling, Fair‑Share scheduling, Guaranteed scheduling, etc.
Real‑time scheduling algorithms include Earliest Deadline First (EDF) and Rate‑Monotonic Scheduling (RMS).
2.4 Inter‑Process Communication (IPC)
IPC mechanisms enable processes to exchange data:
Pipes : unidirectional byte streams; created with pipe() or shell ‘|’.
Named pipes (FIFOs) : appear in the filesystem, usable by unrelated processes.
Sockets : support local (UNIX domain) and network communication; can be stream, datagram, sequential packet, or raw.
Signals : kernel objects used for asynchronous notifications.
Semaphores : integer counters for synchronization.
Shared memory : multiple processes map the same physical memory region for fast data exchange.
Message queues : kernel‑managed queues allowing many‑to‑many communication.
Each mechanism has trade‑offs in setup cost, flexibility, and security.
2.5 Example Code
Multi‑process example (main.c)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, const char *argv[])
{
int i;
pid_t pid1 = 0, pid2 = 0, pid = 0;
int *status1, *status2;
int k = 0;
for (i = 0; i < 2; i++) {
if (0 == i) pid = pid1 = fork();
else pid = pid2 = fork();
if (pid < 0) {
perror("pid fork");
exit(-1);
} else if (pid == 0) { // child
break;
} else { // parent
waitpid(pid1, status1, 0); // blocking wait
do { // non‑blocking wait
k = waitpid(pid2, status2, WNOHANG);
if (k == 0) {
printf("process2 still running\n");
sleep(1);
}
} while (k == 0);
}
}
if (0 == i) { // child 1
if (-1 == execl("./process1", NULL)) {
perror("process1 exec");
exit(-1);
}
} else if (1 == i) { // child 2
if (-1 == execl("./process2", NULL)) {
perror("process2 exec");
exit(-1);
}
}
printf("parent process exit!!!\n");
exit(0);
}process1.c (executes ls -l )
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argc, const char *argv[])
{
if (-1 == execlp("ls", "ls", "-l", NULL)) {
perror("process1");
exit(-1);
}
exit(0);
}process2.c (sleeps 5 seconds then exits with error)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argc, const char *argv[])
{
sleep(5);
exit(-1);
}Daemon process example
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main() {
pid_t pid = fork();
if (pid < 0) { perror("Fork failed"); exit(1); }
else if (pid > 0) { exit(0); } // parent exits
setsid();
chdir("/");
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
// daemon main logic here
return 0;
}Deepin Linux
Research areas: Windows & Linux platforms, C/C++ backend development, embedded systems and Linux kernel, etc.
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.