CSS430 Threads Textbook Chapter 4

Slides:



Advertisements
Similar presentations
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 AE4B33OSS Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows.
Advertisements

Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Multithreaded Programming.
Threads. Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Threads.
Chapter 4: Multithreaded Programming
Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)
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: Multithreaded Programming
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 2 nd Edition Chapter 4: Threads.
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 – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
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.
Based on Silberschatz, Galvin and Gagne  2009 Threads Definition and motivation Multithreading Models Threading Issues Examples.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Chapter 4: Threads.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Objectives Thread definitions and relationship to process Multithreading.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Chapter 4: Threads READ 4.1 & 4.2 NOT RESPONSIBLE FOR 4.3 &
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Multithreaded Programming.
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.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
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.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Multithreaded Programming Overview.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 4 Operating Systems.
Chapter 4: Threads. 4.2 Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2011Operating System Concepts Essentials – 8 th Edition Chapter 4: Threads.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads A thread (or lightweight process) is a basic unit of CPU.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
CHAPTER 5: THREADS ( 线程 ) Overview Overview Multithreading Models (多线程模型) Multithreading Models (多线程模型) Thread Implementation Issues (线程实现事项) Thread Implementation.
Chapter 4: Threads.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Multithreading Models Threading Issues Pthreads Solaris 2 Threads.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads Modified from the slides of the text book. TY, Sept 2010.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts What is Thread “Thread is a part of a program.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
CSS430 Threads1 Textbook Ch4 These slides were compiled from the OSC textbook slides (Silberschatz, Galvin, and Gagne) and the instructor’s class materials.
Saurav Karmakar. Chapter 4: Threads  Overview  Multithreading Models  Thread Libraries  Threading Issues  Operating System Examples  Windows XP.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Typical Program Processor utilization?
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Operating System Concepts
CSS430 Threads1 Textbook Ch5 These slides were compiled from the OSC textbook slides (Silberschatz, Galvin, and Gagne) and the instructor’s class materials.
Chapter 4: Threads 羅習五. Chapter 4: Threads Motivation and Overview Multithreading Models Threading Issues Examples – Pthreads – Windows XP Threads – Linux.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Lecturer 3: Processes multithreaded Operating System Concepts Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess.
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.
Chapter 5: Threads Overview Multithreading Models Threading Issues
Chapter 4: Threads.
Chapter 4: Threads.
Modified by H. Schulzrinne 02/15/10 Chapter 4: Threads.
Chapter 4: Threads.
Presentation transcript:

CSS430 Threads Textbook Chapter 4 Instructor: Stephen G. Dame e-mail: sdame@uw.edu These slides were adapted from the OSC textbook slides (Silberschatz, Galvin, and Gagne), Professor Munehiro Fukuda and the instructor’s class materials. CSS430 Operating Systems : Threads

WKP 17 “I have not failed. I've just found 10,000 ways that won't work.” – Thomas Edison CSS430 Operating Systems : Threads

Thread Concepts Process-oriented computing Multithreaded computing code files data code files data thread code files data process code files data code files data process Operating System Operating System CSS430 Operating Systems : Threads

Single vs. Multithreaded Processes Carries everything such as an address space, code, data, and files Heavyweight in terms of creation/termination/context switching Threads Shares an address space, code, data and files if they belong to the same process Lightweight in terms of creation/termination/context switching Then, what is uniquely carried by each thread? CSS430 Operating Systems : Threads

Threads in Java vs. C/C++ Threads are built-in to the Java API Portable JVM uses threads as well for running programs and housekeeping (e.g. garbage collection) C/C++ Threads not available in core C/C++ Can be implemented via platform dependent thread libraries (e.g. Pthreads) CSS430 Operating Systems : Threads

new Thread to service each Request Multithreaded Server new Thread to service each Request Request Server Client Thread Thread Thread Listen for additional requests CSS430 Operating Systems : Threads

Thread Benefits Responsiveness Resource sharing Economy Scalability Create new threads to process requests while receiving new requests Resource sharing Threads share an address space, files, code, and data Economy ~25-30x faster to create new threads vs. processes ~ 5x faster to context switch Scalability Better utilization of multiprocessor architecture CSS430 Operating Systems : Threads

Concurrent Programming (preview of coming attractions...) “Programming concurrent applications is a difficult and error-prone undertaking” – Dietel** When thread synchronization is required use the following guidelines (in order of complexity): Use existing classes from the Java API (e.g. import java.util.concurrent.*) Use synchronized keyword and Object methods wait, notify and notifyAll Use Lock and Condition interfaces ** (p.678) Java for Programmers, Deitel & Deitel, 2nd Edition, Prentice Hall, 2011 CSS430 Operating Systems : Threads

Benefit: Responsiveness Multithreaded Server Web Server Clients DB1 DB2 CGI http pages CSS430 Operating Systems : Threads

Benefit: Deadlock avoidance Process 2 Process 1 Bounded buffer send send receive receive Bounded buffer A sequence of send and then receive does not work! Send and receive must be executed concurrently with threads. CSS430 Operating Systems : Threads

Benefit: Utilization of multicore multiprocessor architecture time = t time = t + x Place code, files and data in the main memory. Distribute threads to each of CPUs, and Let them execute in parallel. time = t + 2x data … code files data CSS430 Operating Systems : Threads

Discussion 1 Why do threads context switch much faster than processes? What data structure must threads carry within their own resources? What data structure can they share? What if we use processes rather than threads for the previous three cases? a) Responsiveness b) Deadlock avoidance c) Utilization of multiprocessor architecture Provide one programming example of multithreading giving improved performance over a single-threaded solution. Provide one programming example of multithreading that would not improve performance over a single-threaded solution. CSS430 Operating Systems : Threads

CSS430 ThreadOS Processes are managed by the real operating system, and cannot be handled by ThreadOS. Threads are multiple execution entities inside a process ThreadOS is a process, therefore ThreadOS executes multiple java application programs as threads. CSS430 Operating Systems : Threads

Simple Thread (ThreadOS) CSS430 Operating Systems : Threads

User and Kernel Threads User Threads Thread Management Done by User-Level Threads Library Examples - POSIX Pthreads - Win32 threads - Java threads Kernel does not care how many user threads exist. Kernel Threads Supported by the Kernel - Linux - Windows XP/2000 - Solaris lightweight processes CSS430 Operating Systems : Threads

Many-to-One Model Many user-level threads mapped to a single kernel thread. Used on systems that do not support kernel threads. Examples: Solaris Green Threads, GNU Portable Threads user thread PRO CONS Fast Non preemption or version complicated to handle preemption and I/O No use of MP architecture k kernel thread CSS430 Operating Systems : Threads

One-to-One Model Each user-level thread maps to kernel thread. Examples - Windows NT/XP/2000 - Linux - Solaris 9 or later PRO CONS 1. Everything supported by OS 2. Uses MP architecture Slower than many-to-one model. Too many threads do not help user thread k k k k kernel thread CSS430 Operating Systems : Threads

Many-to-Many Model Addresses the shortcomings of the previous two models Examples - Windows XP fiber library - AIX Pthreads - HP_UX user thread k k k kernel thread CSS430 Operating Systems : Threads

Linux Thread Implementation Process provide by traditional fork() system call Linux doesn’t distinguish between processes and threads Each thread is viewed as a process inside the kernel Linux rather creates tasks using clone()system call Uses flags to manage resources no flags  fork() CSS430 Operating Systems : Threads

