CS-502, Operating Systems Fall 2007

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.
Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 6: Threads Chapter 4.
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,
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.
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.
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.
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.
ThreadsCS-3013 C-term Threads (continued) CS-3013, Operating Systems C-Term 2008 (Slides include materials from Operating System Concepts, 7 th ed.,
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.
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.
Introduction to threads
OPERATING SYSTEM CONCEPT AND PRACTISE
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Multithreaded Programming
Chapter 4: Threads 羅習五.
Nadeem MajeedChoudhary.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
Chapter 4: Threads.
OPERATING SYSTEMS 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.
Chapter 4: Threads.
Presentation transcript:

CS-502, Operating Systems Fall 2007 Threads CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System Concepts, 7th ed., by Silbershatz, Galvin, & Gagne and from Modern Operating Systems, 2nd ed., by Tanenbaum) Assumptions: Graduate level Operating Systems Making Choices about operation systems Why a micro-century? …just about enough time for one concept CS-502 Fall 2007 Threads

Problem Processes in Unix, Linux, and Windows are “heavyweight” OS-centric view – performance Application-centric view – flexibility and application design CS-502 Fall 2007 Threads

OS-centric View of Problem Lots of data in PCB & other data structures Even more when we study memory management More than that when we study file systems, etc. Processor caches a lot of information Memory Management information Caches of active pages Costly context switches and traps 100’s of microseconds CS-502 Fall 2007 Threads

Application-centric View of Problem Separate processes have separate address spaces Shared memory is limited or nonexistent Applications with internal concurrency are difficult Isolation between independent processes vs. cooperating activities Fundamentally different goals CS-502 Fall 2007 Threads

Example Web Server – How to support multiple concurrent requests One solution: create several processes that execute in parallel Use shared memory (shmget()) to map to the same address space in the processes have the OS schedule them in parallel Clumsy and inefficient Space and time: PCB, page tables, cloning entire process, etc. programming: shmget()is really hard to program! CS-502 Fall 2007 Threads

Example 2 Transaction processing systems E.g, airline reservations or bank ATM transactions 1000’s of transactions per second Very small computation per transaction Separate processes per transaction are too costly Other techniques (e.g., message passing) are much more complex CS-502 Fall 2007 Threads

Example 3 Games have multiple active characters Independent behaviors Common context or environment Need “real-time” response to user For interactive gaming experience Programming all characters in separate processes is really, really hard! Programming them in a single process is much harder with some assistance. CS-502 Fall 2007 Threads

This problem … … is partly an artifact of Unix, Linux, and Windows and of Big, powerful processors (e.g., Pentium 4) … tends to occur in most large systems … is infrequent in small-scale systems PDAs, cell phones Closed systems (i.e., controlled applications) CS-502 Fall 2007 Threads

Solution:– Threads A thread is a particular execution of a program or procedure within the context of a Unix or Windows process I.e., a specialization of the concept of process 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 CS-502 Fall 2007 Threads

Threads thread 1 stack SP (T1) 0xFFFFFFFF thread 2 stack SP SP (T2) Virtual address space heap static data 0x00000000 PC code (text) PC (T2) PC (T1) PC (T3) CS-502 Fall 2007 Threads

Single and Multithreaded Processes CS-502 Fall 2007 Threads

Benefits Responsiveness Resource Sharing Economy Utilization of multi-processor architectures CS-502 Fall 2007 Threads

Threads Unix, Windows, and older versions of Linux have their own thread interfaces Similar, not standardized Some issues E.g., ERRNO in Unix — a static variable set by system calls Semantics of fork() and exec() CS-502 Fall 2007 Threads

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 CS-502 Fall 2007 Threads

User Threads Thread management done by user-level threads library Three primary thread libraries: – POSIX Pthreads Win32 threads Java threads CS-502 Fall 2007 Threads

User Threads (continued) Can be implemented without kernel support … or knowledge! Program links with a runtime system that does thread management Operation are very efficient (procedure calls) Space efficient and all in user space (TCB) Task switching is very fast Since kernel not aware of threads, there can be scheduling inefficiencies E.g., blocking I/O calls Non-concurrency of threads on multiple processors CS-502 Fall 2007 Threads

