07 CSC520 04 Bahan Pengajaran Chapter 04 Threads
Chapter 4: Threads Overview
Definition of a Thread: A thread is the fundamental unit of CPU utilization, forming the basis of multithreaded systems.
Key Concepts Under Discussion:
Multicore Programming
Multithreading Models
Thread Libraries (Pthreads, Windows, and Java)
Implicit Threading and Threading Issues
Operating System Examples supporting threads.
Objectives
Introduce the fundamental idea of threads in operating systems.
Discuss APIs for widely used thread libraries: Pthreads, Windows, and Java.
Explore implicit threading strategies and the corresponding programming issues.
Cover OS support for threads in major systems like Windows and Linux.
Motivation for Multithreading
Modern Applications: Most contemporary applications use multithreading to handle multiple tasks concurrently.
Tasks such as updating displays, fetching data, and responding to network requests can be handled by separate threads.
Process vs. Thread Creation:
Process creation is considered heavy-weight, while thread creation is more light-weight.
Threading simplifies code, increases efficiency, and allows kernels to be multithreaded.
Benefits of Multithreading
Responsiveness: Threads improve application responsiveness, particularly in user interfaces.
Resource Sharing: Threads share the resources within a process, simplifying resource management compared to message passing or shared memory.
Economic Efficiency: Thread creation and context switching are less resource-intensive than process creation and context switching.
Scalability: Facilitates the utilization of multiprocessor architectures.
Concurrency vs. Parallelism
Concurrency: Occurs when multiple threads of a single-core process are executed seemingly simultaneously through time-slicing.
Parallelism: True simultaneous execution on a multi-core system, where multiple threads can run at the same time.
Types of Multithreading Models
Many-to-One: Multiple user-level threads mapped to a single kernel thread.
Pros: Simplicity and lower overhead.
Cons: Only one thread can be in the kernel at a time; if one blocks, all block.
Examples: Solaris Green Threads, GNU Portable Threads.
One-to-One: Each user-level thread maps directly to a kernel thread.
Pros: More concurrency.
Cons: Potential for high overhead.
Examples: Windows, Linux, and Solaris 9 or later.
Many-to-Many: Allows multiple user-level threads to be mapped to multiple kernel threads, optimizing concurrency.
Used in earlier Solaris versions and Windows with the ThreadFiber package.
Two-level Model: Combines many-to-many with user threads being bound to kernel threads, enhancing control over scheduling.
Found in systems like IRIX and HP-UX.
Threading Issues
Fork and Exec Semantics: Questions arise regarding whether
fork()duplicates only the calling thread or all threads. Universal understanding is thatexec()replaces the entire process including all threads.Signal Handling: The mechanisms for handling signals vary in single-threaded and multi-threaded contexts. Signals can either be directed to specific threads or to all threads.
Thread Cancellation: Refers to the termination of a thread before its completion, which can occur immediately (asynchronous) or at defined points (deferred).
Thread-Local Storage: Facilitates storing data unique to each thread, allowing for efficient data management without interference among threads across invocations.
Scheduler Activations: Involve communication between the kernel and user threads to manage the allocation of kernel threads for active user processes.
Operating System Examples
Windows Threads: Implements a one-to-one thread model, where each thread has its unique properties, including a thread ID, a register set, and a set of private storage data structures (TEB, KTHREAD).
Linux Threads: Referred to as tasks, created using the
clone()system call that allows child tasks to share the parent's address space, with various flags controlling behavior and thread management through thestruct task_struct.