I. Basic Concepts of Processes and Threads
1. Process
A process is a single running instance of a program, and it is an important abstraction the operating system uses for resource management and isolation.
A program is a static collection of instructions and data, whereas a process is the dynamic entity formed when the program runs.
A process typically contains:
- Process Control Block (PCB): the core data structure the operating system needs in order to manage the process.
- Virtual address space: the range of memory addresses visible to the process.
- Code segment: the program instructions.
- Data segment: data such as global variables and static variables.
- Heap: the region for dynamic memory allocation.
- Stack: the execution context for function calls, local variables, and so on.
- Open files and I/O resources: file descriptors, sockets, and so on.
- At least one thread: responsible for actually executing instructions.
A process is a container for resource management and isolation, but it does not itself directly execute instructions. The actual execution is carried out by the threads within the process.
2. Thread
A thread is a flow of execution inside a process, and it is the basic unit of CPU scheduling.
A process may contain one or more threads. Each thread has its own independent execution context, but threads share most of the resources of the process to which they belong.
A thread typically has:
- A thread identifier (TID).
- A program counter (PC).
- CPU register state.
- An independent thread stack.
- Thread-local storage (TLS).
- Information such as scheduling state and priority.
When a thread executes, the CPU needs to save and restore the thread's registers, program counter, and other context so that it can switch between different threads.
3. Core Differences
| Comparison Dimension | Process | Thread |
|---|---|---|
| Basic role | Unit of resource management and isolation | Unit of execution and scheduling |
| Resource ownership | Has its own process resources | Shares process resources |
| Address space | Usually independent | Shared by threads in the same process |
| Stack space | Each thread within the process has its own | Independent for each thread |
| Register state | Carried by the thread's execution context | Independent for each thread |
| Creation overhead | Usually larger | Usually smaller |
| Switching overhead | Usually larger | Usually smaller |
| Communication method | IPC mechanisms | Shared memory, synchronization primitives |
| Impact of failure | Usually has a process isolation boundary | May affect the entire process |
| Concurrent execution | Realized through threads within the process | Participates directly in scheduling |
Note that the cost of process switching and thread switching is not a fixed value. Factors such as whether the address space is switched, whether a TLB flush occurs, cache hit behavior, and the kernel implementation all affect the actual overhead.
II. Internal Structure of a Process
1. Process Control Block (PCB)
The PCB (Process Control Block) is the data structure the operating system uses to describe and manage a process.
Common information includes:
| Information Category | Main Contents |
|---|---|
| Identification information | PID, parent process PID, user ID |
| Processor state | Program counter, register state |
| Scheduling information | Process state, priority, scheduling parameters |
| Memory information | Page tables, address space description information |
| File information | Open files, file descriptors |
| Resource information | Allocated resources, resource usage statistics |
| Relationship information | Parent and child processes, process groups, sessions |
The PCB is a conceptual operating system management structure. In the Linux kernel, both processes and threads are managed through kernel structures such as task_struct; not all systems use exactly the same data structures.
2. Memory Layout of a Process
The virtual address space of a typical process can be abstracted as:
High address
┌────────────────────────────────────────┐
│ Kernel space │
├────────────────────────────────────────┤
│ User stack │
│ Stack │
│ ↓ │
├────────────────────────────────────────┤
│ │
│ Memory-mapped region │
│ Shared libraries, file mappings, etc. │
│ │
├────────────────────────────────────────┤
│ ↑ │
│ Heap │
├────────────────────────────────────────┤
│ BSS segment │
│ Uninitialized global variables │
├────────────────────────────────────────┤
│ Data segment │
│ Initialized global variables │
├────────────────────────────────────────┤
│ Text segment │
│ Program code │
└────────────────────────────────────────┘
Low address
This is a common illustration of a process's virtual address space; it does not mean that all architectures and operating systems use the same address layout.
It is important to distinguish:
- Virtual address space: the range of logical addresses that a process sees.
- Physical memory: the actual RAM pages.
- Page tables: responsible for mapping virtual addresses to physical addresses.
- Memory-mapped region: used to map shared libraries, files, anonymous memory, and so on.
The fact that a process has its own independent virtual address space does not mean that all of each process's physical memory is completely independent. Multiple processes can share physical pages through shared memory, shared library mappings, and other mechanisms.
3. Ownership of Process Resources
A process typically manages the following resources:
- Its virtual address space.
- Its file descriptor table.
- Its signal handling configuration.
- Its file system context, such as the working directory and root directory.
- Its user identity and permission information.
- Its process-level resource limits.
The specific rules for sharing and inheriting resources depend on the operating system and the mechanism used when creating the process.
III. Internal Structure of a Thread
1. Resources a Thread Owns Independently
Threads within the same process share most of the process's resources, but each thread must maintain its own execution state.
| Thread-private content | Purpose |
|---|---|
| Program counter | Records the next instruction to be executed |
| CPU registers | Hold the current execution state |
| Thread stack | Stores function call frames, local variables, etc. |
| Thread ID | Uniquely identifies the thread |
| Thread-local storage | Stores thread-private data |
| Scheduling state | Describes the thread's current running state |
A thread stack is essential for a thread to execute independently. If multiple threads shared a single call stack, they could not correctly maintain their own function call relationships and local execution state.
2. Resources Shared by Threads
Threads in the same process typically share:
- The program code.
- Global variables and static variables.
- Heap memory.
- Memory-mapped regions.
- The file descriptor table.
- Process-level resources and attributes.
For example:
#include <pthread.h>
#include <stdio.h>
int counter = 0;
void* worker(void* arg) {
counter++;
return NULL;
}
int main() {
pthread_t t1, t2;
pthread_create(&t1, NULL, worker, NULL);
pthread_create(&t2, NULL, worker, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
printf("%d\n", counter);
return 0;
}
Here, the two threads share the global variable counter.
However, this program has a data race: counter++ is not an atomic operation, and the two threads may read and modify the variable at the same time, causing lost updates.
A correct implementation requires a mutex or an atomic variable.
3. Resource Relationship Between Processes and Threads
flowchart TB
P["Process"]
P --> M["Shared address space"]
P --> F["Shared process resources such as file descriptors"]
P --> T1["Thread T1"]
P --> T2["Thread T2"]
P --> T3["Thread T3"]
T1 --> S1["Private stack"]
T1 --> R1["Private registers and program counter"]
T2 --> S2["Private stack"]
T2 --> R2["Private registers and program counter"]
T3 --> S3["Private stack"]
T3 --> R3["Private registers and program counter"]
style P fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style M fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style F fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style T1 fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style T2 fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style T3 fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style S1 fill:#FFF5DE,stroke:#E8D29A,color:#334155
style S2 fill:#FFF5DE,stroke:#E8D29A,color:#334155
style S3 fill:#FFF5DE,stroke:#E8D29A,color:#334155
style R1 fill:#E8EDF2,stroke:#B8C3CE,color:#334155
style R2 fill:#E8EDF2,stroke:#B8C3CE,color:#334155
style R3 fill:#E8EDF2,stroke:#B8C3CE,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
This is also the most important structural feature of multithreaded programming: shared resources improve collaboration efficiency, but at the same time introduce synchronization and data consistency issues.
IV. Process Lifecycle and State Transitions
1. Basic Process States
Operating systems usually use a state machine to describe the process lifecycle.
Common states include:
| State | Meaning |
|---|---|
| New | The process is being created |
| Ready | Meets the conditions to run and is waiting for the CPU |
| Running | Executing on the CPU |
| Blocked | Waiting for I/O or an event |
| Terminated | Execution has finished or the process has been terminated |
Some operating systems also have states such as suspended-ready and suspended-blocked.
2. State Transition Mechanism
flowchart TD
A["New"] --> B["Ready"]
B --> C["Running"]
C --> D["Blocked"]
D --> B
C --> E["Terminated"]
C --> B
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
style E fill:#E8EDF2,stroke:#B8C3CE,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
Main reasons for transitions:
- New to Ready: The operating system completes process creation and initialization of necessary resources.
- Ready to Running: The scheduler selects a runnable thread in the process to execute.
- Running to Ready: The time slice expires, the thread is preempted, or it voluntarily yields the CPU.
- Running to Blocked: Waiting for disk I/O, network I/O, a lock, or another event.
- Blocked to Ready: The awaited event completes.
- Running to Terminated: The program exits normally or is terminated.
Strictly speaking, the scheduling entity in modern operating systems is usually a thread, not an abstract process state. A multithreaded process may simultaneously contain threads in different states: running, ready, and blocked.
3. Difference Between Process States and Thread States
For a multithreaded process:
- The process as a whole may still exist.
- One thread may have already terminated.
- Another thread may be running.
- Other threads may be blocked due to lock contention.
Therefore, one cannot simply assume that a process has only one execution state at any given moment.
V. Process Creation and Termination
1. Process Creation
Taking Linux/Unix systems as an example, common process creation methods include fork() and exec().
fork(): Create a new child process.exec(): Replace the program image of the current process with a new program image.wait()/waitpid(): The parent process waits for a state change in the child process and reaps its exit status.
Typical execution flow:
flowchart TD
A["Parent process running"] --> B["Call fork()"]
B --> C["Create child process"]
C --> D["Parent process continues execution"]
C --> E["Child process continues execution"]
E --> F["Call exec()"]
F --> G["Load new program image"]
G --> H["Execute new program"]
H --> I["Child process exits"]
I --> J["Parent process calls wait() to reap status"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#FFF5DE,stroke:#E8D29A,color:#334155
style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style D fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style E fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style F fill:#FFF5DE,stroke:#E8D29A,color:#334155
style G fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style H fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style I fill:#E8EDF2,stroke:#B8C3CE,color:#334155
style J fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
2. Copy-on-Write (COW)
When fork() creates a child process, it usually does not immediately copy all of the parent process's physical memory.
Modern systems such as Linux typically use the Copy-on-Write (COW) mechanism:
- The parent and child processes initially share the same physical memory pages.
- The pages are marked as copy-on-write.
- When either process attempts to write to a shared page, a page fault is triggered.
- The kernel allocates a new physical page for the writer.
- The contents of the original page are copied to the new page.
- The corresponding page table mapping is updated, completing the write.
flowchart TD
A["fork() creates child process"] --> B["Parent and child processes share physical pages"]
B --> C["Pages marked as COW"]
C --> D["A process attempts to write"]
D --> E["Trigger a page fault"]
E --> F["Allocate a new physical page"]
F --> G["Copy the original page contents"]
G --> H["Update page table mappings"]
H --> I["Process modifies the page independently"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style C fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
style E fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style G fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style H fill:#E8EDF2,stroke:#B8C3CE,color:#334155
style I fill:#E5F4EC,stroke:#A3CBB5,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
COW reduces the memory-copying overhead when creating a process and is especially well suited to scenarios where exec() is called immediately after creation.
Note the following:
- COW does not mean there is no memory overhead at all.
- Creating a child process still requires setting up the corresponding kernel management structures, page tables, and other data.
- Writing to a shared page may trigger an actual copy.
- After
fork(), the parent and child processes have logically independent address spaces.
3. Zombie Processes and Orphan Processes
Zombie Process
The child process has already exited, but the parent process has not yet reaped its exit status through wait() or a related interface.
At this point, most of the child process's resources have been released, but the kernel still retains the necessary exit information and process identifier so that the parent process can query them.
Orphan Process
The parent process exits before the child process, while the child process is still running.
In Linux, orphan processes are usually reparented to an appropriate system-level adopter process or a configured child subreaper.
| Comparison | Zombie Process | Orphan Process |
|---|---|---|
| Whether the child process is running | Has exited | Still running |
| Main issue | Exit status not reaped | Original parent process has exited |
| Resource usage | Retains a small amount of kernel information | Normally occupies runtime resources |
| Handling method | Parent process reaps the exit status | Managed by a new parent process |
VI. Thread Lifecycle and Management
1. Basic Thread States
Common thread states include:
- New
- Runnable / Ready
- Running
- Blocked
- Waiting
- Timed Waiting
- Terminated
Different languages and runtimes do not define thread states in exactly the same way.
For example, Java's Thread.State contains NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, and TERMINATED. In Java, RUNNABLE includes both runnable and running states.
2. Thread State Transitions
flowchart TD
A["New"] --> B["Runnable"]
B --> C["Running"]
C --> D["Blocked"]
C --> E["Waiting"]
C --> F["Timed Waiting"]
D --> B
E --> B
F --> B
C --> G["Terminated"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
style E fill:#FFF5DE,stroke:#E8D29A,color:#334155
style F fill:#FFF5DE,stroke:#E8D29A,color:#334155
style G fill:#E8EDF2,stroke:#B8C3CE,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
3. Thread Creation and Destruction
Take POSIX threads as an example:
#include <pthread.h>
#include <stdio.h>
void* worker(void* arg) {
printf("Worker thread running\n");
return NULL;
}
int main() {
pthread_t tid;
pthread_create(&tid, NULL, worker, NULL);
pthread_join(tid, NULL);
printf("Main thread finished\n");
return 0;
}
Key interfaces:
| Interface | Purpose |
|---|---|
pthread_create() |
Creates a thread |
pthread_join() |
Waits for the specified thread to terminate and reclaims its joinable resources |
pthread_detach() |
Sets the thread to detached state |
pthread_exit() |
Terminates the current thread |
pthread_self() |
Gets the current thread ID |
Thread termination does not mean that the entire process terminates.
Only when all threads in the process have ended, or the process is explicitly terminated, or other such conditions are met, does the process end.
VII. Process and Thread Creation Costs
1. Process Creation Cost
Creating a process typically involves:
- Allocating a process management structure.
- Establishing a new virtual address space.
- Creating or copying page table-related structures.
- Handling inheritance of file descriptors and other resources.
- Initializing scheduling and security-related information.
With COW, it is usually not necessary to immediately copy all physical memory, but kernel management and address space initialization costs still exist.
2. Thread Creation Cost
Creating a thread typically requires:
- Allocating a thread control structure.
- Allocating a thread stack.
- Initializing registers and the execution context.
- Initializing thread-local storage.
- Adding the thread to the scheduling system.
Creating threads within the same process usually does not require creating a new complete address space, so the overhead is generally less than creating an independent process.
3. Threads Are Not Always More Efficient
Although threads are lightweight, a large number of threads incurs additional costs:
- Thread stacks consume memory.
- Scheduling and context-switching overhead.
- Lock contention and synchronization costs.
- CPU cache invalidation.
- Thread management and resource reclamation costs.
If the number of tasks far exceeds the number of CPU cores, blindly creating a large number of threads may reduce system throughput.
VIII. Inter-Process Communication (IPC)
Different processes have mutually isolated virtual address spaces, and one process cannot directly access another process's ordinary private memory.
Processes need to use IPC (Inter-Process Communication) mechanisms to exchange data and coordinate behavior.
1. Common IPC Mechanisms
| IPC Mechanism | Basic Principle | Typical Use |
|---|---|---|
| Pipe | The kernel maintains a byte-stream buffer | Communication between parent and child processes |
| Named Pipe (FIFO) | Establishes a communication channel via a file system name | Communication between unrelated processes |
| Message Queue | The kernel maintains message structures | Asynchronous message exchange |
| Shared Memory | Multiple processes map the same physical memory region | Large-volume, high-performance communication |
| Signal | Sends asynchronous event notifications | Event notification, process control |
| Socket | Exchanges data through communication endpoints | Local or network communication |
| Semaphore | Maintains a counter state for synchronization | Resource coordination |
2. Shared Memory
Shared memory is a relatively efficient IPC method.
Multiple processes map the same physical memory region into their respective virtual address spaces, thereby sharing data.
flowchart TB
A["Process A"] --> B["Process A virtual address space"]
C["Process B"] --> D["Process B virtual address space"]
B --> E["Shared physical memory region"]
D --> E
E --> F["Shared data"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style C fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style D fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style E fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style F fill:#FFF5DE,stroke:#E8D29A,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
Advantages of shared memory:
- Avoids frequent data copying.
- Suitable for transferring large blocks of data.
- Data access speed is usually relatively fast.
Main issues:
- Requires additional synchronization mechanisms.
- Must handle concurrent reads and writes.
- Requires managing the lifetime of the shared memory.
- Different processes may map the shared region to different virtual addresses.
Shared memory only solves the problem of data sharing; it does not automatically solve the problem of data consistency. It usually needs to be combined with inter-process mutexes, semaphores, or other synchronization primitives.
IX. Inter-Thread Communication and Synchronization
1. Inter-Thread Communication
Threads in the same process share an address space, so they can directly pass data through shared variables, heap objects, and similar means.
Common approaches:
- Shared variables.
- Shared objects.
- Message queues.
- Condition variables.
- Thread-safe queues.
- Atomic variables.
Sharing data between threads is very convenient, but it also easily leads to race conditions.
2. Thread Synchronization Mechanisms
| Synchronization Mechanism | Purpose | Typical Scenario |
|---|---|---|
| Mutex | Ensures mutually exclusive access to critical sections | Shared data protection |
| Read-Write Lock (RWLock) | Allows multiple readers and a single writer | Frequent reads, infrequent writes |
| Semaphore | Controls the number of concurrent accesses | Limiting resource concurrency |
| Condition Variable | Waits for a specific condition to become true | Producer-consumer |
| Spinlock | Acquires the lock by spinning in a loop | Extremely short critical sections |
| Atomic Operation | Provides indivisible operations | Counters, status flags |
3. Race Condition
A race condition means that the program's result depends on the specific interleaving order of multiple execution flows.
For example:
counter++;
It can usually be abstracted into three steps:
- Read
counter. - Perform the addition.
- Write the result back to
counter.
Two threads may interleave as follows:
sequenceDiagram
participant T1 as Thread T1
participant M as Shared variable counter
participant T2 as Thread T2
T1->>M: Read 0
T2->>M: Read 0
T1->>T1: Compute 0 + 1
T2->>T2: Compute 0 + 1
T1->>M: Write 1
T2->>M: Write 1
Note over M: The final result is 1, not 2
Solutions include: