14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.

Slides:



Advertisements
Similar presentations
OPERATING SYSTEMS Threads
Advertisements

Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 AE4B33OSS Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows.
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.
國立台灣大學 資訊工程學系 Chapter 4: Threads. 資工系網媒所 NEWS 實驗室 Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the.
Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 6: Threads Chapter 4.
Chapter 4: Multithreaded Programming
Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 TY, Sept 2011.
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.
4.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Chapter 4 Multithreaded Programming Objectives Objectives To introduce a notion of.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 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.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Objectives Thread definitions and relationship to process Multithreading.
02/02/2004CSCI 315 Operating Systems Design1 Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
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.
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.
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. 資工系網媒所 NEWS 實驗室 Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Chapter 4: Multithreaded Programming. 4.2 Multithreaded Programming n Overview n Multithreading Models n Thread Libraries n Threading Issues n Operating.
Chapter 4: Threads. From Processes to Threads 4.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Threads.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 T Yang, Sept 2012.
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 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  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 Modified from the slides of the text book. TY, Sept 2010.
Chapter 4: Threads.
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.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Outline n Overview n Multithreading.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Lecture 3 Threads Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Saurav Karmakar. Chapter 4: Threads  Overview  Multithreading Models  Thread Libraries  Threading Issues  Operating System Examples  Windows XP.
Operating System Concepts
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads (7 th Edition)
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.
OPERATING SYSTEM CONCEPT AND PRACTISE
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Presentation transcript:

14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads

14.2 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Announcements Project -2 deadline extension? – 09/26 10% bonus who submit before 09/24 11:59 PM Project-3 will be uploaded by 09/27 Multithreaded server using sockets Midterm exam – 10/03 or 10/08 In class exam, closed book, 1 cheat sheet allowed Signup for homework presentations

14.3 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition User-level – Kernel-level Thread Mapping Many-to-One One-to-One Many-to-Many

14.4 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Many-to-One Many user-level threads mapped to single kernel thread Thread management done by user-level library Entire process blocks if a thread makes blocking system call Not suitable for multi-processor environment Examples: Solaris Green Threads GNU Portable Threads

14.5 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Many-to-One Model

14.6 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition One-to-One One user-level thread mapped to single kernel thread Provides for more concurrency Suitable for multi-processor systems Drawback: Thread creation requires kernel intervention Systems restrict number of threads Examples: Linux Threads WIndowsThreads

14.7 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition One-to-one Model

14.8 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Many – to - Many Multiplexes many user-level threads to equal or smaller number of kernel threads Number of kernel threads may be specific to application or system Does not suffer from drawbacks of one-to- one or many-to-one models No restriction on number of threads user can create Example: Windows NT/2000 with ThreadsFiber package

14.9 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Many-to-Many Model

14.10 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Two-Level Model Variant of Many-to-Many model Multiplexes user-level threads to equal or smaller number of kernel threads Also allows a user-level thread to bind to a kernel-level thread Examples: IRIX HP-UX Tru64 UNIX Solaris 8 and earlier

14.11 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Two-level Model

14.12 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Thread Libraries Thread library provides programmer with API for creating and managing threads Library entirely in user space Code and data structures entirely in user space Kernel-level library supported by the OS Code and data structures in kernel space Invoking functions will cause system call Pthreads – Can be either user or system-level Win32 Threads – Kernel level Java Threads – Depends upon the host OS

14.13 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Pthreads May be provided either as user-level or kernel- level A POSIX standard (IEEE c) API for thread creation and synchronization – not an actual implementation 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)

14.14 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Pthreads (Contd.) pthread_t – thread identifier pthread_attr_t – thread attributes pthread_create – function to create threads Need to specify a start routine The routine can take a single parameter (pointer) Global variables shared among all threads pthread_wait() – wait for thread to complete pthread_exit() – Thread terminates itself

14.15 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Java Threads Java threads are managed by the JVM Java two mechanisms for creating threads Create a new class derived from Thread class overriding the run() method Implementing the Runnable interface Must define the run() method Code provided in the run() method executes as new thread

14.16 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Java Threads (Contd.) Code that the new thread executes provided in run() method of class implementing runnable Creating a thread – start() method Note that creating a thread object does not create the thread Allocates memory for new thread Calls run() method of the class implementing runnable Data sharing via passing object references join () for waiting on threads (equivalent to pthreads_join())

