Chapter 4 Threads Chapter 4 Threads n Overview

  • Slides: 24
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 Operating System Concepts – 7 th edition, Jan 23, 2005 4. 2 Silberschatz, Galvin and Gagne © 2005

Single and Multithreaded Processes Operating System Concepts – 7 th edition, Jan 23, 2005

Single and Multithreaded Processes Operating System Concepts – 7 th edition, Jan 23, 2005 4. 3 Silberschatz, Galvin and Gagne © 2005

Benefits n Responsiveness n Resource Sharing n Economy n Utilization of MP Architectures Operating

Benefits n Responsiveness n Resource Sharing n Economy n Utilization of MP Architectures Operating System Concepts – 7 th edition, Jan 23, 2005 4. 4 Silberschatz, Galvin and Gagne © 2005

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 Operating System Concepts – 7 th edition, Jan 23, 2005 4. 5 Silberschatz, Galvin and Gagne © 2005

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 Operating System Concepts – 7 th edition, Jan 23, 2005 4. 6 Silberschatz, Galvin and Gagne © 2005

Multithreading Models n Many-to-One n One-to-One n Many-to-Many Operating System Concepts – 7 th

Multithreading Models n Many-to-One n One-to-One n Many-to-Many Operating System Concepts – 7 th edition, Jan 23, 2005 4. 7 Silberschatz, Galvin and Gagne © 2005

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 Operating System Concepts – 7 th edition, Jan 23, 2005 4. 8 Silberschatz, Galvin and Gagne © 2005

Many-to-One Model Operating System Concepts – 7 th edition, Jan 23, 2005 4. 9

Many-to-One Model Operating System Concepts – 7 th edition, Jan 23, 2005 4. 9 Silberschatz, Galvin and Gagne © 2005

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 Operating System Concepts – 7 th edition, Jan 23, 2005 4. 10 Silberschatz, Galvin and Gagne © 2005

One-to-one Model Operating System Concepts – 7 th edition, Jan 23, 2005 4. 11

One-to-one Model Operating System Concepts – 7 th edition, Jan 23, 2005 4. 11 Silberschatz, Galvin and Gagne © 2005

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 Operating System Concepts – 7 th edition, Jan 23, 2005 4. 12 Silberschatz, Galvin and Gagne © 2005

Many-to-Many Model Operating System Concepts – 7 th edition, Jan 23, 2005 4. 13

Many-to-Many Model Operating System Concepts – 7 th edition, Jan 23, 2005 4. 13 Silberschatz, Galvin and Gagne © 2005

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 Operating System Concepts – 7 th edition, Jan 23, 2005 4. 14 Silberschatz, Galvin and Gagne © 2005

Two-level Model Operating System Concepts – 7 th edition, Jan 23, 2005 4. 15

Two-level Model Operating System Concepts – 7 th edition, Jan 23, 2005 4. 15 Silberschatz, Galvin and Gagne © 2005

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 Operating System Concepts – 7 th edition, Jan 23, 2005 4. 16 Silberschatz, Galvin and Gagne © 2005

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) Operating System Concepts – 7 th edition, Jan 23, 2005 4. 17 Silberschatz, Galvin and Gagne © 2005

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) Operating System Concepts – 7 th edition, Jan 23, 2005 4. 18 Silberschatz, Galvin and Gagne © 2005

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) Operating System Concepts – 7 th edition, Jan 23, 2005 4. 19 Silberschatz, Galvin and Gagne © 2005

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) Operating System Concepts – 7 th edition, Jan 23, 2005 4. 20 Silberschatz, Galvin and Gagne © 2005

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 Operating System Concepts – 7 th edition, Jan 23, 2005 4. 21 Silberschatz, Galvin and Gagne © 2005

Java Thread States Operating System Concepts – 7 th edition, Jan 23, 2005 4.

Java Thread States Operating System Concepts – 7 th edition, Jan 23, 2005 4. 22 Silberschatz, Galvin and Gagne © 2005

A Simple Example of Java Thread class Thread. Example extends Thread { public Thread.

A Simple Example of Java Thread class Thread. Example extends Thread { public Thread. Example(String name) { super(name); } public void run() { while(true) { System. out. println(get. Name()+": Hello"); } } public static void main(String[] args) { Thread. Example t 1 = new Thread. Example("Yoshi"); Thread. Example t 2 = new Thread. Example("King"); t 1. start(); t 2. start(); } } Operating System Concepts – 7 th edition, Jan 23, 2005 4. 23 Silberschatz, Galvin and Gagne © 2005

Check the API Doc for java. lang. Thread n http: //java. sun. com/j 2

Check the API Doc for java. lang. Thread n http: //java. sun. com/j 2 se/1. 5. 0/docs/api/java/lang/Thread. html Operating System Concepts – 7 th edition, Jan 23, 2005 4. 24 Silberschatz, Galvin and Gagne © 2005