1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.

Slides:



Advertisements
Similar presentations
5.1 Silberschatz, Galvin and Gagne ©2003 Operating System Concepts with Java Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads.
Advertisements

02/05/2008CSCI 315 Operating Systems Design1 Java Threads Notice: The slides for this lecture have been largely based on those accompanying an earlier.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
Silberschatz, Galvin, and Gagne  Applied Operating System Concepts Module 5: Threads Benefits User and Kernel Threads Multithreading Models Solaris.
CHAPTER 5 THREADS & MULTITHREADING 1. Single and Multithreaded Processes 2.
Chapter 4: Multithreaded Programming
Chapter 5 Threads os5.
Chapter 4: Multithreaded Programming
02/05/2007CSCI 315 Operating Systems Design1 Java Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Silberschatz, Galvin, and Gagne  Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading.
Threads - Definition - Advantages using Threads - User and Kernel Threads - Multithreading Models - Java and Solaris Threads - Examples - Definition -
CS238 Lecture 5 Threads Dr. Alan R. Davis. Threads Definitions Benefits User and Kernel Threads Multithreading Models Solaris 2 Threads Java Threads.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Based on Silberschatz, Galvin and Gagne  2009 Threads Definition and motivation Multithreading Models Threading Issues Examples.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
02/02/2004CSCI 315 Operating Systems Design1 Java Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Process Concept An operating system executes a variety of programs
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
9/13/20151 Threads ICS 240: Operating Systems –William Albritton Information and Computer Sciences Department at Leeward Community College –Original slides.
Operating System Principles Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Chapter 4 MultiThreaded Programming. 2015/9/18os Outline Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows XP/2000.
Chapter 4: Threads. From Processes to Threads 4.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Threads.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Multithreading Allows application to split itself into multiple “threads” of execution (“threads of execution”). OS support for creating threads, terminating.
Silberschatz, Galvin and Gagne ©2011Operating System Concepts Essentials – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Operating System Concepts Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
CHAPTER 5: THREADS ( 线程 ) Overview Overview Multithreading Models (多线程模型) Multithreading Models (多线程模型) Thread Implementation Issues (线程实现事项) Thread Implementation.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Multithreading Models Threading Issues Pthreads Solaris 2 Threads.
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 13 Threads Read Ch 5.1.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
Overview Multithreading Models Threading Issues Pthreads Solaris 2 Threads Windows 2000 Threads Linux Threads Java Threads.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
H.-H. S. Lee 1 ECE3055 Computer Architecture and Operating Systems Lecture 12 Threads Prof. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering.
Cooperating Processes The concurrent processes executing in the operating system may be either independent processes or cooperating processes. A process.
CSS430 Threads1 Textbook Ch4 These slides were compiled from the OSC textbook slides (Silberschatz, Galvin, and Gagne) and the instructor’s class materials.
Chapter 4: Threads Joe McCarthy CSS 430: Operating Systems - Threads1.
CSS430 Threads1 Textbook Ch5 These slides were compiled from the OSC textbook slides (Silberschatz, Galvin, and Gagne) and the instructor’s class materials.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads  Overview  Multithreading Models  Threading Issues  Pthreads.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Multithreaded Programming.
Introduction to threads
OPERATING SYSTEM CONCEPT AND PRACTISE
Chapter 4: Threads.
Concurrency, Processes and Threads
Chapter 5: Threads Overview Multithreading Models Threading Issues
Operating System (013022) Dr. H. Iwidat
Chapter 5: Threads Overview Multithreading Models(多线程模型)
Operating System Concepts
Chapter 5: Threads Overview Multithreading Models Threading Issues
Chapter 5: Threads Overview Multithreading Models Threading Issues
Concurrency, Processes and Threads
Chapter 4: Threads.
Presentation transcript:

1 Chapter 5 Threads

2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads

3 Thread  A thread, sometimes called a lightweight process, is a basic unit of CPU utilization  Comprises  A thread ID  A program counter  A register set  A stack  Shares with other threads belonging to the same process:  Code section  Data section  Other resources § 5.1

4 Thread  A traditional, or heavyweight, process has a single thread of control.

5 Thread Examples  Busy web server has many concurrently accessing clients.  Java program connecting to the server will be blocked until the connection is made since Java has no concept of asynchronous. Solution: another thread with timeout sleeping checking the connecting thread when wake up.

6 Benefits  Responsiveness – continue running even if part of it is blocked  Resource sharing – allows an application to have several different threads of activity all within the same address space  Economy – it is more economical to create and context switch threads than process  Utilization of multiprocessor architectures – in single-processor architecture, multithread is only an illusion § 5.2

7 User Threads  User threads are supported by user-level threads library without the need for kernel intervention.  Fast to create and manage.  Examples - POSIX Pthreads - Mach C-threads - Solaris threads § 5.3

8 Kernel Threads  Kernel threads are supported directly by the OS in kernel space  Slower to create and manage.  Examples - Windows 95/98/NT - Solaris - Digital UNIX § 5.3.2

9 Multithreading Models  Many-to-One Model  One-to-One Model  Many-to-Many Model § 5.4

