CSE 451: Operating Systems Winter 2014 Module 5 Threads Mark Zbikowski Allen Center 476 © 2013 Gribble, Lazowska, Levy, Zahorjan.

Slides:



Advertisements
Similar presentations
Day 10 Threads. Threads and Processes  Process is seen as two entities Unit of resource allocation (process or task) Unit of dispatch or scheduling (thread.
Advertisements

Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
Course: Operating Systems Instructor: Umar Kalim NUST Institute of Information Technology, Pakistan Operating Systems.
Threads. Announcements Cooperating Processes Last time we discussed how processes can be independent or work cooperatively Cooperating processes can.
CSE 451: Operating Systems Winter 2006 Module 4 Processes Ed Lazowska Allen Center 570.
Processes CSCI 444/544 Operating Systems Fall 2008.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
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.
Threads vs. Processes April 7, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
Threads CSCI 444/544 Operating Systems Fall 2008.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Chapter 4: Threads.
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
CS 3013 & CS 502 Summer 2006 Threads1 CS-3013 & CS-502 Summer 2006.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
CS 153 Design of Operating Systems Spring 2015
Chapter 4: Threads. From Processes to Threads 4.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Threads.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Threads, Thread management & Resource Management.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
IT 344: Operating Systems Winter 2010 Module 5 Process and Threads
CSE 451: Operating Systems Winter 2015 Module 5 1 / 2 User-Level Threads & Scheduler Activations Mark Zbikowski 476 Allen Center.
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
Department of Computer Science and Software Engineering
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
CSE 153 Design of Operating Systems Winter 2015 Lecture 4: Threads.
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.
CSE 451: Operating Systems Spring 2010 Module 5 Threads John Zahorjan Allen Center 534.
CS 6560: Operating Systems Design
CSE 120 Principles of Operating
CSE 451: Operating Systems Winter 2011 Threads
CSE 451: Operating Systems Spring 2013 Module 5 Threads
Threads.
Operating Systems ECE344 Lecture 4: Threads Ding Yuan.
Chapter 4: Threads.
Operating System Concepts
IT 344: Operating Systems Module 5 Process and Threads
CSE 451: Operating Systems Winter 2010 Module 5 Threads
CSE 451: Operating Systems Winter 2007 Module 5 Threads
CSE 451: Operating Systems Autumn 2004 Module 5 Threads
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
CSE 451: Operating Systems Winter 2011 Processes
CSE 451: Operating Systems Spring 2008 Module 5 Threads
CSE 153 Design of Operating Systems Winter 2018
Thread Implementation Issues
Lecture Topics: 11/1 General Operating System Concepts Processes
CSE 451: Operating Systems Autumn 2003 Lecture 5 Threads
CSE 451: Operating Systems Winter 2007 Module 5 Threads
CSE 451: Operating Systems Winter 2003 Lecture 5 Threads
CSE 451: Operating Systems Winter 2009 Module 5 Threads
CSE 451: Operating Systems Spring 2005 Module 5 Threads
October 9, 2002 Gary Kimura Lecture #5 October 9, 2002
CSE 451: Operating Systems Winter 2012 Threads
CSE 451: Operating Systems Autumn 2009 Module 5 Threads
Threads vs. Processes Hank Levy 1.
CS510 Operating System Foundations
CSE 451: Operating Systems Autumn 2004 Module 4 Processes
CSE 153 Design of Operating Systems Winter 2019
CS703 – Advanced Operating Systems
CSE451 Introduction to Operating Systems Spring 2007
CSE 451: Operating Systems Winter 2006 Module 5 Threads
CSE 451: Operating Systems Winter 2001 Lecture 5 Threads
Threads.
Threads.
Presentation transcript:

CSE 451: Operating Systems Winter 2014 Module 5 Threads Mark Zbikowski Allen Center 476 © 2013 Gribble, Lazowska, Levy, Zahorjan

2 What’s “in” a process? A process consists of (at least): –An address space, containing the code (instructions) for the running program the data for the running program –Thread state, consisting of The program counter (PC), indicating the next instruction The stack pointer register (implying the stack it points to) Other general purpose register values –A set of OS resources open files, network connections, sound channels, … That’s a lot of concepts bundled together! Today: decompose … –address space –thread of control (stack, stack pointer, program counter, registers) –OS resources

© 2013 Gribble, Lazowska, Levy, Zahorjan 3 Module overview Big picture: Achieving concurrency/parallelism Kernel threads User-level threads

© 2013 Gribble, Lazowska, Levy, Zahorjan 4 The Big Picture Threads are about concurrency and parallelism –Parallelism: physically simultaneous operations for performance –Concurrency: logically (and possibly physically) simultaneous operations for convenience/simplicity One way to get concurrency and parallelism is to use multiple processes –The programs (code) of distinct processes are isolated from each other Threads are another way to get concurrency and parallelism –Threads “share a process” – same address space, same OS resources –Threads have private stack, CPU state – are schedulable

© 2013 Gribble, Lazowska, Levy, Zahorjan 5 Concurrency/Parallelism 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 client (browser), which might like to initiate multiple requests concurrently –The CSE home page has dozens of “src= …” html commands, each of which is going to involve a lot of sitting around! Wouldn’t it be nice to be able to launch these requests concurrently? Imagine a parallel program running on a multiprocessor, which might like to employ “physical concurrency” –For example, multiplying two large matrices – split the output matrix into k regions and compute the entries in each region concurrently, using k processors

© 2013 Gribble, Lazowska, Levy, Zahorjan 6 What’s needed? In each of these examples of concurrency (web server, web client, parallel program): –Everybody wants to run the same code –Everybody wants to access the same data –Everybody has the same privileges –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

© 2013 Gribble, Lazowska, Levy, Zahorjan 7 How could we achieve this? Given the process abstraction as we know it: –fork several processes –cause each to map to the same physical memory to share data see the shmget() system call for one way to do this (kind of) This is like making a pig fly – it’s really inefficient –space: PCB, page tables, etc. –time: creating OS structures, fork/copy address space, etc. Some equally bad alternatives for some of the examples: –Entirely separate web servers –Manually programmed asynchronous programming (non- blocking I/O) in the web client (browser)

© 2013 Gribble, Lazowska, Levy, Zahorjan 8 Can we do better? Key idea: –separate the concept of a process (address space, OS resources) –… from that of a minimal “thread of control” (execution state: stack, stack pointer, program counter, registers) This execution state is usually called a thread, or sometimes, a lightweight process thread

© 2013 Gribble, Lazowska, Levy, Zahorjan 9 Threads and processes Most modern OS’s (Mach (Mac OS), Chorus, Windows, UNIX) 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 bound to a single process / address space –address spaces, however, can have multiple threads executing within them –sharing data between threads is cheap: all see the same address space –creating threads is cheap too! Threads become the unit of scheduling –processes / address spaces are just containers in which threads execute

© 2013 Gribble, Lazowska, Levy, Zahorjan 10 Threads are concurrent executions sharing an address space (and some OS resources) Address spaces provide isolation –If you can’t name it, you can’t read or write it Hence, communicating between processes is expensive –Must go through the OS to move data from one address space to another Because threads are in the same address space, communication is simple/cheap –Just update a shared variable!

© 2013 Gribble, Lazowska, Levy, Zahorjan 11 The design space address space thread one thread per process many processes many threads per process many processes one thread per process one process many threads per process one process MS/DOS Java older UNIXes Mach, NT, Chorus, Linux, … Key

© 2013 Gribble, Lazowska, Levy, Zahorjan 12 (old) Process address space 0x xFFFFFFFF address space code (text segment) static data (data segment) heap (dynamic allocated mem) stack (dynamic allocated mem) PC SP

© 2013 Gribble, Lazowska, Levy, Zahorjan 13 (new) Address space with threads 0x xFFFFFFFF 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)