14.17 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Java Threads - Example Program

14.18 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Java Threads - Example Program

14.19 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Java Thread States

14.20 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Threading Issues Semantics of fork() and exec() system calls Thread cancellation of target thread Asynchronous or deferred Signal handling Thread pools Thread-specific data Scheduler activations

14.21 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Semantics of fork() and exec() What happens if a thread calls fork() Duplicate only the calling thread or all threads? Some Unix systems provide two types of fork() exec() works in a similar fashion as discussed before If exec() called right after forking, no need to duplicate all threads If exec() not called after forking all threads need to be duplicated

14.22 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Thread Cancellation Terminating a thread before it has finished Why termination is needed? Asynchronous cancellation Another thread immediately terminates target thread Drawback: Cancelled thread might be holding some system resources or updating shared data Deferred cancellation Target thread periodically checks if it should be cancelled Cancellation points in Pthreads Provides opportunity for cancelled thread to cleanup

14.23 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Java Thread Cancellation Asynchronous termination via stop() method – deprecated interrupt() method for deferred cancellation Sets the interruption status Threads can periodically check their interruption status using interrupted or isInterrupted() methods Both are defined in the Thread class currentThread() to obtain the thread object corresponding to the thread that is currently running

14.24 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Signal Handling Signals are used in to notify a process that a particular event has occurred. A signal handler is used to process signals.  Signal is generated by particular event  Signal is delivered to a process  Signal is handled Two types of signals Synchronous signals Asynchronous signals

14.25 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Signal Handling (Contd.) Options: Deliver the signal to the thread to which the signal applies Deliver the signal to every thread in the process Deliver the signal to certain threads in the process Assign a specific thread to receive all signals for the process Unix systems allow threads to specify the signals it will block and signals it will process Deliver signal to any thread that does not block it

14.26 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Thread Pools On-demand thread creation has inherent problems Create a number of threads in a pool where they await work. Advantages: Usually slightly faster to service a request with an existing thread than create a new thread. Allows the number of threads in the application(s) to be bound to the size of the pool.

14.27 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition End of Chapter 4

14.28 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling

14.29 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Thread Scheduling Multiple-Processor Scheduling Operating Systems Examples Algorithm Evaluation

14.30 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Objectives To introduce CPU scheduling, which is the basis for multiprogrammed operating systems To describe various CPU-scheduling algorithms To discuss evaluation criteria for selecting a CPU-scheduling algorithm for a particular system

14.31 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Basic Concepts Maximum CPU utilization obtained with multiprogramming Not a good idea to “idle” the CPU Jobs typically do I/O to get the data they need to process Process will not actively utilize CPU when doing I/O CPU–I/O Burst Cycle – Cycle of CPU execution and I/O wait

14.32 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Alternating Sequence of CPU And I/O Bursts

14.33 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition CPU Burst Distribution Processes alternate between CPU burst and I/O burst Process begins with a CPU burst and ends with a CPU burst Duration of CPU and I/O bursts vary widely CPU-bound jobs have relatively long CPU- bursts I/O bound jobs have shorter CPU bursts

14.34 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Histogram of CPU-burst Times

14.35 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition CPU Scheduler Selects from among the processes in ready queue and allocates the CPU to one of them CPU scheduling decisions may take place when a process: 1.Switches from running to waiting state (e.g., I/O) 2.Switches from running to ready state (e.g., interrupt) 3.Switches from waiting to ready  Terminates Scheduling under 1 and 4 is nonpreemptive All other scheduling is preemptive

14.36 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Dispatcher Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves: switching context switching to user mode jumping to the proper location in the user program to restart that program Dispatch latency – time it takes for the dispatcher to stop one process and start another running

14.37 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Scheduling Criteria CPU utilization – %age of time CPU is busy Throughput – # of processes that complete their execution per time unit Turnaround time – amount of time to execute a particular process Waiting time – amount of time a process has been waiting in the ready queue Response time – amount of time it takes from when a request was submitted until the first response is produced, not output (for time-sharing environment)

14.38 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Scheduling Algorithm Optimization Criteria Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time