Processes 3
Overview
1 Threads vs. processes
2 Different thread implementations
3 POSIX Threads (PThreads)
Threads
Threads from an OS Perspective[观点]
- A process consists of two fundamental units
- Resources: all related resources are grouped together
- A logical address space containing the process image (program, data, heap, stack)
- Files, I/O devices, I/O channels, . . .
- Execution trace[执行追踪], i.e., an entity that gets executed
- Resources: all related resources are grouped together
- A process can share its resources between multiple execution traces, i.e., multiple threads running in the same resource environment
- Every thread has its own execution context (e.g. program counter, stack, registers)
- All threads have access to the process’ shared resources
- E.g. files, one thread opens a file, all threads of the same process can access the file
- Global variables, memory, etc. (⇒ synchronisation!)
- Some CPUs (hyperthreaded ones) have direct hardware support for multi-threading
- Similar to processes, threads have:
- States and transitions (new, running, blocked, ready, terminated)
- A thread control block
Threads create/terminate/switch with less overhead (address space remains the same for threads of the same process)
- Inter-thread communication is easier/faster than inter-process communication (threads share memory by default)
- No protection boundaries[边界] are required in the address space (threads are cooperating, belong to the same user, and have a common goal)
- Synchronisation has to be considered carefully!
Why Use Threads
- Multiple related activities apply to the same resources, these resources should be accessible/shared
- Processes will often contain multiple blocking tasks
- I/O operations (thread blocks, interrupt marks completion)
- Memory access: pages faults are result in blocking
- Such activities should be carried out in parallel/concurrently
- Application examples: webservers, make program, spreadsheets, word processors, processing large data volumes
OS Implementations of Threads
- User threads
- Kernel threads
- Hybrid[混合] implementations
User Threads
Many-to-One
- Thread management (creating, destroying, scheduling, thread control block manipulation[处理]) is carried out in user space with the help of a user library
- The process maintains a thread table managed by the runtime system without the kernel’s knowledge
- Similar to process table
- Used for thread switching
- Tracks thread related information
Advantages:
- Threads are in user space (i.e., no mode switches required)
- Full control over the thread scheduler
- OS independent[独立不受约束] (threads can run on OS that do not support them)
Disadvantages:
- Blocking system calls suspend[延缓] the entire process (user threads are mapped onto a single process, managed by the kernel)
- No true parallelism (a process is scheduled on a single CPU)
- Clock interrupts are non-existent (i.e. user threads are non-preemptive)
- Page faults[错误] result in blocking the process
Kernel Threads
One-to-One
- The kernel manages the threads, user application accesses threading facilities[工具] through API and system calls
- Thread table is in the kernel, containing thread control blocks (subset of process control blocks)
- If a thread blocks, the kernel chooses thread from same or different process (↔ user threads)
- Windows and Linux apply this approach
Advantages:
- True parallelism can be achieved
- No run-time system needed
Disadvantage:
- Frequent mode switches take place, resulting in lower performance
- Frequent mode switches take place, resulting in lower performance
Performance
Hybrid Implementations
Many-to-Many
- User threads are multiplexed onto kernel threads
- Kernel sees and schedules the kernel threads (a limited number)
- User application sees user threads and creates/schedules these (an “unrestricted” number)
Comparison
Thread Management
- Thread libraries provide an API/interface for managing threads (e.g. creating, running, destroying, synchronising, etc.)
- Thread libraries can be implemented:
- Entirely in user space (i.e. user threads)
- Based on system calls
- Examples of thread APIs include POSIX’s PThreads, Windows Threads, and Java Threads
- The PThread specification can be implemented as user or kernel threads
- POSIX threads are a specification that “anyone” can implement, i.e., it defines a set of APIs (function calls, over 60 of them) and what they do
Summary
- Threads vs. processes
- Thread implementations (user, kernel and hybrid)
- PThreads