Lecture 6: Threads Flashcards
Thread
“Lightweight Process”
Abstraction only of CPU (not memory) : basic unit of CPU utilization
Includes: thread ID, PC, registers, stack
shares with other threads of same process: code, data, files
Steps for Creating new processes:
- Allocate Memory
- Copy Text code
- Copy Data
- Copy Stack
- Copy File Descriptors
- Copy Registers
- Copy PC
Responsiveness:
interactive program (e.g., multithreaded web browser) can keep running even if one thread (e.g., loading images) blocks
Resource Sharing
Code, address space sharing comes for free with threads. For processes, code copying required for former, IPC for latter
Economy
Slightly cheaper to context switch
Utilization of Multiprocessors->Scalability
Better parallelism: threads of same process can run spread across multiple cores at the same time.
Kernel Thread
Managed by OS with multi threaded architecture (Win 7, OS X, Linux, …) similar to a process in the single-threaded system.
- the kernel thread is the unit of execution that is scheduled by the kernel on the CPU
User Thread
Implemented with threads library (e.g., Java threads) relationship to kernel threads: 1. Many-to-one 2. One-to-one 3. Many-to-many
Many-to-One
Many user-level threads mapped to single kernel thread
Many-to-one Pros and Cons
+ managed by the thread library -> efficient
- one thread blocking causes all to block
(process blocks)
- multiple threads may not run in parallel on
multicore systems -> only one may be in
kernel at a time
- few systems currently use this model
One-to-One
Each user-level thread mapped to a kernel thread
One-to-One Pros and Cons
+ runs on multi processors
+ more concurrency than many-to-one (no blocking of threads in the same
process)
-number of threads per process sometimes restricted due to overhead of
creating a kernel thread for each user thread
Many-to-Many
Allows Many User Level Threads To Be Mapped To Many Kernel Threads
Many-to-Many Pros and Cons
+allows the operating system to create a
sufficient number of kernel threads
+can run in a multiprocessor
+ no process blocking
Joining Threads
Parent calls child.join()
• Causes parent to wait until child.run() completes
• If child is interrupted (run() doesn’t complete), will throw
exception that parent should catch