Threads. Computer Systems. 5/12/2009 cse threads Perkins, DW Johnson and University of Washington 1

Similar documents
What s in a process?

10/10/ Gribble, Lazowska, Levy, Zahorjan 2. 10/10/ Gribble, Lazowska, Levy, Zahorjan 4

What s in a traditional process? Concurrency/Parallelism. What s needed? CSE 451: Operating Systems Autumn 2012

Questions answered in this lecture: CS 537 Lecture 19 Threads and Cooperation. What s in a process? Organizing a Process

Notes. CS 537 Lecture 5 Threads and Cooperation. Questions answered in this lecture: What s in a process?

Threads. Raju Pandey Department of Computer Sciences University of California, Davis Spring 2011

Chapter 4: Threads. Operating System Concepts 8 th Edition,

CSE 120 Principles of Operating Systems

CSE 120 Principles of Operating Systems

CSE 120 Principles of Operating Systems

Threads Implementation. Jo, Heeseung

What we will learn. Multi Process Systems: Processes &Threads. Architectural support: Processor Modes. Processes (classic vs.

Lecture 4: Threads; weaving control flow

Processes & Threads. Process Management. Managing Concurrency in Computer Systems. The Process. What s in a Process?

Agenda. Threads. Single and Multi-threaded Processes. What is Thread. CSCI 444/544 Operating Systems Fall 2008

CSE 153 Design of Operating Systems Fall 2018

Threads Implementation. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Threads. CS-3013 Operating Systems Hugh C. Lauer. CS-3013, C-Term 2012 Threads 1

Yi Shi Fall 2017 Xi an Jiaotong University

Threads. CSE 410, Spring 2004 Computer Systems.

CS 318 Principles of Operating Systems

Chapter 4: Threads. Operating System Concepts. Silberschatz, Galvin and Gagne

Chapter 4: Multithreaded Programming

Process Description and Control

Chapter 4: Multithreaded Programming. Operating System Concepts 8 th Edition,

CS 450 Operating System Week 4 Lecture Notes

!! How is a thread different from a process? !! Why are threads useful? !! How can POSIX threads be useful?

CS 318 Principles of Operating Systems

Chapter 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

Motivation. Threads. Multithreaded Server Architecture. Thread of execution. Chapter 4

CS 153 Design of Operating Systems Winter 2016

! How is a thread different from a process? ! Why are threads useful? ! How can POSIX threads be useful?

Module 5: Threads. Background Benefits User and Kernel Threads Multithreading Models Solaris 2 Threads Java Threads

Processes and Threads

Operating System. Chapter 4. Threads. Lynn Choi School of Electrical Engineering

Learning Outcomes. Processes and Threads. Major Requirements of an Operating System. Processes and Threads

Chapter 5: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

Page 1. Analogy: Problems: Operating Systems Lecture 7. Operating Systems Lecture 7

Processes & Threads. Recap of the Last Class. Microkernel System Architecture. Layered Structure

Chapter 4: Threads. Chapter 4: Threads

Processes. 4: Threads. Problem needs > 1 independent sequential process? Example: Web Server. Last Modified: 9/17/2002 2:27:59 PM

Implementation of Processes

Outline. Threads. Single and Multithreaded Processes. Benefits of Threads. Eike Ritter 1. Modified: October 16, 2012

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

CPSC 341 OS & Networks. Threads. Dr. Yingwu Zhu

Chapter 5: Threads. Outline

Announcements/Reminders

Processes and Non-Preemptive Scheduling. Otto J. Anshus

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

For use by students enrolled in #71251 CSE430 Fall 2012 at Arizona State University. Do not use if not enrolled.

CS510 Operating System Foundations. Jonathan Walpole

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: Processes and Threads

Chapter 4: Threads. Chapter 4: Threads

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

Chapter 3 Process Description and Control

Process and Its Image An operating system executes a variety of programs: A program that browses the Web A program that serves Web requests

Threads and Too Much Milk! CS439: Principles of Computer Systems January 31, 2018

Advanced Operating Systems (CS 202) Scheduling (1)

CS 326: Operating Systems. Process Execution. Lecture 5

Threads. CS3026 Operating Systems Lecture 06

Processes and Threads Implementation

Threads Chapter 5 1 Chapter 5

Chapter 4: Threads. Chapter 4: Threads. Overview Multicore Programming Multithreading Models Thread Libraries Implicit Threading Threading Issues

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

CSE 4/521 Introduction to Operating Systems

Learning Outcomes. Processes and Threads Implementation. Processes Process s user-level stack and execution state. Threads The Thread Model.

Disciplina Sistemas de Computação

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Processes and Threads Implementation

Threads and Too Much Milk! CS439: Principles of Computer Systems February 6, 2019

Chapter 4: Multithreaded Programming

Chapter 4: Threads. Operating System Concepts with Java 8 th Edition

Chapter 4: Threads. Operating System Concepts 9 th Edition

Computer Systems II. First Two Major Computer System Evolution Steps

PROCESSES AND THREADS THREADING MODELS. CS124 Operating Systems Winter , Lecture 8

Chapter 1: Introduction. Operating System Concepts 9 th Edit9on

Processes & Threads. Recap of the Last Class. Layered Structure. Virtual Machines. CS 256/456 Dept. of Computer Science, University of Rochester

CPSC/ECE 3220 Fall 2017 Exam Give the definition (note: not the roles) for an operating system as stated in the textbook. (2 pts.

Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads. Operating System Concepts

Exercise (could be a quiz) Solution. Concurrent Programming. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - IV Threads

Chapter 4 Threads, SMP, and

Chapter 4: Multithreaded Programming Dr. Varin Chouvatut. Operating System Concepts 8 th Edition,

Chapter 3: Processes. Operating System Concepts 8 th Edition,

SMD149 - Operating Systems

Part V. Process Management. Sadeghi, Cubaleska RUB Course Operating System Security Memory Management and Protection

CS333 Intro to Operating Systems. Jonathan Walpole

Lecture Topics. Announcements. Today: Threads (Stallings, chapter , 4.6) Next: Concurrency (Stallings, chapter , 5.

Computer Systems Laboratory Sungkyunkwan University

CSE 4/521 Introduction to Operating Systems. Lecture 29 Windows 7 (History, Design Principles, System Components, Programmer Interface) Summer 2018

Multithreaded Programming

CS5460: Operating Systems

Chapter 4: Threads. Operating System Concepts 9 th Edition

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

Process- Concept &Process Scheduling OPERATING SYSTEMS

Lecture 5: Process Description and Control Multithreading Basics in Interprocess communication Introduction to multiprocessors

CS420: Operating Systems

THREADS. Jo, Heeseung

OS structure. Process management. Major OS components. CSE 451: Operating Systems Spring Module 3 Operating System Components and Structure

Transcription:

Threads CSE 410, Spring 2009 Computer Systems http://www.cs.washington.edu/410 5/12/2009 cse410-20-threads 2006-09 Perkins, DW Johnson and University of Washington 1

Reading and References Reading» Read sec. 4.1-4.2, rest of ch. 4 as background, Operating System Concepts, Silberschatz, Galvin, and Gagne 5/12/2009 cse410-20-threads 2006-09 Perkins, DW Johnson and University of Washington 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 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, That s a lot of concepts bundled together! 5/12/2009 HML 3

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 client (browser), which might like to initiate multiple requests concurrently» The ecse home epage has 46 src= sc 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 concurrently employ multiple processors» For example, multiplying a large matrix split the output matrix into k regions and compute the entries in each region concurrently using k processors 5/12/2009 HML 4

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 5/12/2009 HML 5

How could we achieve this? Given the process abstraction as we know it:» fork several processes» cause each to map to the same address space to share data This is like making a pig fly it s really inefficient» space: PCB, page tables, etc.» time: creating OS structures, fork and copy addr space, etc. Some equally bad alternatives for some of the cases:» Entirely separate web servers» Asynchronous programming (explicit programming of non-blocking I/Os) in the web client (browser) 5/12/2009 HML 6

Can we do better? Key idea:» separate the concept of a process (address space, etc.)» from that of a minimal thread of control (execution state: PC, etc.) This execution state is usually called a thread, or sometimes, a lightweight g process 5/12/2009 HML 7

Threads and processes Most modern OS s (Mach, Chorus, Win/XP, modern Unix) therefore support two entities:» the process, which defines the address space and general process attributes (such as open files, etc.)» the thread, which h defines a sequential execution stream within a process A thread is bound to a single process» processes, however, can have multiple threads executing within them» sharing data between threads is cheap: all see same address space Threads become the unit of scheduling» processes are just containers in which threads execute 5/12/2009 HML 8

The design space key MS/DOS older UNIXes address space one thread/process one process one thread/process many processes thread Java many threads/process one process many threads/process many processes Mach, NT, Chorus, Linux, 5/12/2009 HML 9

(old) Process address space 0xFFFFFFFF stack (dynamic allocated mem) SP address space 0x00000000 heap (dynamic allocated mem) static data (data segment) code (text segment) PC 5/12/2009 HML 10

(new) Address space with threads 0xFFFFFFFF address space thread 1 stack thread d2 stack thread 3 stack SP (T1) SP (T2) SP (T3) heap (dynamic allocated mem) static data (data segment) 0x00000000 PC (T2) code PC (T1) (text t segment) PC (T3) 5/12/2009 HML 11

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 5/12/2009 HML 12

Where do threads come from, Mommy? Natural answer: the kernel is responsible for creating/managing i 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 5/12/2009 HML 13

User-Level 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 Thread package multiplexes l user-level lthreads on top of kernel thread(s), which it treats as virtual processors» we call these user-level threads 5/12/2009 HML 14

The design space MS/DOS older UNIXes address space one thread/process one process one thread/process many processes thread Java many threads/process one process many threads/process many processes Mach, NT, Chorus, Linux, 5/12/2009 HML 15

Kernel threads address space thread os kernel CPU Mach, NT, Chorus, Linux, (thread create, destroy, signal, wait, etc.) 5/12/2009 HML 16

User-level threads, conceptually user-level thread library address space? Mach, NT, Chorus, Linux, (thread create, destroy, signal, wait, etc.) os kernel thread CPU 5/12/2009 HML 17

User-level threads, really user-level thread library address space Mach, NT, Chorus, Linux, (thread create, destroy, signal, wait, etc.) thread os kernel CPU (kernel thread create, destroy, signal, wait, etc.) kernel threads 5/12/2009 HML 18

Multiple kernel threads powering each address space user-level thread library address space Mach, NT, Chorus, Linux, (thread create, destroy, signal, wait, etc.) thread os kernel CPU (kernel thread create, destroy, signal, wait, etc.) kernel threads 5/12/2009 HML 19

Kernel threads OS now manages threads and processes» 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 (e.g., orders of magnitude more expensive than a procedure call)» thread operations are all system calls context switch argument checks» must maintain kernel state for each thread 5/12/2009 HML 20

User-level threads To make threads cheap and fast, they need to be implemented at the user level l» managed entirely by user-level library, e.g. libpthreads.a User-level threads are small and fast» 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 10-100x faster than kernel threads as a result 5/12/2009 HML 21

Performance example On a 700MHz Pentium running Linux 2.2.16:» Processes fork/exit: 251 µs» Kernel threads pthread_create()/pthread_join(): 94 µs» User-level threads pthread_ create()/pthread_ join: 4.5 µs 5/12/2009 HML 22

User-level thread implementation The kernel thread (the kernel-controlled executable entity associated with the address space) executes the code in the address space This code includes the thread support library and its associated thread scheduler The thread scheduler determines when a 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 5/12/2009 HML 23

How to keep a thread from hogging gthe CPU? 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» User-level scheduler requests that a timer interrupt be delivered by the OS periodically» at each timer interrupt, scheduler gains control and context switches as appropriate 5/12/2009 HML 24

Thread context switch Very simple for user-level threads:» save context of currently running thread push machine state onto thread stack» restore context of the next thread pop machine state from next thread s stack» return as the new thread execution resumes at PC of next thread 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 5/12/2009 HML 25

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! Could have one kernel thread powering each user-level thread» common case operations (e.g., synchronization) would be quick Could have a limited-size it i pool of fkernel threads powering all the user-level threads in the address space» the kernel will be scheduling its threads obliviously to what s going on at user-level 5/12/2009 HML 26

What if the kernel preempts a thread holding a lock? Other threads will be unable to enter the critical section and will block (stall)» tradeoff, as with everything else Solving this requires coordination between the kernel and the user-level thread manager» scheduler activations a research paper from UW with hhuge effect on industry 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 5/12/2009 HML 27

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 verification User-level l threads are:» fast as blazes» great for common-case operations creation, synchronization, destruction» can suffer in uncommon cases due to kernel obliviousness I/O preemption of a lock-holder ld Scheduler activations are the answer» pretty subtle though 5/12/2009 HML 28