Presentation is loading. Please wait.

Presentation is loading. Please wait.

Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading.

Similar presentations


Presentation on theme: "Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading."— Presentation transcript:

1 Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading Models Solaris 2 Threads Java Threads NOTE: Instructor annotations in BLUE

2 Silberschatz, Galvin, and Gagne  1999 5.2 Applied Operating System Concepts Threads - Overview A thread (or lightweight process) is a basic unit of CPU utilization; it consists of: –program counter –register set –stack space A thread shares with its peer threads (in same process) its: –code section –data section –operating-system resources collectively know as a task. Actually the three things above belong to the process Threads in the process share this stuff. A traditional or heavyweight process is equal to a task with one thread

3 Silberschatz, Galvin, and Gagne  1999 5.3 Applied Operating System Concepts Threads (- Overview Cont.) In a multiple threaded task, while one server thread is blocked and waiting, a second thread in the same task can run. (assuming the blocked thread doesn’t block the process - if so, everything comes to a screeching halt!) –Cooperation of multiple threads in same job confers higher throughput and improved performance. –Applications that require sharing a common buffer (i.e., producer-consumer) benefit from thread utilization. Threads provide a mechanism that allows sequential processes to make blocking system calls while also achieving parallelism. … a thread within the process makes the call & only it blocks while other threads in the process still run …assuming kernel supported threads Kernel-supported threads – can reside in user or kernel space, but kernel is involved in their control. User-level (supported) threads; supported from a set of library calls at the user level. Resides in user space. Hybrid approach implements both user-level and kernel-supported threads (Solaris 2).

4 Silberschatz, Galvin, and Gagne  1999 5.4 Applied Operating System Concepts Multiple Threads within a Task

5 Silberschatz, Galvin, and Gagne  1999 5.5 Applied Operating System Concepts Responsiveness - Opens the possibility of blocking only one thread in a process on a blocking call rather than the entire process. Resource Sharing – Threads share resources of process to which they belong – code sharing allow multiple instantiations of code execution Economy – low overhead in thread context switching & thread management compared to process context switching & management Utilization of MP Architectures – can be applied to a multi- processor. In a uniprocessor, task switching is so fast that it gives illusion of parallelism. Benefits

6 Silberschatz, Galvin, and Gagne  1999 5.6 Applied Operating System Concepts Single and Multithreaded Processes

7 Silberschatz, Galvin, and Gagne  1999 5.7 Applied Operating System Concepts User Level (supported) Threads (ULTs) Thread Management Done by User-Level Threads Library Kernel unaware of ULTs No kernel intervention needed for thread management. Thus fast to create and manage If thread blocks, the kernel sees it as the process blocking, and may block the entire process (all threads). Examples - POSIX Pthreads - Mach C-threads - Solaris threads … but not totally kernel independent …

8 Silberschatz, Galvin, and Gagne  1999 5.8 Applied Operating System Concepts Kernel Level (supported) Threads (KLTs) Supported by the Kernel – thread management done by kernel in kernel space. Since kernel managing threads, kernel can schedule another thread if a given thread blocks rather than blocking the entire processes. Examples - Windows 95/98/NT - Solaris - Digital UNIX

9 Silberschatz, Galvin, and Gagne  1999 5.9 Applied Operating System Concepts Multithreading Models User Threads / Kernel Threads models. Three common types of threading implementation: Many-to-One One-to-One Many-to-Many

10 Silberschatz, Galvin, and Gagne  1999 5.10 Applied Operating System Concepts Many-to-One Many User-Level Threads Mapped to Single Kernel Thread. Thread management done at user level - efficient Entire user processes blocks if one of its threads block – kernel only sees the process. Bad for multiprocessor (parallel) architectures because only one thread at a time can access kernel. Used on Systems That Do Not Support Kernel Threads. - Single threaded kernel

11 Silberschatz, Galvin, and Gagne  1999 5.11 Applied Operating System Concepts Many-to-one Model

12 Silberschatz, Galvin, and Gagne  1999 5.12 Applied Operating System Concepts One-to-One Each User-Level Thread Maps to a distinct Kernel Thread. Other threads can run when a particular thread makes a blocking call - better concurrency. Multiple threads can run in parallel in a multiprocessing architecture. Drawback is that creating a user thread requires creating a kernel thread … overhead factor … number of threads restricted Examples - Windows 95/98/NT - OS/2

13 Silberschatz, Galvin, and Gagne  1999 5.13 Applied Operating System Concepts One-to-one Model

14 Silberschatz, Galvin, and Gagne  1999 5.14 Applied Operating System Concepts Many-to-Many Model Time Multiplexes many ULT’s to a smaller of equal number of KLT’s. Number of kernel threads may be specific to either a particular application or machine … typically a multiprocessor may get more k-threads than a uniprocessor. Programmer may create as many user threads as desired (compare to 1-to-1 in which there my be a shortage of k-threads. True concurrency limited to scheduling (multiplexing) many user threads against a less number of k-threads. But the multiple k- threads can be run in parallel on a multiprocessor machine.

15 Silberschatz, Galvin, and Gagne  1999 5.15 Applied Operating System Concepts Many-to-many Model

16 Silberschatz, Galvin, and Gagne  1999 5.16 Applied Operating System Concepts Pthreads Example of user controlled threads Refers to the POSIX standard (IEEE 1003.1c) API - specifies behavior, not implementation. More commonly implemented on UNIX based system such as Solaris Runs from a Pthead user level library with not specified relationship and any associated kernel threads. –Some implementations may allow some kernel control - ex: scheduling??? The “main” function in the C application program is the initial thread created by default, all other threads are created using library calls from the application program- each thread has stack

17 Silberschatz, Galvin, and Gagne  1999 5.17 Applied Operating System Concepts Pthreads(continued) Two typical Pthread library calls: –pthread_create  creates a new thread  allows you to pass the name of a function which has will be the behavior or function that this thread will do.  Allows you to pass parameters to the function of the new thread.  sets a thread id variable to the unique “tid”. –Pthread_join  allows a thread to wait for another specified thread to complete before continuing on - a synchronization tedchique There is a very large and rich set of library function calls to use in Pthread programming - Lots of calls for thread synchronization - more later! See example in Figure 5.5, page 140.

18 Silberschatz, Galvin, and Gagne  1999 5.18 Applied Operating System Concepts Threads Support in Solaris 2 Solaris 2 is a version of UNIX with support for threads at both the kernel and user levels, symmetric multiprocessing, and real-time scheduling. Solaris 2 implements the pthread API, in addition to supporting userlevel threadswith a thread library with API’s for thread creation & management ==> Solaris threads LWP – intermediate level between user-level threads and kernel-level threads. Resource needs of thread types: –Kernel thread (are in the kernel - does OS stuff or associates with an LWP) : small data structure and a stack; thread switching does not require changing memory access information – relatively fast. –LWP: Associated with a both a user process (PCB) and a kernel thread. Has register data, accounting, and memory information; switching between LWPs is relatively slow. - because of kernel intervention. Scheduled by kernel –User-level thread: only need stack and program counter; no kernel involvement means fast switching (multiplexing to LPW). Kernel only sees the LWPs that are dedicated to user-level threads.

19 Silberschatz, Galvin, and Gagne  1999 5.19 Applied Operating System Concepts Solaris 2 Threads

20 Silberschatz, Galvin, and Gagne  1999 5.20 Applied Operating System Concepts Solaris Threads Threads in user space similar to “pure” Pthreads but with required kernel control Process Structure control information completely in kernel space LWP in kernel space but share the user process resources - can interface with user threads - is visible to user threads. LWP can be thought of as a virtual CPU that is available for executing code... We have a single process in one memory space with many virtual “CPU’s” running different parts of the process concurrently (Lewis & Berg). Each LWP is separately scheduled by kernel - kernel schedules LWP’s not user threads. The thread library for user threads is in user space - the user level thread library schedules (multiplexes) user threads onto LWP’s, and LWP’s, in turn, are implemented by kernel threads and scheduled by the kernel.

21 Silberschatz, Galvin, and Gagne  1999 5.21 Applied Operating System Concepts Solaris Process

22 Silberschatz, Galvin, and Gagne  1999 5.22 Applied Operating System Concepts Java Threads Java Threads May be Created by: –Extending Thread class –Implementing the Runnable interface

23 Silberschatz, Galvin, and Gagne  1999 5.23 Applied Operating System Concepts Extending the Thread Class class Worker1 extends Thread { public void run() { System.out.println(“I am a Worker Thread”); }

24 Silberschatz, Galvin, and Gagne  1999 5.24 Applied Operating System Concepts Creating the Thread public class First { public static void main(String args[]) { Worker runner = new Worker1(); runner.start(); System.out.println(“I am the main thread”); }

25 Silberschatz, Galvin, and Gagne  1999 5.25 Applied Operating System Concepts The Runnable Interface public interface Runnable { public abstract void run(); }

26 Silberschatz, Galvin, and Gagne  1999 5.26 Applied Operating System Concepts Implementing the Runnable Interface class Worker2 implements Runnable { public void run() { System.out.println(“I am a Worker Thread”); }

27 Silberschatz, Galvin, and Gagne  1999 5.27 Applied Operating System Concepts Creating the Thread public class Second { public static void main(String args[]) { Runnable runner = new Worker2(); Thread thrd = new Thread(runner); thrd.start(); System.out.println(“I am the main thread”); }

28 Silberschatz, Galvin, and Gagne  1999 5.28 Applied Operating System Concepts Java Thread Management suspend() – suspends execution of the currently running thread. sleep() – puts the currently running thread to sleep for a specified amount of time. resume() – resumes execution of a suspended thread. stop() – stops execution of a thread.

29 Silberschatz, Galvin, and Gagne  1999 5.29 Applied Operating System Concepts Java Thread States Runnable state has both actively executing threads, and “ready” threads

30 Silberschatz, Galvin, and Gagne  1999 5.30 Applied Operating System Concepts Producer Consumer Problem public class Server { public Server() { MessageQueue mailBox = new MessageQueue(); Producer producerThread = new Producer(mailBox); Consumer consumerThread = new Consumer(mailBox); producerThread.start(); consumerThread.start(); } public static void main(String args[]) { Server server = new Server(); }

31 Silberschatz, Galvin, and Gagne  1999 5.31 Applied Operating System Concepts Producer Thread class Producer extends Thread { public Producer(MessageQueue m) { mbox = m; } public void run() { while (true) { // produce an item & enter it into the buffer Date message = new Date(); mbox.send(message); } private MessageQueue mbox; }

32 Silberschatz, Galvin, and Gagne  1999 5.32 Applied Operating System Concepts Consumer Thread class Consumer extends Thread { public Consumer(MessageQueue m) { mbox = m; } public void run() { while (true) { Date message = (Date)mbox.receive(); if (message != null) // consume the message } private MessageQueue mbox; }


Download ppt "Silberschatz, Galvin, and Gagne  1999 5.1 Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading."

Similar presentations


Ads by Google