Lightweight Process (LWP) A virtual CPU that is available for executing code. * Can perform independent system calls Can incur independent page faults Multiple LWPs, in the same process, can run in parallel on multiple processors registers LWP ID priority Intr. mask LWP “effectively we can say that threads and light weight processes are same. It’s just that thread is a term that is used at user level while light weight process is a term used at kernel level.” ** * Multithreaded Programming with JAVA™ Technology, Lewis/Berg ** What are Linux Processes, Threads, Light Weight Processes, and Process State CSS430 Operating Systems : Threads

Process Structure Kernel schedules each light-weight process separately. Traditional UNIX Process Multi-threaded Process Directory Entry UID GID EUID EGID Process ID TTY Directory Entry UID GID EUID EGID Process ID TTY Signal Dispatch Table Memory Map Signal Dispatch Table Memory Map LWP1 LWP2 1 2 3 stdin stdout stderr File Descriptors 1 2 3 stdin stdout stderr File Descriptors priority Intr. mask registers CPU Status CSS430 Operating Systems : Threads

Thread Structure Thread library runs in a user space. Process JVM Java Threads Thread library runs in a user space. All threads shared the same address space. Each thread maintains its own information and stack space. Once a thread is mapped to a LWP, it runs on a CPU. thread info Thread3 stack red zone thread info Thread2 stack red zone thread info Thread1 stack Thread1 Thread2 Thread3 red zone heap Thread3 code + global data Pthreads Win32 threads Thread to LWP mapping User mode Kernel mode LWP1 LWP2 CSS430 Operating Systems : Threads

Pthreads A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization API specifies behavior of the thread library, implementation is up to development of the library Common in UNIX operating systems (Solaris, Linux, Mac OS X) CSS430 Operating Systems : Threads

Pthreads (in C) CSS430 Operating Systems : Threads

Threads (Java) MyThread.java Compile and run ThreadFunc.java CSS430 Operating Systems : Threads

Java Threads Java threads may be created one of 2 ways: Extending thread class Implementing the Runnable interface CSS430 Operating Systems : Threads

Extending thread class Worker1.java First.java {Thread creation} CSS430 Operating Systems : Threads

Runnable Interface Worker2.java Second.java {Thread creation} CSS430 Operating Systems : Threads

Java Threads Producer-Consumer CSS430 Operating Systems : Threads

Java Threads Producer CSS430 Operating Systems : Threads

Java Threads Consumer CSS430 Operating Systems : Threads

Java Threads Utilities CSS430 Operating Systems : Threads

Java Thread Management setPriority( ) – changes this thread’s priority. sleep() – puts the currently running thread to sleep for a specified amount of time. suspend() – suspends execution of the currently running thread. resume() – resumes execution of a suspended thread. stop() – stops execution of a thread. For more information, visit: JAVA SE7 API THREAD CLASS Suspend, resume, and stop are currently deprecated in the specification. CSS430 Operating Systems : Threads

Thread Pools Key Benefits of TPs are: Servicing incoming request is faster with existing threads than creating new threads Thread Pools limit the total number of threads in the system. CSS430 Operating Systems : Threads

Thread Pools PrintTask (worker example) CSS430 Operating Systems : Threads

Thread Creation w/o Pools Driver (TaskCreator) CSS430 Operating Systems : Threads

Thread Creation using Pools Driver (TaskExecutor) * Better way to manage programs with many threads! CSS430 Operating Systems : Threads

Java Thread States CSS430 Operating Systems : Threads

Thread Life-cycle UML State Model New acquire lock, interrupt, I/O completes runnable enter synchronized statement notify notifyAll Interval expires Notify notifyAll task completes sleep wait issue I/O request wait waiting timed waiting terminated blocked CSS430 Operating Systems : Threads

Rewrite this program in Java. Exercises (no-turn in) build and run all code snippets provided in the slides. (turn-in) Exercise 4.6 - A Pthread program that performs the summation function was provided in Section 4.3.1. Rewrite this program in Java. ...for next Monday midnight deadline  Note: more homework will be added to Thursday night’s lecture on scheduling CSS430 Operating Systems : Threads