User Threads (continued) Thread Dispatcher Queues in process memory to keep track of threads’ state Scheduler – non-preemptive Assume threads are well-behaved Thread voluntarily gives up CPU by calling yield() – does thread context switch to another thread Scheduler – preemptive Assumes threads may not be well-behaved Scheduler sets timer to create a signal that invokes scheduler Scheduler can force thread context switch Increased overhead Application or thread library must handle all concurrency itself! CS-502 Fall 2007 Threads

Thread Interface E.g., POSIX pthreads API: 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 CS-502 Fall 2007 Threads

Kernel Threads Supported by the Kernel Examples OS maintains data structures for thread state and does all of the work of thread implementation. Examples Windows XP/2000 Solaris Linux version 2.6 Tru64 UNIX Mac OS X CS-502 Fall 2007 Threads

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 CS-502 Fall 2007 Threads

Threads – supported by processor E.g., Pentium 4 with Hyperthreading™ www.intel.com/products/ht/hyperthreading_more.htm Multiple processor cores on a single chip True concurrent execution within a single process Requires kernel support Re-opens old issues Deadlock detection Critical section management of synchronization primitives CS-502 Fall 2007 Threads

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 CS-502 Fall 2007 Threads

Threading Issues Semantics of fork() and exec() system calls for processes Thread cancellation Signal handling Thread pools Thread specific data Scheduler activations CS-502 Fall 2007 Threads

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 Windows XP has something similar CS-502 Fall 2007 Threads

Thread Cancellation Terminating a thread before it has finished Two general approaches: Asynchronous cancellation terminates the target thread immediately Deferred cancellation allows the target thread to periodically check if it should be cancelled CS-502 Fall 2007 Threads

Signal Handling Signals are used in UNIX systems 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 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 CS-502 Fall 2007 Threads

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 the application(s) to be bound to the size of the pool CS-502 Fall 2007 Threads

Thread Specific Data Allows each thread to have its own copy of data Useful when you do not have control over the thread creation process (i.e., when using a thread pool) CS-502 Fall 2007 Threads

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; CS-502 Fall 2007 Threads

Models for Kernel Implementation Many-to-One One-to-One Many-to-Many CS-502 Fall 2007 Threads

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 ThreadFiber package CS-502 Fall 2007 Threads

Many-to-Many Model CS-502 Fall 2007 Threads

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 CS-502 Fall 2007 Threads

Two-level Model CS-502 Fall 2007 Threads

Many-to-One Many user-level threads mapped to single kernel thread Examples: Solaris Green Threads GNU Portable Threads CS-502 Fall 2007 Threads

Many-to-One Model CS-502 Fall 2007 Threads

One-to-One Each user-level thread maps to kernel thread Examples Windows NT/XP/2000 Linux Solaris 9 and later CS-502 Fall 2007 Threads

One-to-one Model CS-502 Fall 2007 Threads

Modern Linux Threads Implemented in kernel “A thread is just a special kind of process.” Robert Love, Linux Kernel Development, p.23 The primary unit of scheduling and computation implemented by Linux 2.6 kernel Every thread has its own task_struct in kernel … CS-502 Fall 2007 Threads

Modern Linux Threads (continued) Process task_struct has pointer to own memory & resources Thread task_struct has pointer to process’s memory & resources fork() and thread_create() are library functions implemented by clone() kernel call. … CS-502 Fall 2007 Threads

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 CS-502 Fall 2007 Threads

Process Context 0xFFFFFFFF SP Virtual address space PC 0x00000000 Kernel Code and Data Kernel Space stack (dynamically allocated) SP User Space heap (dynamically allocated) static data PC code (text) 32-bit Linux & Win XP – 3G/1G user space/kernel space CS-502 Fall 2007 Threads

Modern Linux Threads (continued) Multiple threads in kernel at any one 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 or thread … CS-502 Fall 2007 Threads

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 CS-502 Fall 2007 Threads

Reading Assignment Robert Love, Linux Kernel Development Silbertshatz Chapter 3 – “Process Management” Silbertshatz Chapter 4 – “Threads” CS-502 Fall 2007 Threads

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, §22.3.2.2 CS-502 Fall 2007 Threads

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 CS-502 Fall 2007 Threads

Questions? CS-502 Fall 2007 Threads