Date: September 2, 2024
Topic: Threads vs Processes
Recall
As modern CPUs have multi-cores, a process can be multi-threaded and thus require multiple execution contexts (multiple PCBs)
Notes
Threads and Concurrency
To take advantage of multi-CPU systems, a process needs to have multiple execution contexts
- An execution within a single process is a thread

Threads
A thread is like a worker in a toy shop:
- Is an active entity executing unit of a process
- Works simultaneously with others as many threads execute at the same time (concurrency)
- Requires coordination of assets like sharing of I/O devices, CPUs, memory
For multi-threaded processes:
- Some information like code and data is shared
- Others like the program counter and stack pointer are not
Process vs Thread

Single threaded process (left) vs multi-threaded processes (right)
Process:
- A single threaded process is represented by its address space
- The address space contains all of the virtual to physical address mappings for the process, its code, its data
- Process also represented by its execution context that contains information about the values of the registers, stack pointer, program counter etc
- The OS represents all this information in a Process Control Block (PCB)
Threads:
- Represent multiple, independent execution contexts (multiple threads running same process)
- Part of the same virtual address space (shares all virtual to physical address mappings)
- They share all the code, data and files
- However they may differ as:
- May potentially execute different instructions
- Access different portions of that address space
- Operate on different portions of the input
- Differ in other ways
- Hence each thread needs a different program counter, stack pointer, stack, thread-specific registers
- For each thread, we need to have separate data structures to represent this per-thread information
- The PCB will contain:
- Info shared among all threads (virtual address space, code etc)
- Per-thread execution context (program counter, stack pointer etc)
<aside>
📌 SUMMARY: Threads allow for multiple processes to be run concurrently, sharing some aspects like code while having different aspects like program counters, stack pointers etc
</aside>
Date: September 2, 2024
Topic: Multithreading
Recall
Multithreading allows for:
- Parallelization
- Specialization
- More efficient than multi-processing
Notes
Benefits of Multithreading
While threads may be executing the same code, they may not be executing the exact same instruction at a single point in time, thus needing different program counter, stack pointer, etc
- The input for each threaded process may be different
- Allows for parallelization (speeds things up)
- Allows for specialization (allows for hot cache → better performance)
- Multi-threading is more efficient than multi-processing (lower memory requirement and cheaper IPC)
<aside>
📌 SUMMARY: Multi-threading can be beneficial due to shared address spaces, leading to lower memory overhead. Also as data needed by the different threads is more likely to be in processor cache, allowing for hotter caches
</aside>