ThreadsCS-3013 C-term 20081 Threads (continued) CS-3013, Operating Systems C-Term 2008 (Slides include materials from Operating System Concepts, 7 th ed.,

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: 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.
4.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Chapter 4 Multithreaded Programming Objectives Objectives To introduce a notion of.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
ThreadsCS-502 Fall Threads CS-502 Operating Systems (Slides include materials from Operating System Concepts, 7 th ed., by Silbershatz, Galvin, &
ThreadsCS-3013 C-term Threads CS-3013, Operating Systems C-Term 2008 (Slides include materials from Operating System Concepts, 7 th ed., by Silbershatz,
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.
ThreadsCS-3013 A-term Threads CS-3013, Operating Systems A-term 2009 (Slides include materials from Modern Operating Systems, 3 rd ed., by Andrew.
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: 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.
CS 3013 & CS 502 Summer 2006 Threads1 CS-3013 & CS-502 Summer 2006.
Chapter 4: Threads READ 4.1 & 4.2 NOT RESPONSIBLE FOR 4.3 &
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.
國立台灣大學 資訊工程學系 Chapter 4: Threads. 資工系網媒所 NEWS 實驗室 Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the.
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.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
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.
Shan Gao Fall 2007 Department of Computer Science Georgia State University.
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.
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.
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.
Operating System Concepts
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.
CS-502, Operating Systems Fall 2007
Nadeem MajeedChoudhary.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Modified by H. Schulzrinne 02/15/10 Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
CS-3013 Operating Systems Hugh C. Lauer
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Presentation transcript:

ThreadsCS-3013 C-term Threads (continued) CS-3013, Operating Systems C-Term 2008 (Slides include materials from Operating System Concepts, 7 th ed., by Silbershatz, Galvin, & Gagne and from Modern Operating Systems, 2 nd ed., by Tanenbaum)

ThreadsCS-3013 C-term Review Threads introduced because Processes are heavyweight in Windows and Linux Difficult to develop concurrent applications when address space is unique per process Thread — a particular execution of a program within the context of a Windows- Unix-Linux process Multiple threads in same address space at same time

ThreadsCS-3013 C-term This problem … … is partly an artifact of Unix, Linux, and Windows and of Big, powerful processors (e.g., Pentium, Athlon) … tends to occur in most large systems … is infrequent in small-scale systems PDAs, cell phones Closed systems (i.e., controlled applications)

ThreadsCS-3013 C-term Characteristics A thread has its own Program counter, registers, PSW Stack A thread shares Address space, heap, static data, program code Files, privileges, all other resources with all other threads of the same process

ThreadsCS-3013 C-term Address Space for Multiple Threads 0x xFFFFFFFF Virtual address space code (text) static data heap thread 1 stack PC (T2) SP (T2) thread 2 stack thread 3 stack SP (T1) SP (T3) PC (T1) PC (T3) SP PC

ThreadsCS-3013 C-term Who creates and manages threads? User-level implementation –done with function library (e.g., POSIX) –Runtime system – similar to process management except in user space –Windows NT – fibers: a user-level thread mechanism ??? Kernel implementation – new system calls and new entity to manage –Linux: lightweight process (LWP) –Windows NT & XP: threads

ThreadsCS-3013 C-term Mutual Exclusion within Threads extern void thread_yield(); extern int TestAndSet(int &i); /* sets the value of i to 1 and returns the previous value of i. */ void enter_critical_region(int &lock) { while (TestAndSet(lock) == 1) thread_yield(); /* give up processor */ }; void leave_critical_region(int &lock) { lock = 0; };

ThreadsCS-3013 C-term Reading Assignment Silbertshatz –Chapter 4 – “Threads” Robert Love, Linux Kernel Development –Chapter 3 – “Process Management”

ThreadsCS-3013 C-term Kernel Threads Supported by the Kernel OS maintains data structures for thread state and does all of the work of thread implementation. Examples Solaris Tru64 UNIX Mac OS X Windows 2000/XP/Vista Linux version 2.6

ThreadsCS-3013 C-term Kernel Threads (continued) OS schedules threads instead of processes Benefits –Overlap I/O and computing in a process –Creation is cheaper than processes –Context switch can be faster than processes Negatives –System calls (high overhead) for operations –Additional OS data space for each thread

ThreadsCS-3013 C-term Threads – supported by processor E.g., Pentium 4 with Hyperthreading™ Multiple processor cores on a single chip True concurrent execution within a single process Requires kernel thread support Re-opens old issues Deadlock detection Critical section management of synchronization primitives (especially in OS kernel)

ThreadsCS-3013 C-term Unix Processes vs. Threads On a 700 Mhz Pentium running Linux –Processes: fork()/exit() : 250 microsec –Kernel threads: pthread_create() / pthread_join() : 90 microsec –User-level threads: pthread_create() / pthread_join() : 5 microsec

ThreadsCS-3013 C-term POSIX pthread Interface Data type:– pthread_t int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void*(*start_routine) (void), void *arg) ; –creates a new thread of control –new thread begins executing at start_routine pthread_exit(void *value_ptr) –terminates the calling thread pthread_join(pthread_t thread, void **value_ptr); –blocks the calling thread until the thread specified terminates pthread_t pthread_self() –Returns the calling thread's identifier

ThreadsCS-3013 C-term Threads and “small” operating systems Many “small” operating systems provide a common address space for all concurrent activities Each concurrent execution is like a Linux- Unix thread But its often called a process! pthread interface and tools frequently used for managing these processes …

ThreadsCS-3013 C-term Java Threads Thread class Thread worker = new Thread(); Configure it Run it public class application extends Thread { … } Methods to Run, wait, exit Cancel, join Synchronize …

ThreadsCS-3013 C-term Threading Issues Semantics of fork() and exec() system calls for processes Thread cancellation Signal handling Kernel thread implementations –Thread pools –Thread specific data –Scheduler activations

ThreadsCS-3013 C-term Semantics of fork() and exec() Does fork() duplicate only the calling thread or all threads? –Easy if user-level threads –Not so easy with kernel-level threads Linux has special clone() operation – only forking thread is created in child process Windows XP has something similar

ThreadsCS-3013 C-term Thread Cancellation Terminating a thread before it has finished Reason:– Some other thread may have completed the joint task E.g., searching a database Issue:– Other threads may be depending cancelled thread for resources, synchronization, etc. May not be able to cancel one until all can be cancelled

ThreadsCS-3013 C-term Thread Cancellation (continued) Two general approaches: –Asynchronous cancellation terminates the target thread immediately –Deferred cancellation allows the target thread to periodically check if it should cancel itself pthreads provides cancellation points

ThreadsCS-3013 C-term Signal Handling Signals are used in Unix-Linux to notify process that a particular event has occurred — e.g. –Divide-by-zero –Illegal memory access, stack overflow, etc. –CTL-C typed, or kill command issued at console –Timer expiration; external alarm –… A signal handler is used to process signals –Signal is generated by particular event –Signal is delivered to a process –Signal is handled by a signal handler All processes provided with default signal handler Applications may install own handlers for specific signals

ThreadsCS-3013 C-term Signal Handling Options Deliver signal to specific thread to which it applies E.g., illegal memory access, divide-by-zero, etc. Deliver signal to every thread in the process CTL-C typed Deliver signal to certain threads in the process I.e., threads that have agreed to receive such signals (or not blocked them) Assign a specific thread to receive all signals for the process …

ThreadsCS-3013 C-term Kernel Thread Implementations Linux Windows Others

ThreadsCS-3013 C-term Modern Linux Thread Implementation Implemented directly in kernel Primary unit of scheduling and computation implemented by Linux 2.6 kernel “A thread is just a special kind of process.” Robert Love, Linux Kernel Development, p.23 Every thread has its own task_struct in kernel …

ThreadsCS-3013 C-term Definition Task (from point of view of Linux kernel):– –Process –Thread –Kernel thread (see later)

ThreadsCS-3013 C-term Modern Linux Threads (continued) Process task_struct has pointer to own memory & resources Thread task_struct has pointer to process’s memory & resources Kernel thread task_struct has null pointer to memory & resources fork() and pt hread_create() are library functions that invoke clone() system call Arguments specify what kind of clone …

ThreadsCS-3013 C-term Modern Linux Threads (continued) Threads are scheduled independently of each other Threads can block independently of each other Even threads of same process Threads can make their own kernel calls Kernel maintains a small kernel stack per thread During kernel call, kernel is in process context

ThreadsCS-3013 C-term Process Context 0x xFFFFFFFF Virtual address space code (text) static data heap (dynamically allocated ) Kernel Code and Data PC SP1 User Space stack (dynamically allocated ) Kernel Space 32-bit Linux & Win XP – 3G/1G user space/kernel space stack (dynamically allocated ) SP2

ThreadsCS-3013 C-term Modern Linux Threads (continued) Multiple threads can be executing in kernel at same time When in process context, kernel can sleep on behalf of its thread take pages faults on behalf of its thread move data between kernel and process address space on behalf of thread …

ThreadsCS-3013 C-term Linux Kernel Threads Kernel has its own threads No associated process context Supports concurrent activity within kernel Multiple devices operating at one time Multiple application activities at one time Multiple processors in kernel at one time A useful tool Special kernel thread packages, synchronization primitives, etc. Useful for complex OS environments

ThreadsCS-3013 C-term Windows XP Threads Much like to Linux 2.6 threads Primitive unit of scheduling defined by kernel Threads can block independently of each other Threads can make kernel calls … Process is a higher level (non-kernel) abstraction See Silbershatz, §

ThreadsCS-3013 C-term Other Implementations of Kernel Threads Many-to-Many Many-to-One One-to-One

ThreadsCS-3013 C-term Thread Pools (Implementation technique) 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 application(s) to be bounded by size of pool

ThreadsCS-3013 C-term Many-to-Many Model Allows many user level threads to be mapped to many kernel threads Allows the operating system to create a sufficient number of kernel threads Solaris prior to version 9 Windows NT/2000 with the Thread/Fiber package

ThreadsCS-3013 C-term Many-to-Many Model

ThreadsCS-3013 C-term Two-level Model Similar to M:M, except it allows a specific thread to be bound to one kernel thread Examples IRIX HP-UX Tru64 UNIX Solaris 8 and earlier

ThreadsCS-3013 C-term Two-level Model

ThreadsCS-3013 C-term Many-to-One Many user-level threads mapped to single kernel thread Examples: Solaris Green Threads GNU Portable Threads

ThreadsCS-3013 C-term Many-to-One Model

ThreadsCS-3013 C-term One-to-One Each user-level thread maps to kernel thread Examples Windows NT/XP/2000 Linux Solaris 9 and later

ThreadsCS-3013 C-term One-to-one Model

ThreadsCS-3013 C-term Threads – Summary Threads were invented to counteract the heavyweight nature of Processes in Unix, Windows, etc. Provide lightweight concurrency within a single address space Have evolved to become primitive abstraction defined by kernel Fundamental unit of scheduling in Linux, Windows, etc

ThreadsCS-3013 C-term Reading Assignment Silbertshatz –Chapter 4 – “Threads” Robert Love, Linux Kernel Development –Chapter 3 – “Process Management”

ThreadsCS-3013 C-term Questions?