Professional Documents
Culture Documents
Threads: Source: Operating System Concepts by Silberschatz, Galvin and Gagne
Threads: Source: Operating System Concepts by Silberschatz, Galvin and Gagne
Threads: Source: Operating System Concepts by Silberschatz, Galvin and Gagne
4.1
BB Karki, LSU
4.2
BB Karki, LSU
Overview
s Process can have 3 a single thread of control or activity 3 multiple threads of control or activity. s A thread is a flow of control within a process 3 a basic unit of CPU utilization also called a LWP 3 thread ID, program counter, register set and stack 3 all threads share the same address space of their process. s Multithreaded computer systems are common. 3 e.g., desktop PCs 3 Web browser can have two threads, one for display and the other for data retrieving. s Multithreading models s Threading issues s Thread libraries s OS examples
CSC 4103: Operating System 4.3 BB Karki, LSU
code section, data section and other resources, e.g., open files.
CSC 4103: Operating System 4.4 BB Karki, LSU
Benefits
s Increased responsiveness to user: 3 A program continues running with other threads even if part of it is blocked or performing a lengthy operation in one thread. s Resource sharing 3 Threads share memory and resources of their process. s Economy 3 Less time consuming to create and manage threads than processes as threads share resources, 3 e.g., thread creating is 30 times faster than process creating in Solaris. s Utilization of multiprocessor architectures 3 Increases concurrency because different threads can run in parallel on different processors (CPUs).
4.5
BB Karki, LSU
Thread Types
s User Threads
s Kernel Threads
4.6
BB Karki, LSU
4.7
BB Karki, LSU
Multithreading Models
Three common ways of establishing a relationship between user-level threads and kernel-level threads
s Many-to-One s One-to-One s Many-to-Many 3 Two-Level
4.8
BB Karki, LSU
Many-to-One Model
s Many user-level threads
4.9
BB Karki, LSU
One-to-One Model
s Each user-level thread maps to a kernel thread. s Overhead of creating kernel threads, one for each user thread. s No blocking problem s Provides concurrency. s Examples: Windows NT/XP/2000, Linux, Solaris 9 or later
4.10
BB Karki, LSU
Many-to-Many Model
s Allows many user level threads to be mapped to many kernel
threads. s Allows the OS to create a sufficient number of kernel threads. s Users can create as many as user threads as necessary.
s No blocking and
concurrency problems.
s Examples: Solaris
Two-Level Model
s Similar to M:M, except that it allows a user thread to be bound to
kernel thread s Examples: IRIX, HP-UX, Tru64 UNIX, Solaris 8 and earlier
4.12
BB Karki, LSU
4.13
BB Karki, LSU
Cancellation
s Task of terminating a thread before it has completed. 3 Canceling one thread in a multithreaded searching through a database. 3 Stopping a web page from loading. s Asynchronous cancellation 3 One thread immediately terminates the target thread (one to be cancelled). s Deferred cancellation 3 The target thread can periodically check if it should terminate, allowing a normal exit.
4.14
BB Karki, LSU
Signal Handling
s Signal to notify a process that a particular event has occurred. 3 Default or user defined signal handler. s Synchronous signal is related to the operation performed by a running
process.
3 Illegal memory access or division by zero.
s Asynchronous signal is caused by an event external to a running process. 3 Terminating a process (<control><C>) or a timer expires. s Options for delivering signals in a multithreaded process: 3 Signal to the thread to which the signal applies. 3 Signal to all threads. 3 Signal to certain threads. 3 Signal to a specific thread.
4.15
BB Karki, LSU
Thread Pools
them into a pool where they sit and wait for work.
3 e.g. for multithreading a web server.
pool size.
4.16
BB Karki, LSU
Specific Data
4.17
BB Karki, LSU
Scheduler Activations
s Communication between the user-thread
(light-weight process).
3 User thread runs on a virtual processor (LWP) 3 Corresponding kernel thread runs on a physical
LWP
processors from OS
3 Application schedules threads on these
processors 3 Kernel informs an application about certain events issuing upcalls, which are handled by thread library.
4.18
BB Karki, LSU
Thread Libraries
s A thread library provides the programmer with an API for
4.19
BB Karki, LSU
Pthreads
synchronization.
s A set of C language programming types and procedure calls. s Implemented with pthread.h header/include file and a thread
library.
s Common in UNIX operating systems.
4.20
BB Karki, LSU
Win32 Threads
4.23
BB Karki, LSU
Java Threads
s Java threads are managed by the JVM 3 All Java programs comprise at least a single thread of control 3 Two ways of creating Java threads s Extending Thread class: 3 Create a new thread that is derived from the Thread class, and override the run() method
4.24
BB Karki, LSU
run()method: thrd.start(); s Two threads created by JVM: 3 The first (parent) thread starts execution of the main() method. 3 The second (child) thread begins execution in the run() method.
s Java thread states
4.25
BB Karki, LSU
many mappings. s Each thread contains: A thread id and context (register set, separate user and kernel stacks, and private data storage area). s Data structures of a thread:
3 ETHREAD (executive thread block)
Pointer to the belonging process Pointer to the corresponding KTHREAD Address to the routine in which the thread starts control. 3 KTHREAD (kernel thread block) Scheduling and synchronization information Kernel stack Pointer to TEB. 3 TEB (thread environment block) User-space data structure for user-level thread User stack Array of thread-specific data (called thread-local storage).
CSC 4103: Operating System 4.26 BB Karki, LSU
Linux
s Linux refers to them as tasks rather than threads s Thread creation is done through clone() system call s clone() allows a child task to share the address space of
4.27
BB Karki, LSU
Summary
s A thread is a flow of control within the process. A process can have several
4.28
BB Karki, LSU