10 Many-to-One Model  Many user-level threads mapped to single kernel thread.  Used on systems that do not support multiple kernel threads.  Efficient, but the entire process will be blocked if a thread makes a blocking system call  Since only one thread can access the kernel at a time, multiple threads are unable to run in parallel on multiprocessors

11 Many-to-One Model

12 One-to-One Model  Each user thread maps to a kernel thread.  Provides more concurrency than the many-to-one  Drawback: creating a user thread requires creating the corresponding kernel thread … burden the performance … most system restrict the number of threads supported.  Examples - Windows 95/98/NT - OS/2

13 One-to-One Model

14 Many-to-Many Model  Multiplex many user threads to a smaller or equal number of kernel threads.  Developers can create as many user threads as necessary and the corresponding kernel threads can run in parallel on a multiprocessor.  Examples:  Solaris  IRIX  Digital UNIX

15 Many-to-Many Model

16 Solaris 2 Threads  Solaris 2 is a version of UNIX  Supported only traditional heavy-weight processes within a single thread of control until 1992  Intermediate level thread: lightweight processes (LWP)  Each process contains at least one LWP  Only user-level threads currently connected to an LWP accomplish work § 5.5

17 Solaris 2 Threads

18 Solaris Process ( 略 )

19 Java Threads  Java is unique because it provides support for creation and management of threads at the language level, it provides commands that allows the programmer to create and manipulate threads of control within the program.  All Java program comprise at least a single thread of control.  Java Threads May be Created by:  Extending Thread class  Implementing the Runnable interface § 5.6

20 Extending the Thread Class class Worker1 extends Thread { public void run() { System.out.println(“I am a Worker Thread”); } public class First { public static void main(String args[]) { Worker1 runner = new Worker1(); runner.start(); System.out.println(“I am the main thread”); } Worker1 Thread Worker1 runner run start

21 Extending the Thread Class  An object of this derived class will run as a separate thread of control in the JVM.  Two threads are created by the JVM:  The thread associated with the application – the thread that starts execution at the main() method.  The runner thread that is created explicitly with the start() method.

22 Extending the Thread Class class Worker1 extends Thread { public void run() { System.out.println(“I am a Worker Thread”); } public class First { public static void main(String args[]) { Worker runner = new Worker1(); runner.start(); System.out.println(“I am the main thread”); } 1 2

23 The Runnable Interface  Another option to create a separate thread is to define a class that implements the Runnable interface.  The runnable interface is defined as public interface Runnable { public abstract void run(); }  When a class implements Runnable, it must define a run() method.

24 Implementing the Runnable Interface class Worker2 implements Runnable { public void run() { System.out.println(“I am a Worker 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”); }

25 Why Implements Runnable ?  Since Java does not support multiple inheritance, if a class is already derived from another class, it will not also be able to extend the Thread class.  Example: an applet already extends the Applet class has to also implement the Runnable interface: public class ThreadedApplet extends Applet implements Runnable { ……… }  Unless a class requires multithreading and is already extended, we will adopt the approach of extending the Thread class.

26 Java Thread Management  Java APIs for managing threads:  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. § 5.6.2

27 Applets with threads  Applets are natural examples for multithreading because they commonly have graphics, animation, and audio – all good candidates as threads.  start() method of an applet is called when an applet is first displayed. If the user leaves the web page or the applet scrolls off the screen, the applet’s stop() method is called.  The destroy() method of an applet is called when the applet is removed from the browser’s cache.

28 Java Thread States  A Java thread can be in one of four states:  New: when an object for the thread is created  Runnable: When a thread’s run() method is invoked, the thread moves from the New to the Runnable state.  Blocked: when performing a blocking statement, such as doing I/O, or if it invokes methods like sleep() or suspend().  Dead: when its run() method terminates or when its stop() method is called. § 5.6.3

29 Java Thread States  It is not possible to determine the exact state of a thread, however, isAlive() method returns a boolean value to determine whether or not a thread is dead.

30 Threads and the JVM  In addition to a Java application program contains several different threads of control, there are several threads running asynchronously on behalf of the JVM handling system-level tasks such as memory management and graphics control. § 5.6.4

31 JVM and Host OS  The typical implementation of the JVM is on top of a host OS. This allows the JVM to hide the implementation details of the underlying OS and to provide a consistent, abstract environment that allows Java programs to operate on any platform that supports a JVM.  Windows NT: one-to-one, each Java thread for a JVM running on NT maps to a kernel thread  Solaris 2: many-to-one  Solaris 2.6: many-to-many § 5.6.5

32 Producer Consumer Problem public class Server { public Server() { // first create the message buffer MessageQueue mailBox = new MessageQueue(); // now create the producer and consumer threads Producer producerThread = new Producer(mailBox); Consumer consumerThread = new Consumer(mailBox); producerThread.start(); consumerThread.start(); } public static void main(String args[]) { Server server = new Server(); }

33 Producer Consumer Problem MessageQueue send receive vector queue mailBox consumer producer producerThread consumerThread Server

34 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; } Producer MessageQueue send receive vector queue mBox Thread

35 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; } Consumer MessageQueue send receive vector queue mBox Thread