Chapter 4 Threads Chapter 4 Threads n Overview

  • Slides: 28
Download presentation
Chapter 4: Threads

Chapter 4: Threads

Chapter 4: Threads n Overview n Multithreading Models n Threading Issues n Pthreads n

Chapter 4: Threads n Overview n Multithreading Models n Threading Issues n Pthreads n Windows XP Threads n Linux Threads n Java Threads 4. 2

Single and Multithreaded Processes 4. 3

Single and Multithreaded Processes 4. 3

Benefits n Responsiveness n Resource Sharing n Economy n Utilization of MP Architectures 4.

Benefits n Responsiveness n Resource Sharing n Economy n Utilization of MP Architectures 4. 4

User Threads n Thread management done by user-level threads library n Three primary thread

User Threads n Thread management done by user-level threads library n Three primary thread libraries: l POSIX Pthreads l Win 32 threads l Java threads 4. 5

Kernel Threads n Supported by the Kernel n Examples l Windows XP/2000 l Solaris

Kernel Threads n Supported by the Kernel n Examples l Windows XP/2000 l Solaris l Linux l Tru 64 UNIX l Mac OS X 4. 6

Multithreading Models n Many-to-One n One-to-One n Many-to-Many 4. 7

Multithreading Models n Many-to-One n One-to-One n Many-to-Many 4. 7

Many-to-One n Many user-level threads mapped to single kernel thread n Examples: l Solaris

Many-to-One n Many user-level threads mapped to single kernel thread n Examples: l Solaris Green Threads l GNU Portable Threads 4. 8

Many-to-One Model 4. 9

Many-to-One Model 4. 9

One-to-One n Each user-level thread maps to kernel thread n Examples l Windows NT/XP/2000

One-to-One n Each user-level thread maps to kernel thread n Examples l Windows NT/XP/2000 l Linux l Solaris 9 and later 4. 10

One-to-one Model 4. 11

One-to-one Model 4. 11

Many-to-Many Model n Allows many user level threads to be mapped to many kernel

Many-to-Many Model n Allows many user level threads to be mapped to many kernel threads n Allows the operating system to create a sufficient number of kernel threads n Solaris prior to version 9 n Windows NT/2000 with the Thread. Fiber package 4. 12

Many-to-Many Model 4. 13

Many-to-Many Model 4. 13

Two-level Model n Similar to M: M, except that it allows a user thread

Two-level Model n Similar to M: M, except that it allows a user thread to be bound to kernel thread n Examples l IRIX l HP-UX l Tru 64 UNIX l Solaris 8 and earlier 4. 14

Two-level Model 4. 15

Two-level Model 4. 15

Threading Issues n Semantics of fork() and exec() system calls n Thread cancellation n

Threading Issues n Semantics of fork() and exec() system calls n Thread cancellation n Signal handling n Thread pools n Thread specific data n Scheduler activations 4. 16

Semantics of fork() and exec() n Does fork() duplicate only the calling thread or

Semantics of fork() and exec() n Does fork() duplicate only the calling thread or all threads? 4. 17

Thread Cancellation n Terminating a thread before it has finished n Two general approaches:

Thread Cancellation n Terminating a thread before it has finished n Two general approaches: l Asynchronous cancellation terminates the target thread immediately l Deferred cancellation allows the target thread to periodically check if it should be cancelled 4. 18

Signal Handling n Signals are used in UNIX systems to notify a process that

Signal Handling n Signals are used in UNIX systems to notify a process that a particular event has occurred n A signal handler is used to process signals n 1. Signal is generated by particular event 2. Signal is delivered to a process 3. Signal is handled Options: l Deliver the signal to the thread to which the signal applies l Deliver the signal to every thread in the process l Deliver the signal to certain threads in the process l Assign a specific threa to receive all signals for the process 4. 19

Thread Pools n Create a number of threads in a pool where they await

Thread Pools n Create a number of threads in a pool where they await work n Advantages: l Usually slightly faster to service a request with an existing thread than create a new thread l Allows the number of threads in the application(s) to be bound to the size of the pool 4. 20

Thread Specific Data n Allows each thread to have its own copy of data

Thread Specific Data n Allows each thread to have its own copy of data n Useful when you do not have control over the thread creation process (i. e. , when using a thread pool) 4. 21

Scheduler Activations n Both M: M and Two-level models require communication to maintain the

Scheduler Activations n Both M: M and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application n Scheduler activations provide upcalls - a communication mechanism from the kernel to the thread library n This communication allows an application to maintain the correct number kernel threads 4. 22

Pthreads n A POSIX standard (IEEE 1003. 1 c) API for thread creation and

Pthreads n A POSIX standard (IEEE 1003. 1 c) API for thread creation and synchronization n API specifies behavior of the thread library, implementation is up to development of the library n Common in UNIX operating systems (Solaris, Linux, Mac OS X) 4. 23

Windows XP Threads n Implements the one-to-one mapping n Each thread contains l A

Windows XP Threads n Implements the one-to-one mapping n Each thread contains l A thread id l Register set l Separate user and kernel stacks l Private data storage area n The register set, stacks, and private storage area are known as the context of the threads n The primary data structures of a thread include: l ETHREAD (executive thread block) l KTHREAD (kernel thread block) l TEB (thread environment block) 4. 24

Linux Threads n Linux refers to them as tasks rather than threads n Thread

Linux Threads n Linux refers to them as tasks rather than threads n Thread creation is done through clone() system call n clone() allows a child task to share the address space of the parent task (process) 4. 25

Java Threads n Java threads are managed by the JVM n Java threads may

Java Threads n Java threads are managed by the JVM n Java threads may be created by: l Extending Thread class l Implementing the Runnable interface 4. 26

Java Thread States 4. 27

Java Thread States 4. 27

End of Chapter 4

End of Chapter 4