Operating System Concepts Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.

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.
Chap 4 Multithreaded Programming. Thread A thread is a basic unit of CPU utilization It comprises a thread ID, a program counter, a register set and a.
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.
Chapter 4: Multithreaded Programming
Chapter 5 Threads os5.
Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)
Chapter 4: Multithreaded Programming
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.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java 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 ©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.
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.
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. 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.
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 5: THREADS ( 线程 ) Overview Overview Multithreading Models (多线程模型) Multithreading Models (多线程模型) Thread Implementation Issues (线程实现事项) Thread Implementation.
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.
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.
Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.
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.
Lecture 3 Threads Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
CS307 Operating Systems Threads Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2011.
Saurav Karmakar. Chapter 4: Threads  Overview  Multithreading Models  Thread Libraries  Threading Issues  Operating System Examples  Windows XP.
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.
Contents 1.Overview 2.Multithreading Model 3.Thread Libraries 4.Threading Issues 5.Operating-system Example 2 OS Lab Sun Suk Kim.
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
OPERATING SYSTEM CONCEPT AND PRACTISE
Nadeem MajeedChoudhary.
Chapter 4: Threads.
Operating System Concepts
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4:Threads Book: Operating System Principles , 9th Edition , Abraham Silberschatz, Peter Baer Galvin, Greg Gagne.
Presentation transcript:

Operating System Concepts Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

2Ku-Yaw ChangChapter 5 Threads A process With a single thread of control With a single thread of control With multiple threads of control With multiple threads of control Multithreaded computer systems Pthreads Solaris 2 threads Windows 2000 threads Linux threads Java threads

3Ku-Yaw ChangChapter 5 Threads 1.Overview 2.Multithreading Models 3.Threading Issues 4.Pthreads 5.Solaris 2 Threads 6.Windows 2000 Threads 7.Linux Threads 8.Java Threads 9.Summary 10.Exercises

4Ku-Yaw ChangChapter 5 Threads 5.1 Overview A thread A basic unit of CPU utilization A basic unit of CPU utilization A thread ID A program counter A register set A stack Called a lightweight process (LWP) Called a lightweight process (LWP) A traditional process has a single thread of control, called a heavyweight process (HWP)

5Ku-Yaw ChangChapter 5 Threads Single- and Multithreaded processes

6Ku-Yaw ChangChapter 5 Threads Motivation A web browser One thread display images or text One thread display images or text Another thread retrieves data from the network Another thread retrieves data from the network A word processor One thread for displaying graphics One thread for displaying graphics Another thread for reading keystrokes Another thread for reading keystrokes Third thread for performing spelling and grammar checking Third thread for performing spelling and grammar checking Process creation very heavyweight. If new process will perform the same tasks as the existing process, why incur all that overhead? If new process will perform the same tasks as the existing process, why incur all that overhead?

7Ku-Yaw ChangChapter 5 Threads Benefits Responsiveness Allow a program to continue even if part of it is blocked or is performing a lengthy operation Allow a program to continue even if part of it is blocked or is performing a lengthy operation Resource sharing Memory Memory Different threads all within the same address space Resources ResourcesEconomy More economical to create and context switch threads More economical to create and context switch threads Utilization of multiprocessor architectures Increase concurrency Increase concurrency

8Ku-Yaw ChangChapter 5 Threads User and Kernel Threads User threads Implemented by a thread library at the user level Implemented by a thread library at the user level Supported above the kernel Advantage Advantage Fast to create and manage Disadvantage Disadvantage A thread may cause the entire process to block Examples Examples POSIX Pthreads Mach C-threads Solaris 2 UI-threads

9Ku-Yaw ChangChapter 5 Threads User and Kernel Threads Kernel threads The kernel performs thread creation, scheduling, and management The kernel performs thread creation, scheduling, and management Supported directly by OS Advantage Advantage Kernel can schedule another thread when a thread is blocked Disadvantage Disadvantage Slow to create and manage Examples Examples Windows NT/2000 Solaris 2 BeOS Tru64 Unix

10Ku-Yaw ChangChapter 5 Threads User and Kernel Threads Java threads Created and managed by the Java virtual machine (JVM) Created and managed by the Java virtual machine (JVM) Do not fall under the realm of either user or kernel threads

11Ku-Yaw ChangChapter 5 Threads 1.Overview 2.Multithreading Models 3.Threading Issues 4.Pthreads 5.Solaris 2 Threads 6.Windows 2000 Threads 7.Linux Threads 8.Java Threads 9.Summary 10.Exercises

12Ku-Yaw ChangChapter 5 Threads 5.2 Multithreading Models Many systems provide support for both user and kernel threads Different multithreading models Different multithreading models Many-to-one model One-to-one model Many-to-many model