© 2013 Gribble, Lazowska, Levy, Zahorjan 14 Value of process/thread separation Concurrency (multithreading) is useful for: –handling concurrent events (e.g., web servers and clients) –building parallel programs (e.g., matrix multiply, ray tracing) –improving program structure (the Java argument) Multithreading is useful even on a uniprocessor –even though only one thread can run at a time Supporting multithreading – that is, separating the concept of a process (address space, files, etc.) from that of a minimal thread of control (execution state), is a big win –creating concurrency does not require creating new processes –“faster / better / cheaper”

© 2013 Gribble, Lazowska, Levy, Zahorjan 15 Terminology Just a note that there’s the potential for some confusion … –Old world: “process” == “address space + OS resources + single thread” –New world: “process” typically refers to an address space + system resources + all of its threads … When we mean the “address space” we need to be explicit “thread” refers to a single thread of control within a process / address space A bit like “kernel” and “operating system” … –Old world: “kernel” == “operating system” and runs in “kernel mode” –New world: “kernel” typically refers to the microkernel; lots of the operating system runs in user mode

© 2013 Gribble, Lazowska, Levy, Zahorjan 16 “Where do threads come from, Mommy?” Natural answer: the OS is responsible for creating/managing threads –For example, the kernel call to create a new thread would allocate an execution stack within the process address space create and initialize a Thread Control Block –stack pointer, program counter, register values stick it on the ready queue –We call these kernel threads –There is a “thread name space” Thread id’s (TID’s) TID’s are integers (surprise!)

