Repository navigation
EN_CS_Process_Thread
somaz edited this page Sep 22, 2026
·
5 revisions
Question: Explain the concepts of program, process, and thread, and describe the difference in memory structure between processes and threads. Also explain IPC methods and how to solve concurrency problems in multithreading.
| Term | Description |
|---|---|
| Program | Static executable file stored on disk |
| Process | Running instance of a program (independent memory space) |
| Thread | Lightweight execution unit within a process (shared memory) |
| IPC | Inter-Process Communication — mechanism for communication between processes |
| Context Switching | The CPU switching the running process/thread |
| Race Condition | State where multiple threads access a shared resource concurrently and the result is non-deterministic |
| Deadlock | State where two or more threads wait on each other's locks indefinitely |
| Mutex | Mutual exclusion lock — lets only one thread enter the Critical Section at a time |
Program (static file)
↓ execute
Process (independent memory space, assigned PID)
↓ contains
Thread (shared memory, assigned TID)
In summary: a program is code; a process is what happens when that code runs.
Processes (independent): Threads (shared):
┌─────────────┐ ┌─────────────┐ ┌─────────────────────────────┐
│ Stack (A) │ │ Stack (B) │ │ Stack T1 │ Stack T2 │ ... │ ← independent
├─────────────┤ ├─────────────┤ ├─────────────────────────────┤
│ Heap (A) │ │ Heap (B) │ │ Shared Heap │ ← shared
├─────────────┤ ├─────────────┤ ├─────────────────────────────┤
│ Data (A) │ │ Data (B) │ │ Shared Data Segment │ ← shared
├─────────────┤ ├─────────────┤ ├─────────────────────────────┤
│ Code (A) │ │ Code (B) │ │ Shared Code Segment │ ← shared
└─────────────┘ └─────────────┘ └─────────────────────────────┘
Process A Process B Single Process
| Aspect | Process | Thread |
|---|---|---|
| Memory | Independent (fully separated) | Shared (only Stack is independent) |
| Creation Cost | High (fork) | Low (pthread_create) |
| Communication | Requires IPC | Direct communication via shared memory |
| Independence | Fully independent | Dependent (a crash terminates the whole process) |
| Context Switching | Slow | Fast |
| Keyword | Isolation | Concurrency |
| Method | Characteristics | Speed | Use Case |
|---|---|---|---|
| Pipe | Unidirectional, between parent and child | Fast | Simple data transfer |
| Message Queue | Asynchronous, message-based | Medium | Asynchronous messages between processes |
| Shared Memory | Direct memory sharing | Very fast | Sharing large data |
| Socket | Network communication possible | Slow | Communication between distributed systems |
Multiple threads access a shared variable concurrently → the result varies with execution order.
Solution: Mutex (Lock) Restrict Critical Section entry to one thread at a time.
Thread A holds Lock1 while waiting for Lock2, and Thread B holds Lock2 while waiting for Lock1.
Solution: Lock Ordering Force all threads to acquire locks in the same order.
| Concept | Description |
|---|---|
| Concurrency | Processes multiple tasks by alternating between them (possible even on 1 CPU) |
| Parallelism | Processes multiple tasks truly at the same time (requires multiple CPUs) |