13Ku-Yaw ChangChapter 5 Threads Many-to-One Model Many user-level threads mapped to one kernel thread Advantage Efficient EfficientDisadvantage Entire process may block if a thread makes a blocking system call Entire process may block if a thread makes a blocking system call Unable to run in parallel on multiprocessors Unable to run in parallel on multiprocessors Used on systems that do not support kernel threads Green threads – a thread library available for Solaris 2 Green threads – a thread library available for Solaris 2

14Ku-Yaw ChangChapter 5 Threads Many-to-One Model

15Ku-Yaw ChangChapter 5 Threads One-to-One Model Each user thread mapped to one kernel thread Advantage More concurrency More concurrencyDisadvantage Overhead of creating kernel threads can burden the performance of an application Overhead of creating kernel threads can burden the performance of an application Restrict the number of threads Example Windows NT/2000 and OS/2 Windows NT/2000 and OS/2

16Ku-Yaw ChangChapter 5 Threads One-to-One Model

17Ku-Yaw ChangChapter 5 Threads Many-to-Many Model Multiplex many user-level threads to a small or equal number of kernel threads Advantage Developers can create as many as user threads as wish Developers can create as many as user threads as wish More concurrency More concurrencyExample Solaris 2 Solaris 2 Windows NT/2000 with the ThreadFiber package Windows NT/2000 with the ThreadFiber package IRIX IRIX HP-UX HP-UX Tru64 Unix Tru64 Unix

18Ku-Yaw ChangChapter 5 Threads Many-to-Many Model

19Ku-Yaw ChangChapter 5 Threads 1.Overview 2.Multithreading Models 3.Threading Issues 4.Pthreads 5.Solaris 2 Threads 6.Windows 2000 Threads 7.Linux Threads 8.Java Threads 9.Summary 10.Exercises

20Ku-Yaw ChangChapter 5 Threads 5.3 Threading Issues The fork and exec System Calls Cancellation Signal Handling Thread Pools Thread-Specific Data

21Ku-Yaw ChangChapter 5 Threads The fork and exec System Calls One thread in a program calls fork New process duplicate all threads New process duplicate all threads New process is single threaded New process is single threaded The exec system call works in the same way as described before. Some systems have two versions of fork If exec is called immediately, then duplicating all threads is unnecessary. If exec is called immediately, then duplicating all threads is unnecessary. If not, the separate process should duplicate all threads. If not, the separate process should duplicate all threads.

22Ku-Yaw ChangChapter 5 Threads Cancellation Thread cancellation The task of terminating a thread before it has completed The task of terminating a thread before it has completed Target thread Target thread The thread that is to be cancelled Two different scenarios Asynchronous cancellation Asynchronous cancellation One thread immediately terminates the target threads. Deferred cancellation Deferred cancellation The target thread periodically checks if it is should terminate, allowing itself an opportunity to terminate itself in an orderly fashion. Cancellation points (Pthreads)

23Ku-Yaw ChangChapter 5 Threads Signal Handling A signal Used in Unix systems Used in Unix systems Notify a process that a particular event has occurred Notify a process that a particular event has occurred May be received either synchronously or asynchronously May be received either synchronously or asynchronously All signals follow the same pattern A signal is generated by the occurrence of a particular event. A signal is generated by the occurrence of a particular event. A generated signal is delivered to a process. A generated signal is delivered to a process. Once delivered, the signal must be handled. Once delivered, the signal must be handled.

24Ku-Yaw ChangChapter 5 Threads Signal Handling Synchronous signal An illegal memory access or divided by zero An illegal memory access or divided by zero Asynchronous signal Terminating a process with specific keystrokes ( ) Terminating a process with specific keystrokes ( ) Signal may be handled by A default signal handler A default signal handler Every signal has a default signal handler run by the kernel A user-defined signal handler A user-defined signal handler Override the default signal handler

25Ku-Yaw ChangChapter 5 Threads Signal Handling Delivering signals is more complicated in multithreaded programs Deliver the signal to the thread to which the signal applies Deliver the signal to the thread to which the signal applies Deliver the signal to every thread in the process Deliver the signal to every thread in the process Deliver the signal to certain threads in the process Deliver the signal to certain threads in the process Assign a specific thread to receive all signals for the process Assign a specific thread to receive all signals for the process

26Ku-Yaw ChangChapter 5 Threads Signal Handling Windows 2000 Does not explicitly support signals Does not explicitly support signals Emulation using Asynchronous Procedure Calls (APCs) Emulation using Asynchronous Procedure Calls (APCs) Allow a thread to specify a function that is to be called when the thread receives notification of a particular event

