CS330 - Operating Systems
Multithreading Models, Benefits, and Implementation
A thread is a basic unit of CPU utilization. It is also called a lightweight process. A thread comprises a thread ID, program counter, register set, and stack.
| Type | Description | Characteristics |
|---|---|---|
| Heavyweight Process | Traditional process with single thread | One thread of control |
| Lightweight Process | Another name for thread | Basic unit of CPU utilization |
| Multithreaded Process | Process with multiple threads | Multiple threads share process resources |
[Insert diagram showing single-threaded vs multithreaded process structure]
Threads within the same process share memory and resources, which makes communication between threads efficient but requires careful synchronization to avoid race conditions.
Allows a program to continue running even if part is blocked. Example: Web browser can allow user interaction in one thread while loading images in another.
Threads share memory and resources of the process they belong to. Easier than shared memory or message passing between processes.
Thread creation is faster than process creation. Context switching between threads has lower overhead. In Solaris, creating process is 30 times slower than thread.
Can utilize multiprocessor architectures. Multiple threads can run in parallel on different CPUs. Single-threaded process can run only on one CPU.
Q: List the four major benefits of multithreaded programming.
Answer:
Threads are interleaved over time
Threads run simultaneously on different cores
| Aspect | Concurrency | Parallelism |
|---|---|---|
| Definition | Multiple tasks make progress | Multiple tasks execute simultaneously |
| Hardware | Can work on single core | Requires multiple cores |
| Execution | Interleaved execution | Simultaneous execution |
Distributes subsets of the same data across multiple cores, same operation on each.
Example: Summing array elements - divide array into chunks, sum each chunk in parallel.
Distributing threads across cores, each thread performing unique operation.
Example: Web server - one thread handles requests, another processes data, another updates logs.
Threads managed by user-level threads library, not in the kernel. Scheduled by the thread library.
| Advantages | Disadvantages |
|---|---|
|
|
Examples: POSIX Pthreads, Java threads, Win32 threads
All thread management done by kernel itself. Kernel does creation, scheduling and management.
| Advantages | Disadvantages |
|---|---|
|
|
Examples: Windows, Linux, Mac OS X, iOS, Android
Q: One disadvantage of User-Level Threads (ULTs) compared to Kernel-Level Threads (KLTs) is:
Examples: Solaris Green Threads, GNU Portable Threads
Examples: Windows, Linux
Examples: Solaris prior to version 9, Windows with ThreadFiber
Similar to M:M, except it allows a user thread to be bound to kernel thread.
Examples: IRIX, HP-UX, Tru64 UNIX, Solaris 8 and earlier
A thread library provides programmer with API for creating and managing threads.
| Function | Description |
|---|---|
pthread_create() |
Create a new thread |
pthread_join() |
Wait for thread to terminate |
pthread_exit() |
Terminate calling thread |
pthread_attr_init() |
Initialize thread attributes |
pthread_self() |
Get calling thread's ID |
pthread_equal() |
Compare two thread IDs |
When a multithreaded process calls fork():
Different systems handle this differently. exec() typically replaces entire process.
Where should signal be delivered?
| Type | Description | Issues |
|---|---|---|
| Asynchronous | Terminate target thread immediately | Resources may not be freed, data may be inconsistent |
| Deferred | Target thread checks flag periodically | Safer but requires cooperation |
Allows each thread to have its own copy of data. Useful when you don't have control over thread creation process.
Create number of threads at startup and place them in pool where they await work.
Q1. Which is NOT shared by threads?
Q2. A process that has multiple threads of control can do more than one task at a time. This is called:
Q3. In the Many-to-One threading model, if one thread performs a blocking system call:
Q4. Thread creation is:
Q5. Consider this code segment. How many unique processes and threads are created?
Q6. In a multiprocessor system with many-to-many threading model, discuss performance when:
a) Number of kernel threads < number of processors
b) Number of kernel threads = number of processors
a) Some processors remain idle since scheduler maps only kernel threads to processors, not user threads.
b) All processors can be utilized simultaneously. However, if kernel thread blocks (page fault, system call), corresponding processor becomes idle.
Q7. What is the difference between concurrency and parallelism?
| Term | Definition |
|---|---|
| Thread | Basic unit of CPU utilization; lightweight process |
| Multithreading | Ability of OS to support multiple threads in single process |
| User Thread | Thread managed by user-level library without kernel support |
| Kernel Thread | Thread managed directly by operating system kernel |
| Thread Pool | Collection of pre-created threads awaiting work |
| TLS | Thread-Local Storage - per-thread data storage |
| pthread | POSIX thread library specification |
| Concurrency | Multiple tasks making progress (possibly interleaved) |
| Parallelism | Multiple tasks executing simultaneously |
| Race Condition | When outcome depends on timing of uncontrolled events |
[Insert diagram from Chapter_4.pdf page 6]
[Insert diagram showing user/kernel space separation]
[Insert server architecture diagram from slides]
[Insert many-to-one, one-to-one, many-to-many models]
Note: Please provide the actual diagrams from your Chapter_4.pdf slides to replace these placeholders.