Operating Systems - ch5-F06
Operating Systems - ch5-F06
Threads
404452 Section 1 25 October 2004 Chapter 5
Chapter 4: Threads
Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads
Chapter 4
Operating Systems
Overview
Process characteristics can be grouped into two categories: Scheduling and execution dispatching priority and execution state Resource ownership memory, I/O devices, Unit of execution and a collection of resources with which the unit of execution is associated characterize the concept of a process. From this perspective, a: unit of resource ownership is a process, and unit of execution is a thread.
Chapter 4 Operating Systems 3
What is a Thread??
A thread, also referred to as a light-weight process (LWP), is an execution unit that can be independently scheduled but that shares a single address space with other threads making up a process. As a basic unit of CPU utilization, a thread consists of: an instruction pointer (also referred to as the PC or instruction counter), CPU register set, some storage for its local variables and a stack. A thread shares its code and data, as well as system resources and other OS related information, with its peer group (other threads of the same process).
Chapter 4
Operating Systems
Chapter 4
Operating Systems
Threads: an example
A good example of an application that could make use of threads is a file server on a local area network (LAN). A controller thread accepts file service requests and spawns a worker thread for each request, therefore may handle many requests concurrently. When a worker thread finishes servicing a request, it is destroyed.
Chapter 4
Operating Systems
Benefits
Responsiveness (responsiveness is how quickly an interactive program responds to user request). Ex. Multithreaded web server. Resource Sharing (threads belongs to the same process shares the same memory/resources no need to replicate the resources). Economy ( creating process instead of thread is costly in terms of time and resources). Utilization of MP Architectures (running different threads on different processors concurrently).
Chapter 4
Operating Systems
Thread implementations
User level implemented by thread library (as a set of library functions) OS is unaware of this type of threads therefore user level threads cannot be scheduled independently each thread gets partial time quantum of a process Blocking system calls like read() blocks the entire set of threads of a single process Low overhead, high computation performance and less costly (thread) operations. Kernel level implemented as system calls, can be scheduled directly by the OS, independent operation of threads in a single process, more expensive (thread) operations. Hybrid approach combines the advantages of the above two; e.g., Solaris threads, user-level threads that operates independently can be mapped onto different kernel-level threads, similarly, user-level threads, depending on each other, can be mapped onto the same kernel-level thread.
Chapter 4
Operating Systems
User Threads
Thread management done by user-level threads library Three primary thread libraries: POSIX Pthreads Win32 threads Java threads
Kernel Threads
Supported by the Kernel Examples Windows XP/2000 Solaris Linux Tru64 UNIX Mac OS X
Chapter 4
Operating Systems
10
Multithreading Models
Many-to-One One-to-One Many-to-Many
Chapter 4
Operating Systems
11
Many-to-One
Many user-level threads mapped to single kernel thread Thread management is done by thread library efficient and blocking. One user thread can access the kernel at a time. Examples: Solaris Green Threads GNU Portable Threads
Chapter 4
Operating Systems
12
One-to-One
Each user-level thread maps to kernel thread thus achieves higher concurrency Requires kernel thread for each user thread costly. Examples Windows NT/XP/2000 Linux Solaris 9 and later
Chapter 4
Operating Systems
13
Many-to-Many Model
Allows many user level threads to be mapped to many kernel threads Allows the operating system to create a sufficient number of kernel threads Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package
Chapter 4
Operating Systems
14
Two-level Model
Similar to M:M, except that it allows a user thread to be bound to kernel thread Examples IRIX HP-UX Tru64 UNIX Solaris 8 and earlier
Chapter 4
Operating Systems
15
Threading Issues Semantics of fork() and exec() system calls Does fork() duplicate only the calling thread or all threads? Thread cancellation Signal handling Thread pools Thread specific data Scheduler activations
Chapter 4 Operating Systems
16
Thread Cancellation
Terminating a thread before it has finished Thread to be canceled is called target thread Two general approaches: Asynchronous cancellation one thread terminates the target thread immediately Deferred cancellation allows the target thread to periodically check if it should be cancelled (usually there is a flag indicates if it should be cancelled).
Chapter 4
Operating Systems
17
Signal Handling
Signals are used in UNIX systems to notify a process that a particular event has occurred A signal handler is used to process signals 1. Signal is generated by particular event 2. Signal is delivered to a process 3. Signal is handled Options: Deliver the signal to the thread to which the signal applies (synchronous signals ) Deliver the signal to every thread in the process Deliver the signal to certain threads in the process Assign a specific thread to receive all signals for the process
Chapter 4 Operating Systems 18
Thread Pools
At process startup, create a number of threads and store them in a pool where they await work Advantages: Usually slightly faster to service a request with an existing thread than create a new thread Allows the number of threads in the application(s) to be bound to the size of the pool. This is important to systems that cant support large number of concurrent threads. (we refer to this issue as the degree of multithreading (concurrency)). The number of the threads in the pool depends on many factors like the number of CPUs, the available memory and system concurrency level.
Chapter 4 Operating Systems 19
Chapter 4
Operating Systems
20
Scheduler Activations
Scheduler activation: scheme for communication between thread library and the kernel. Both M:M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application The kernel provide a set of virtual processors and the application can schedule user threads onto an available virtual processor The goal is to adjust the number of kernel threads dynamically in order to achieve the best possible performance. Scheduler activations provide upcalls - a communication mechanism from the kernel to the thread library to inform it about happening events. This communication allows an application to maintain the correct number kernel threads.
Chapter 4
Operating Systems
21
Pthreads
A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization API specifies behavior of the thread library, implementation is up to development of the library Common in UNIX operating systems (Solaris, Linux, Mac OS X)
Chapter 4
Operating Systems
22
Windows XP Threads
Implements the one-to-one mapping Each thread contains A thread id Register set Separate user and kernel stacks Private data storage area The register set, stacks, and private storage area are known as the context of the threads The primary data structures of a thread include: ETHREAD (executive thread block) KTHREAD (kernel thread block) TEB (thread environment block)
Chapter 4 Operating Systems 23
Linux Threads
Linux refers to them as tasks rather than threads Thread creation is done through clone() system call clone() allows a child task to share the address space of the parent task (process)
Chapter 4
Operating Systems
24
Java Threads
Java threads are managed by the JVM Java threads may be created by: Extending Thread class Implementing the Runnable interface
Chapter 4
Operating Systems
25
Chapter 4
Operating Systems
26