© 2013 Gribble, Lazowska, Levy, Zahorjan 17 address space thread Mach, NT, Chorus, Linux, … os kernel (thread create, destroy, signal, wait, etc.) CPU Kernel threads

© 2013 Gribble, Lazowska, Levy, Zahorjan 18 Kernel threads OS now manages threads and processes / address spaces –all thread operations are implemented in the kernel –OS schedules all of the threads in a system if one thread in a process blocks (e.g., on I/O), the OS knows about it, and can run other threads from that process possible to overlap I/O and computation inside a process Kernel threads are cheaper than processes –less state to allocate and initialize But, they’re still pretty expensive for fine-grained use –orders of magnitude more expensive than a procedure call –thread operations are all system calls context switch argument checks

© 2013 Gribble, Lazowska, Levy, Zahorjan 19 There is an alternative to kernel threads Threads can also be managed at the user level (that is, entirely from within the process) –a library linked into the program manages the threads because threads share the same address space, the thread manager doesn’t need to manipulate address spaces (which only the kernel can do) threads differ (roughly) only in hardware contexts (PC, SP, registers), which can be manipulated by user-level code the thread package multiplexes user-level threads on top of kernel thread(s) each kernel thread is treated as a “virtual processor” –we call these user-level threads “Where do threads come from, Mommy?” (2)

© 2013 Gribble, Lazowska, Levy, Zahorjan 20 address space thread os kernel CPU User-level threads user-level thread library (thread create, destroy, signal, wait, etc.)

© 2013 Gribble, Lazowska, Levy, Zahorjan 21 address space thread os kernel CPU User-level threads: what the kernel sees

© 2013 Gribble, Lazowska, Levy, Zahorjan 22 address space thread Mach, NT, Chorus, Linux, … os kernel (kernel thread create, destroy, signal, wait, etc.) CPU User-level threads: the full story user-level thread library (thread create, destroy, signal, wait, etc.) kernel threads

© 2013 Gribble, Lazowska, Levy, Zahorjan 23 User-level threads User-level threads are small and fast –managed entirely by user-level library E.g., pthreads ( libpthreads.a ) –each thread is represented simply by a PC, registers, a stack, and a small thread control block (TCB) –creating a thread, switching between threads, and synchronizing threads are done via procedure calls no kernel involvement is necessary! –user-level thread operations can be x faster than kernel threads as a result

© 2013 Gribble, Lazowska, Levy, Zahorjan 24 Performance example On a 700MHz Pentium running Linux (only the relative numbers matter; ignore the ancient CPU!): –Processes fork/exit : 251  s –Kernel threads pthread_create()/pthread_join() : 94  s (2.5x faster – ~150  s faster) –User-level threads pthread_create()/pthread_join : 4.5  s (another 20x faster - ~100  s faster) Why?

