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

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.
國立台灣大學 資訊工程學系 Chapter 4: Threads. 資工系網媒所 NEWS 實驗室 Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the.
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
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.
Chapter 4: Threads. 2 What’s in a process? A process consists of (at least): –an address space –the code for the running program –the data for the running.
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: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Objectives To introduce a notion of a thread.
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.
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.
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: Threads. From Processes to Threads 4.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Threads.
Thread. A basic unit of CPU utilization. It comprises a thread ID, a program counter, a register set, and a stack. It is a single sequential flow of control.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Multithreaded Programming Overview.
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. 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.
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.
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.
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 ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads (7 th Edition)
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Lecture 5. Example for periority The average waiting time : = 41/5= 8.2.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Multithreaded Programming.
Threads Overview Benefits, User and Kernel Threads.
Introduction to threads
OPERATING SYSTEM CONCEPT AND PRACTISE
Chapter 4: Multithreaded Programming
Chapter 4: Threads.
Chapter 4: Threads.
Operating System Concepts
Chapter 4: Threads.
Chapter 4: Threads.
Modified by H. Schulzrinne 02/15/10 Chapter 4: Threads.
Multithreaded Programming
Chapter 4: Threads.
Presentation transcript:

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

4.2 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads Overview Multithreading Models Thread Libraries

4.3 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems To discuss the APIs for the Pthreads, Win32, and Java thread libraries

4.4 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition What’s in a process? A process consists of (at least): an address space the code for the running program the data for the running program an execution stack and stack pointer (SP)  traces state of procedure calls made the program counter (PC), indicating the next instruction a set of general-purpose processor registers and their values a set of OS resources  open files, network connections, sound channels, …

4.5 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Concurrency Imagine a web server, which might like to handle multiple requests concurrently While waiting for the credit card server to approve a purchase for one client, it could be retrieving the data requested by another client from disk, and assembling the response for a third client from cached information Imagine a web browser, which might like to initiate multiple requests concurrently While browser displays images or text, it retrieves data from the network. A word processor For example, displaying graphics, responding to keystrokes from the user, and performing spelling and grammar checking in the background.

4.6 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition What’s needed? In each of these examples of concurrency (web server, web browser, word processor): Everybody wants to run the same code Everybody wants to access the same data Everybody has the same privileges (most of the time) Everybody uses the same resources (open files, network connections, etc.) But you’d like to have multiple hardware execution states: an execution stack and stack pointer (SP)  traces state of procedure calls made the program counter (PC), indicating the next instruction a set of general-purpose processor registers and their values

4.7 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition How could we achieve this? Given the process abstraction as we know it: fork several processes This is really inefficient!! Resource intensive  ex: space: PCB, page tables, etc. Time consuming  creating OS structures, fork and copy address space, etc. So any support that the OS can give for doing multi-threaded programming is a win

4.8 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Single-Threaded Example Imagine the following C program: main() { ComputePI(“pi.txt”); PrintClassList(“clist.text”); } What is the behavior here? Program would never print out class list, because “ComputePI” would never finish.

4.9 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Use of Threads Version of program with Threads: main() { CreateThread(ComputePI(“pi.txt”)); CreateThread(PrintClassList(“clist.text”)); } What does “CreateThread” do? Start independent thread running for a given procedure What is the behavior here? Now, you would actually see the class list This should behave as if there are two separate CPUs

4.10 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Multithreaded server architecture clientserverthread (1)Request (3) Resume listening for additional client requests (2) Create new thread to service the request

4.11 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Threads and processes Most modern OS’s (NT, modern UNIX, etc) therefore support two entities: the process, which defines the address space and general process attributes (such as open files, etc.) the thread, which defines a sequential execution stream within a process A thread is a basic unit of CPU utilization; it comprises a thread ID, PC, a register set, and a stack. Shares with other threads belonging to the same process its code and data sections, and other OS resources (ex: open files and signals) Threads of the same process are not protected from each other.

4.12 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Single and Multithreaded Processes

4.13 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process address space code (text segment) static data (data segment) heap (dynamic allocated mem) thread 1 stack PC (T2) SP (T2) thread 2 stack thread 3 stack SP (T1) SP (T3) PC (T1) PC (T3) code (text segment) static data (data segment) heap (dynamic allocated mem) stack (dynamic allocated mem) PC SP

4.14 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Benefits of multithreaded Responsiveness: A multithreaded interactive application allows a program to continue running even if part of it is blocked or performing a lengthy operation. Thereby increasing responsiveness to the user. Resource Sharing (code, data, files) Threads share the memory and resources of the process to which they belong by default. Sharing data between threads is cheaper than processes  all see the same address space. Economy Creating and destroying threads is cheaper than processes. Context switching between threads is also cheaper. It’s much easier to communicate between threads. Scalability Multithreading can be greatly increased in a multiprocessor systems Threads may be running in parallel on different processors.

4.15 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Benefits of multithreaded Scalability Multithreading can be greatly increased in a multiprocessor systems Threads may be running in parallel on different processors.

4.16 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Multicore Programming On a single-core system, concurrency means that the execution of threads will be interleaved over time – executing only one thread at a time. Parallel execution for threads on a multi-core system.

4.17 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition User and Kernel Threads User threads: are visible to the programmer and unknown to the kernel. thread management done by user-level threads library, without kernel support. Kernel threads: Most OS kernels are multi-threaded. Several threads operate in the kernel, each performing a specific task. Ex: managing devices, interrupt handling. Supported and managed directly by the Kernel. Examples: Windows XP/2000, Solaris, Linux, Tru64 UNIX, Mac OS X. User-level threads are faster to create and manage than are kernel threads. Why?  Because no intervention from the kernel is required.

4.18 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Multithreading Models A relationship must exist between user threads and kernel threads, established by one of three ways:  Many-to-One  One-to-One  Many-to-Many

4.19 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Many-to-One Many user-level threads mapped to single kernel thread: Thread management is done by the thread library in user space  efficient. The entire process will block if a thread makes a blocking system call. Because only one thread can access the kernel at a time, multiple threads are unable to run in parallel on multiprocessors. Examples: Solaris Green Threads GNU Portable Threads

4.20 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition One-to-One Each user-level thread maps to kernel thread Adv : allows another thread to run when a thread makes a blocking system call  more concurrency. Adv : allows multiple threads to run in parallel on multiprocessors. Dis : creating a user thread requires creating corresponding kernel thread  can burden the applications performance. Examples Windows NT/XP/2000 Linux Solaris 9 and later

4.21 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Many-to-Many Model Allows many user level threads to be mapped to many kernel threads Does not suffer from the shortcomings of the previous two models. How? read P159 Solaris prior to version 9

4.22 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Two-level Model Similar to M:M, except that it allows a user thread to be bound to kernel thread Examples IRIX HP-UX Tru64 UNIX Solaris 8 and earlier

4.23 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Thread Libraries Thread library provides programmer with API for creating and managing threads Two primary ways of implementing a thread library: Library entirely in user space (all code and data structures for the library in user space)  Invoking a function in the API ->local function call in user space and not a system call. Kernel-level library supported by the OS (all code and data structures for the library in kernel space)  Invoking a function in the API -> system call to the kernel. Three primary thread libraries: POSIX Pthreads (maybe KL or UL), common in UNIX operating systems Win32 threads (KL), in Windows systems. Java threads (UL), in JVM.

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