What s in a process?

Similar documents
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

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

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

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?

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

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

Threads Implementation. Jo, Heeseung

CSE 120 Principles of Operating Systems

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

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

CSE 120 Principles of Operating Systems

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

CSE 120 Principles of Operating Systems

Lecture 4: Threads; weaving control flow

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

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

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

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

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

Yi Shi Fall 2017 Xi an Jiaotong University

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

Process Description and Control

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

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

CSE 451: Operating Systems Winter Module 4 Processes. Mark Zbikowski Allen Center 476

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

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

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Chapter 5: Threads. Outline

CSE 153 Design of Operating Systems Fall 2018

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

Threads. CS3026 Operating Systems Lecture 06

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

Implementation of Processes

Distributed Systems Operation System Support

Chapter 4: Multithreaded Programming

Processes and Threads

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

CS 326: Operating Systems. Process Execution. Lecture 5

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

Announcements/Reminders

Disciplina Sistemas de Computação

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

Threads Chapter 5 1 Chapter 5

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

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Review: Easy Piece 1

Native POSIX Thread Library (NPTL) CSE 506 Don Porter

THREADS. Jo, Heeseung

Processes and Threads Implementation

CS 318 Principles of Operating Systems

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

Computer Systems II. First Two Major Computer System Evolution Steps

CS510 Operating System Foundations. Jonathan Walpole

Process management. What s in a process? What is a process? The OS s process namespace. A process s address space (idealized)

Processes and Threads

W4118 Operating Systems. Junfeng Yang

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

Computer Systems Laboratory Sungkyunkwan University

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

CSci 4061 Introduction to Operating Systems. (Thread-Basics)

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

CSE 451 Midterm 1. Name:

Multithreaded Programming

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

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

CS 318 Principles of Operating Systems

Chapter 3 Process Description and Control

CS333 Intro to Operating Systems. Jonathan Walpole

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

Chapter 4: Multithreaded

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

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control

Operating System Services

CS 450 Operating System Week 4 Lecture Notes

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

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

CS5460: Operating Systems

CS 153 Design of Operating Systems Winter 2016

Processes and Threads Implementation

Processes and Threads. From Processes to Threads. The Case for Threads. The Case for Threads. Process abstraction combines two concepts.

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

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

I.-C. Lin, Assistant Professor. Textbook: Operating System Concepts 8ed CHAPTER 4: MULTITHREADED PROGRAMMING

OS lpr. www. nfsd gcc emacs ls 9/18/11. Process Management. CS 537 Lecture 4: Processes. The Process. Why Processes? Simplicity + Speed

SMD149 - Operating Systems

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

Definition Multithreading Models Threading Issues Pthreads (Unix)

Processes, Threads and Processors

CSE 451: Operating Systems Winter Module 2 Architectural Support for Operating Systems

Chapter 4: Multi-Threaded Programming

www nfsd emacs lpr Process Management CS 537 Lecture 4: Processes Example OS in operation Why Processes? Simplicity + Speed

Even coarse architectural trends impact tremendously the design of systems

Sistemi in Tempo Reale

CSE 410: Computer Systems Spring Processes. John Zahorjan Allen Center 534

OPERATING SYSTEM. Chapter 4: Threads

Transcription:

CSE 451: Operating Systems Winter 2015 Module 5 Threads Mark Zbikowski mzbik@cs.washington.edu Allen Center 476 2013 Gribble, Lazowska, Levy, Zahorjan 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 of control (stack, stack pointer, program counter, registers) OS resources 2013 Gribble, Lazowska, Levy, Zahorjan 2 1

Module overview Big picture: Achieving concurrency/parallelism Kernel s User-level s 2013 Gribble, Lazowska, Levy, Zahorjan 3 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 4 2

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 5 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 6 3

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 (nonblocking I/O) in the web client (browser) 2013 Gribble, Lazowska, Levy, Zahorjan 7 Can we do better? Key idea: separate the concept of a process (address space, OS resources) from that of a minimal of control (execution state: stack, stack pointer, program counter, registers) This execution state is usually called a, or sometimes, a lightweight process 2013 Gribble, Lazowska, Levy, Zahorjan 8 4

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, which defines a sequential execution stream within a process A is bound to a single process / address space address spaces, however, can have multiple s executing within them sharing data between s is cheap: all see the same address space creating s is cheap too! Threads become the unit of scheduling processes / address spaces are just containers in which s execute 2013 Gribble, Lazowska, Levy, Zahorjan 9 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 s are in the same address space, communication is simple/cheap Just update a shared variable! 2013 Gribble, Lazowska, Levy, Zahorjan 10 5

The design space Key address space MS/DOS one per process one process one per process many processes older UNIXes Java many s per process one process Mach, NT, Chorus, Linux, many s per process many processes 2013 Gribble, Lazowska, Levy, Zahorjan 11 (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 2013 Gribble, Lazowska, Levy, Zahorjan 12 6

(new) Address space with s 0xFFFFFFFF address space 0x00000000 1 stack 2 stack 3 stack heap (dynamic allocated mem) static data (data segment) code (text segment) SP (T1) SP (T2) SP (T3) PC (T2) PC (T1) PC (T3) 2013 Gribble, Lazowska, Levy, Zahorjan 13 Value of process/ separation Concurrency (multiing) 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) Multiing is useful even on a uniprocessor even though only one can run at a time Supporting multiing that is, separating the concept of a process (address space, files, etc.) from that of a minimal of control (execution state), is a big win creating concurrency does not require creating new processes faster / better / cheaper 2013 Gribble, Lazowska, Levy, Zahorjan 14 7

Terminology Just a note that there s the potential for some confusion Old world: process == address space + OS resources + single New world: process typically refers to an address space + system resources + all of its s When we mean the address space we need to be explicit refers to a single 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 15 Where do s come from, Mommy? Natural answer: the OS is responsible for creating/ managing s For example, the kernel call to create a new 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 s There is a name space Thread id s (TID s) TID s are integers (surprise!) 2013 Gribble, Lazowska, Levy, Zahorjan 16 8

Kernel s address space Mach, NT, Chorus, Linux, os kernel CPU ( create, destroy, signal, wait, etc.) 2013 Gribble, Lazowska, Levy, Zahorjan 17 Kernel s OS now manages s and processes / address spaces all operations are implemented in the kernel OS schedules all of the s in a system if one in a process blocks (e.g., on I/O), the OS knows about it, and can run other s from that process possible to overlap I/O and computation inside a process Kernel s 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 operations are all system calls context switch argument checks 2013 Gribble, Lazowska, Levy, Zahorjan 18 9

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

User-level s: what the kernel sees address space os kernel CPU 2013 Gribble, Lazowska, Levy, Zahorjan 21 User-level s: the full story user-level library address space os kernel CPU Mach, NT, Chorus, Linux, (kernel create, destroy, signal, wait, etc.) ( create, destroy, signal, wait, etc.) kernel s 2013 Gribble, Lazowska, Levy, Zahorjan 22 11

User-level s User-level s are small and fast managed entirely by user-level library E.g., ps (libps.a) each is represented simply by a PC, registers, a stack, and a small control block (TCB) creating a, switching between s, and synchronizing s are done via procedure calls no kernel involvement is necessary! user-level operations can be 10-100x faster than kernel s as a result 2013 Gribble, Lazowska, Levy, Zahorjan 23 Performance example On a 700MHz Pentium running Linux 2.2.16 (only the relative numbers matter; ignore the ancient CPU!): Processes fork/exit: 251 µs Why? Kernel s p_create()/p_join(): 94 µs (2.5x faster ~150µs faster) User-level s p_create()/p_join: 4.5 µs (another 20x faster - ~100µs faster) Why? 2013 Gribble, Lazowska, Levy, Zahorjan 24 12

User-level implementation The OS schedules the kernel The kernel executes user code, including the support library and its associated scheduler The scheduler determines when a user-level runs it uses queues to keep track of what s are doing: run, ready, wait just like the OS and processes but, implemented at user-level as a library 2013 Gribble, Lazowska, Levy, Zahorjan 25 Thread interface This is taken from the POSIX ps API: rcode = p_create(&t, attributes, start_procedure) creates a new of control new begins executing at start_procedure p_cond_wait(condition_variable, mutex) the calling blocks, sometimes called _block() p_signal(condition_variable) starts a waiting on the condition variable p_exit() terminates the calling p_wait(t) waits for the named to terminate 2013 Gribble, Lazowska, Levy, Zahorjan 26 13

Thread context switch Very simple for user-level s: save context of currently running push CPU state onto stack restore context of the next pop CPU state from next s stack return as the new execution resumes at PC of next 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 27 How to keep a user-level from hogging the CPU? Strategy 1: force everyone to cooperate a willingly gives up the CPU by calling yield() yield() calls into the scheduler, which context switches to another ready what happens if a 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 userlevel by the OS instead of delivered to OS by hardware at each timer interrupt, scheduler gains control and context switches as appropriate 2013 Gribble, Lazowska, Levy, Zahorjan 28 14

What if a tries to do I/O? The kernel powering it is lost for the duration of the (synchronous) I/O operation! The kernel blocks in the OS, as always It maroons with it the state of the user-level Could have one kernel powering each userlevel common case operations (e.g., synchronization) would be quick Could have a limited-size pool of kernel s powering all the user-level s in the address space the kernel will be scheduling these s, obliviously to what s going on at user-level 2013 Gribble, Lazowska, Levy, Zahorjan 29 Multiple kernel s powering each address space user-level library ( create, destroy, signal, wait, etc.) address space os kernel CPU (kernel create, destroy, signal, wait, etc.) kernel s 2013 Gribble, Lazowska, Levy, Zahorjan 30 15

What if the kernel preempts a holding a lock? Other s will be unable to enter the critical section and will block (stall) 2013 Gribble, Lazowska, Levy, Zahorjan 31 Addressing these problems Effective coordination of kernel decisions and userlevel s requires OS-to-user-level communication OS notifies user-level that it is about to suspend a kernel This is called scheduler activations a research paper from UW with huge effect on practice each process can request one or more kernel s process is given responsibility for mapping user-level s onto kernel s kernel promises to notify user-level before it suspends or destroys a kernel ACM TOCS 10,1 2013 Gribble, Lazowska, Levy, Zahorjan 32 16

Summary You really want multiple s per address space Kernel s are much more efficient than processes, but they re still not cheap all operations require a kernel call and parameter validation User-level s 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 33 The design space MS/DOS older UNIXes address space one /process one process one /process many processes Java many s/process one process many s/process many processes Mach, NT, Chorus, Linux, 2013 Gribble, Lazowska, Levy, Zahorjan 34 17