© 2013 Gribble, Lazowska, Levy, Zahorjan 25 User-level thread implementation The OS schedules the kernel thread The kernel thread executes user code, including the thread support library and its associated thread scheduler The thread scheduler determines when a user-level thread runs –it uses queues to keep track of what threads are doing: run, ready, wait just like the OS and processes but, implemented at user-level as a library

© 2013 Gribble, Lazowska, Levy, Zahorjan 26 Thread interface This is taken from the POSIX pthreads API: –rcode = pthread_create(&t, attributes, start_procedure) creates a new thread of control new thread begins executing at start_procedure –pthread_cond_wait(condition_variable, mutex) the calling thread blocks, sometimes called thread_block() –pthread_signal(condition_variable) starts a thread waiting on the condition variable –pthread_exit() terminates the calling thread –pthread_wait(t) waits for the named thread to terminate

© 2013 Gribble, Lazowska, Levy, Zahorjan 27 Thread context switch Very simple for user-level threads: –save context of currently running thread push CPU state onto thread stack –restore context of the next thread pop CPU state from next thread’s stack –return as the new thread execution resumes at PC of next thread –Note: no changes to memory mapping required! This is all done by assembly language –it works at the level of the procedure calling convention thus, it cannot be implemented using procedure calls

© 2013 Gribble, Lazowska, Levy, Zahorjan 28 Strategy 1: force everyone to cooperate –a thread willingly gives up the CPU by calling yield() –yield() calls into the scheduler, which context switches to another ready thread –what happens if a thread never calls yield() ? Strategy 2: use preemption –scheduler requests that a timer interrupt be delivered by the OS periodically usually delivered as a UNIX signal ( man signal ) signals are just like software interrupts, but delivered to user- level by the OS instead of delivered to OS by hardware –at each timer interrupt, scheduler gains control and context switches as appropriate How to keep a user-level thread from hogging the CPU?

© 2013 Gribble, Lazowska, Levy, Zahorjan 29 What if a thread tries to do I/O? The kernel thread “powering” it is lost for the duration of the (synchronous) I/O operation! –The kernel thread blocks in the OS, as always –It maroons with it the state of the user-level thread Could have one kernel thread “powering” each user- level thread –“common case” operations (e.g., synchronization) would be quick Could have a limited-size “pool” of kernel threads “powering” all the user-level threads in the address space –the kernel will be scheduling these threads, obliviously to what’s going on at user-level

© 2013 Gribble, Lazowska, Levy, Zahorjan 30 address space thread os kernel user-level thread library (thread create, destroy, signal, wait, etc.) (kernel thread create, destroy, signal, wait, etc.) CPU Multiple kernel threads “powering” each address space kernel threads

© 2013 Gribble, Lazowska, Levy, Zahorjan 31 What if the kernel preempts a thread holding a lock? Other threads will be unable to enter the critical section and will block (stall)

© 2013 Gribble, Lazowska, Levy, Zahorjan 32 Addressing these problems Effective coordination of kernel decisions and user- level threads requires OS-to-user-level communication –OS notifies user-level that it is about to suspend a kernel thread This is called “scheduler activations” a research paper from UW with huge effect on practice each process can request one or more kernel threads –process is given responsibility for mapping user-level threads onto kernel threads –kernel promises to notify user-level before it suspends or destroys a kernel thread ACM TOCS 10,1

© 2013 Gribble, Lazowska, Levy, Zahorjan 33 Summary You really want multiple threads per address space Kernel threads are much more efficient than processes, but they’re still not cheap –all operations require a kernel call and parameter validation User-level threads are: –really fast/cheap –great for common-case operations creation, synchronization, destruction –can suffer in uncommon cases due to kernel obliviousness I/O preemption of a lock-holder Scheduler activations are an answer –pretty subtle though

© 2013 Gribble, Lazowska, Levy, Zahorjan 34 The design space address space thread one thread/process many processes many threads/process many processes one thread/process one process many threads/process one process MS/DOS Java older UNIXes Mach, NT, Chorus, Linux, …