Skip to content

EN_CS_Process_Thread

somaz edited this page Mar 30, 2026 · 1 revision

Q4 & Q5: Program, Process, and Thread

Question: Explain the concepts of program, process, and thread. Describe the memory structure differences between processes and threads, IPC methods, and how to handle concurrency issues in multithreading.


Key Terms

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 — mechanisms for processes to communicate
Context Switching CPU switching between running processes or threads
Race Condition Non-deterministic result when threads concurrently access shared data
Deadlock Two threads waiting on each other's lock indefinitely
Mutex Mutual exclusion lock — only one thread enters the critical section at a time

Program → Process → Thread

Program (static file on disk)
    ↓ execute
Process (independent memory space, assigned PID)
    ↓ contains
Thread (shared memory within process, assigned TID)

In summary: A program is code; a process is what happens when the code runs.


Memory Structure

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

Process vs Thread Comparison

Aspect Process Thread
Memory Independent (fully isolated) Shared (only Stack is independent)
Creation Cost High (fork) Low (pthread_create)
Communication Requires IPC Direct via shared memory
Independence Fully independent Crash affects entire process
Context Switch Slow Fast
Keyword Isolation Concurrency

IPC (Inter-Process Communication)

Method Characteristics Speed Use Case
Pipe Unidirectional, parent-child Fast Simple data transfer
Message Queue Async message-based Medium Async inter-process messaging
Shared Memory Direct memory sharing Very fast Large data sharing
Socket Network-capable Slow Distributed systems

Multithreading Concurrency Issues

Race Condition

Multiple threads access a shared variable simultaneously → result depends on execution order.

Solution: Mutex (Lock) Restrict critical section entry to one thread at a time.

Deadlock

Thread A holds Lock1 and waits for Lock2; Thread B holds Lock2 and waits for Lock1 → infinite wait.

Solution: Lock Ordering Enforce all threads to acquire locks in the same order.

Concurrency vs Parallelism

Concept Description
Concurrency Multiple tasks interleaved (possible on 1 CPU)
Parallelism Multiple tasks truly simultaneous (requires multiple CPUs)

Reference

Clone this wiki locally