27Ku-Yaw ChangChapter 5 Threads Thread Pools Potential problems Amount of time required to create the thread prior to servicing the request Amount of time required to create the thread prior to servicing the request Unlimited threads could exhaust the system resources Unlimited threads could exhaust the system resources Thread pool To create a number of threads at process startup and place them in a pool To create a number of threads at process startup and place them in a pool Sit and wait for work A server receives a request, a thread is awaken from the pool A server receives a request, a thread is awaken from the pool Once the thread completes its work, it returns to the pool Once the thread completes its work, it returns to the pool If the pools contains no available thread, the server waits until one become free If the pools contains no available thread, the server waits until one become free

28Ku-Yaw ChangChapter 5 Threads Thread-Specific Data Threads belonging to a process share the data of the process. One of the benefits of multithreaded programming One of the benefits of multithreaded programming Each thread also needs its own copy of certain data – called thread specific data

29Ku-Yaw ChangChapter 5 Threads 1.Overview 2.Multithreading Models 3.Threading Issues 4.Pthreads 5.Solaris 2 Threads 6.Windows 2000 Threads 7.Linux Threads 8.Java Threads 9.Summary 10.Exercises

30Ku-Yaw ChangChapter 5 Threads 5.4 Pthreads a POSIX standard (IEEE c) API for thread creation and synchronization A specification for thread behavior, not an implementation A specification for thread behavior, not an implementation Common in UNIX-based systems Common in UNIX-based systems Generally not supported in Windows systems Generally not supported in Windows systems

31Ku-Yaw ChangChapter 5 Threads 1.Overview 2.Multithreading Models 3.Threading Issues 4.Pthreads 5.Solaris 2 Threads 6.Windows 2000 Threads 7.Linux Threads 8.Java Threads 9.Summary 10.Exercises

32Ku-Yaw ChangChapter 5 Threads 5.5 Solaris 2 Threads Read it by yourself P. 141 to 143 P. 141 to 143

33Ku-Yaw ChangChapter 5 Threads 1.Overview 2.Multithreading Models 3.Threading Issues 4.Pthreads 5.Solaris 2 Threads 6.Windows 2000 Threads 7.Linux Threads 8.Java Threads 9.Summary 10.Exercises

34Ku-Yaw ChangChapter 5 Threads 5.6 Windows 2000 Threads Win32 API Primary API for the family of MS OSs Primary API for the family of MS OSs Provide one-to-one mapping model A fiber library provides the many-to-many model A fiber library provides the many-to-many model Primary data structures ETHREAD (executive thread block) ETHREAD (executive thread block) In kernel space KTHREAD (kernel thread block) KTHREAD (kernel thread block) In kernel space TEB (Thread environment block) TEB (Thread environment block) User-space data structure

35Ku-Yaw ChangChapter 5 Threads 1.Overview 2.Multithreading Models 3.Threading Issues 4.Pthreads 5.Solaris 2 Threads 6.Windows 2000 Threads 7.Linux Threads 8.Java Threads 9.Summary 10.Exercises

36Ku-Yaw ChangChapter 5 Threads 5.7 Linux Threads Read it by yourself P. 144 to 145 P. 144 to 145

37Ku-Yaw ChangChapter 5 Threads 1.Overview 2.Multithreading Models 3.Threading Issues 4.Pthreads 5.Solaris 2 Threads 6.Windows 2000 Threads 7.Linux Threads 8.Java Threads 9.Summary 10.Exercises

38Ku-Yaw ChangChapter 5 Threads 5.8 Java Threads Java Support threads at the language level Support threads at the language level For the creation and management of threads Threads are managed by JVM Threads are managed by JVM Not by a user-level library or kernel A Java program A Java program Only a main method runs as a single thread in JVM

39Ku-Yaw ChangChapter 5 Threads Thread Creation Thread creation To create a new class that derived from the Thread class To create a new class that derived from the Thread class Override the run method of the Thread class Calling the start method actually creates the new thread Calling the start method actually creates the new thread

40Ku-Yaw ChangChapter 5 Threads Java Thread States

41Ku-Yaw ChangChapter 5 Threads The JVM and the Host Operation System JVM does not indicate how Java thread are to be mapped to the underlying OS Leaving the decision to the particular implementation of the JVM Leaving the decision to the particular implementation of the JVM Windows use one-to-one model

42Ku-Yaw ChangChapter 5 Threads 1.Overview 2.Multithreading Models 3.Threading Issues 4.Pthreads 5.Solaris 2 Threads 6.Windows 2000 Threads 7.Linux Threads 8.Java Threads 9.Summary 10.Exercises

43Ku-Yaw ChangChapter 5 Threads Summary P.147

44Ku-Yaw ChangChapter 5 Threads 1.Overview 2.Multithreading Models 3.Threading Issues 4.Pthreads 5.Solaris 2 Threads 6.Windows 2000 Threads 7.Linux Threads 8.Java Threads 9.Summary 10.Exercises

45Ku-Yaw ChangChapter 5 Threads Exercises

The End