Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Objectives Thread definitions and relationship to process Multithreading.

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

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
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.
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.
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.
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.
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.
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 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  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.
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.
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 ©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.
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.
OPERATING SYSTEM CONCEPT AND PRACTISE
Nadeem MajeedChoudhary.
Chapter 4: Threads.
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:

Chapter 4: Threads

4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Objectives Thread definitions and relationship to process Multithreading Models Threading Issues Some example thread libraries (Pthreads, Win32, Java)

4.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Thread Definitions A thread is a basic unit of CPU utilization A sequence of instructions enclosed in a function which CPU can execute as a unit A process is a program in execution A process is composed of one or more threads Each thread is comprised of (from OS perspective) Program counter Register set, and Stack Threads belonging to the same process share Code section Data section OS resources such as open files and signals

4.4 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Single and Multithreaded Processes Shared among threads

4.5 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Why Multithreading? Example multithreaded applications Web browsers: parallel downloads Web servers: handle multiple concurrent clients Word processors: spell check in the background …. Many others … Why multithreading? Responsiveness: one thread for GUI and another for processing Resource Sharing: similar requests handled by the same code and use same files/resources Economy: threads are much cheaper to create/delete than processes Utilization of multiprocessors: single threaded-process can NOT make use of multiple processors

4.6 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Multithreading Models Threads can be created and managed at two levels: User level threads  All data structures are maintained in the user level  All thread operations are performed in user mode  Kernel knows nothing about user-level threads  Provided by user-level libraries Kernel level threads  All data structures are maintained in the kernel space   All thread operations have to be in kernel mode (need system calls to perform them)  Provided by the OS (kernel) – Most modern OSs (e.g., XP, Linux, MaC OS X, Solaris) are multithreaded

4.7 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Multithreading Models (cont’d) In multithreaded kernels (i.e., most current OSs), mapping from user-level threads to kernel-level threads can be: Many-to-One One-to-One Many-to-Many

4.8 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Many-to-One Many user-level threads mapped to single kernel thread Thread management (create, delete, schedule, …) is done in user level Pros Managing user threads are faster (no system calls, no need to switch to kernel mode) Cons One thread blocks, the entire process blocks Can not use multiprocessors Examples (Libraries) Solaris Green Threads GNU Portable Threads

4.9 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts One-to-One Each user-level thread maps to a kernel thread Pros Increased concurrency (can use multiprocessors) A thread blocks, others can run Cons Creating too many kernel threads may degrade performance because they consume OS resources  may put limit on number of threads a user can create Examples: Windows NT/XP/2000, Linux, Solaris 9 and later

4.10 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Many-to-Many Model Multiple user threads are mapped to multiple kernel threads Mapping is NOT fixed, need thread scheduler (in user level) OS support: a user thread blocks, kernel notifies thread scheduler (upcall) to select another to use the free kernel thread Pros Increased Concurrency Flexible: user may create as many user threads as she wants, and kernel creates only a sufficient number of kernel threads Cons Complex to implement Examples: Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package

4.11 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Two-level Model Similar to Many-to-Many, except that it allows a user thread to be bound to kernel thread Bound thread can be used for critical operations or could be the thread scheduler Examples IRIX HP-UX Tru64 UNIX Solaris 8 and earlier

4.12 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Some Threading Issues Semantics of fork() and exec() system calls Signal handling Thread pools

4.13 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Semantics of fork() within threads A process has multiple thread. One of them called fork(). Should fork() duplicate only the calling thread or all threads (entire process)? It depends: If exec() is called immediately after fork(), no need to duplicate all threads (they will be overwritten anyway) Else, all threads should be duplicated

4.14 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Signal Handling Signals are used in UNIX to notify a process that an event has occurred Sequence: 1. Signal is generated by an event 2. Signal is delivered to a process 3. Signal handler processes the signal (default by OS, or user-defined) See example code in the textbook Should OS deliver a signal to: all threads, one thread, or specific thread of a process? It depends on the signal type Synchronous: a thread performed an operation that caused a signal to be generated (division by 0, illegal mem. access)  Deliver signal to the thread Asynchronous: external event generated to the signal (ctrl–c )  Deliver signal to all threads belonging to the process

4.15 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Thread Pools Create a number of threads in a pool where they await work E.g., web server serving many requests of the same page Advantages Usually faster to service a request with an existing thread than create a new one Limit number of threads in an application to pool size  Further requests are queued till a thread is free  important to maintain performance

4.16 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Example Thread Libraries POSIX Threads (Pthreads library) API specifications, implementation is up to the OS Common in UNIX systems: Solaris, Linux, Mac OS X Win 32 Threads Implementation of the one-to-one model in kernel Java Java threads are managed by JVM, which is run on top of an OS JVM specifies the interface, does NOT dictate the implementation JVM uses thread services provided by the host OS

4.17 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts A Note on Linux Threads Linux refers to threads and processes as tasks Thread creation is done through clone() system call clone() can be parameterized to allow a range of resource sharing possibilities: No sharing of resources between parent and child, the entire process is duplicated (same as fork()) Sharing of all resources between parent and child  Only pointers to shared resources are created for child Linux does not differentiate between process and thread (both are tasks). Is this good or bad? Good: simplify scheduling Bad: complex to correlate threads with their processes

4.18 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Summary A thread is a basic unit of CPU utilization, a process is composed of one or more threads Each thread has: Program counter, stack, registers Threads share: code, data, OS resources (open files and signals) User level threads vs. kernel threads Several mapping models from user threads to kernel threads M-to-1, 1-to-1, M-to-M Thread issues: fork(), signals, thread pools, scheduler activation Thread libraries: Pthreads, Win32, Java