Lecture 5: Concurrency and Threads

Similar documents
SE350: Operating Systems. Lecture 3: Concurrency

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

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

Lecture 4: Memory Management & The Programming Interface

r ~ c.. Q.) 0\ 7 < - \1") ::J - ::r 3 ::J,... ::J Q.) 0!:t. !:t. ::J ::J (/') C

Lecture 15: I/O Devices & Drivers

Lecture 4: Threads; weaving control flow

CS 318 Principles of Operating Systems

Threads and Concurrency

Processes and Threads Implementation

Synchroniza+on. Today: Implementa+on issues

CS 318 Principles of Operating Systems

CSE 120 Principles of Operating Systems

CSE 120 Principles of Operating Systems

CS 153 Design of Operating Systems Winter 2016

CSE 120 Principles of Operating Systems

Computer Systems II. First Two Major Computer System Evolution Steps

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

Implementation of Processes

Lecture 4: Mechanism of process execution. Mythili Vutukuru IIT Bombay

Processes and Threads Implementation

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

CS510 Operating System Foundations. Jonathan Walpole

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

Threads and Concurrency

Lecture 13: Address Translation

EECS 482 Introduction to Operating Systems

The Kernel Abstraction. Chapter 2 OSPP Part I

CSC369 Lecture 2. Larry Zhang

CS Lecture 3! Threads! George Mason University! Spring 2010!

Operating Systems and Networks Assignment 2

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

CSC369 Lecture 2. Larry Zhang, September 21, 2015

CS 326: Operating Systems. Process Execution. Lecture 5

Computer Systems Assignment 2: Fork and Threads Package

Review: Program Execution. Memory program code program data program stack containing procedure activation records

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

by Marina Cholakyan, Hyduke Noshadi, Sepehr Sahba and Young Cha

Processes (Intro) Yannis Smaragdakis, U. Athens

System Call. Preview. System Call. System Call. System Call 9/7/2018

Last time: introduction. Networks and Operating Systems ( ) Chapter 2: Processes. This time. General OS structure. The kernel is a program!

CSE 153 Design of Operating Systems Fall 2018

Processes & Threads. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! More of the same J

Processes. CS3026 Operating Systems Lecture 05

Lecture 10: Multi-Object Synchronization

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

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

Lecture 5: Synchronization w/locks

Section 4: Threads CS162. September 15, Warmup Hello World Vocabulary 2

Processes. Dr. Yingwu Zhu

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

Process. Heechul Yun. Disclaimer: some slides are adopted from the book authors slides with permission 1

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Processes and threads

Process. Heechul Yun. Disclaimer: some slides are adopted from the book authors slides with permission

Processes The Process Model. Chapter 2 Processes and Threads. Process Termination. Process States (1) Process Hierarchies

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

CSE 153 Design of Operating Systems Fall 2018

Review: Program Execution. Memory program code program data program stack containing procedure activation records

Exceptions and Processes

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

4. The Abstraction: The Process

Arvind Krishnamurthy Spring Threads, synchronization, scheduling Virtual memory File systems Networking

Processes and Exceptions

Processes. OS Structure. OS Structure. Modes of Execution. Typical Functions of an OS Kernel. Non-Kernel OS. COMP755 Advanced Operating Systems

Processes The Process Model. Chapter 2. Processes and Threads. Process Termination. Process Creation

Lecture 14: Cache & Virtual Memory

3. Process Management in xv6

Processes & Threads. (Chapter 3) CS 4410 Operating Systems. [R. Agarwal, L. Alvisi, A. Bracy, M. George, E. Sirer, R. Van Renesse]

Processes and Threads. Processes and Threads. Processes (2) Processes (1)

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

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Recall: Use of Threads Version of program with Threads (loose syntax): Recall: Memory Footprint: Two-Threads. Recall: Dispatch Loop

518 Lecture Notes Week 3

CSCE Operating Systems Interrupts, Exceptions, and Signals. Qiang Zeng, Ph.D. Fall 2018

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 4: Threads and Dispatching. 4.

Review: Program Execution. Memory program code program data program stack containing procedure activation records

September 14 th, 2015 Prof. John Kubiatowicz

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - III Processes. Louisiana State University. Processes. September 1 st, 2009

EECS 482 Introduction to Operating Systems

SMD149 - Operating Systems

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

Processes and Threads

CS 537 Lecture 2 - Processes

Precept 2: Non-preemptive Scheduler. COS 318: Fall 2018

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

Operating System Structure

Chapter 3 Processes. Process Concept. Process Concept. Process Concept (Cont.) Process Concept (Cont.) Process Concept (Cont.)

CS 31: Intro to Systems Processes. Kevin Webb Swarthmore College March 31, 2016

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

Process Scheduling Queues

CS 3305 Intro to Threads. Lecture 6

CSCE 313: Intro to Computer Systems

Introduction to OS Processes in Unix, Linux, and Windows MOS 2.1 Mahmoud El-Gayyar

CSci 5103 Operating Systems. Jon Weissman. Administrivia

Definition Multithreading Models Threading Issues Pthreads (Unix)

From Processes to Threads

CS 322 Operating Systems Practice Midterm Questions

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

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

Transcription:

CS 422/522 Design & Implementation of Operating Systems Lecture 5: Concurrency and Threads Zhong Shao Dept of Computer Science Yale University Acknowledgement: some slides are taken from previous versions of the CS422/522 lectures taught by Prof Bryan Ford and Dr David Wolinsky, and also from the official set of slides accompanying the OSPP textbook by Anderson and Dahlin Motivation Operating systems (and application programs) often need to be able to handle multiple things happening at the same time Process execution, interrupts, background tasks, system maintenance Humans are not very good at keeping track of multiple things happening simultaneously Threads are an abstraction to help bridge this gap 1

Why concurrency? Servers (expressing logically concurrent tasks) Multiple connections handled simultaneously Parallel programs To achieve better performance Programs with user interfaces To achieve user responsiveness while doing computation Network and disk bound programs To hide network/disk latency The multi-threading illusion Each thread has its illusion of own CPU yet on a uni-processor all threads share the same physical CPU! How does this work? Two key pieces: TCB --- thread control block, one per thread, holds execution state CPU dispatching loop: while(1) interrupt thread save state get next thread load state, jump to it 2

Definitions A thread is a single execution sequence that represents a separately schedulable task Single execution sequence: familiar programming model Separately schedulable: OS can run or suspend a thread at any time Protection is an orthogonal concept Can have one or many threads per protection domain Different processes have different privileges (& address spaces); switch OS s idea of who is running * switch page table, etc Problems for processes: How to share data? How to communicate? The PL world does not know how to model process yet Thread abstraction Infinite number of processors Threads execute with variable speed Programs must be designed to work with any schedule Programmer Abstraction Physical Reality Threads Processors 1 2 3 4 5 1 2 Running Threads Ready Threads 3

Programmer vs processor view Programmer s View x = x + 1 ; y = y + x; z = x + 5y; Possible Execution #1 x = x + 1 ; y = y + x ; z = x + 5 y ; Possible Execution #2 x = x + 1 ; Thread is suspended Other thread(s) run Thread is resumed y = y + x ; z = x + 5 y ; Possible Execution #3 x = x + 1 ; y = y + x; Thread is suspended Other thread(s) run Thread is resumed z = x + 5 y ; Possible executions One Execution Thread 1 Thread 2 Thread 3 Another Execution Thread 1 Thread 2 Thread 3 Another Execution Thread 1 Thread 2 Thread 3 4

Thread operations thread_create(thread, func, args) Create a new thread to run func(args) thread_yield() Relinquish processor voluntarily thread_join(thread) In parent, wait for forked thread to exit, then return thread_exit Quit thread and clean up, wake up joiner if any Example: threadhello #define NTHREADS 10 thread_t threads[nthreads]; main() { for (i = 0; i < NTHREADS; i++) thread_create(&threads[i], &go, i); for (i = 0; i < NTHREADS; i++) { exitvalue = thread_join(threads[i]); printf("thread %d returned with %ld\n", i, exitvalue); printf("main thread done\n"); void go (int n) { printf("hello from thread %d\n", n); thread_exit(100 + n); // REACHED? 5

threadhello: example output Why must thread returned print in order? What is maximum # of threads running when thread 5 prints hello? Minimum? Fork/Join concurrency Threads can create children, and wait for their completion Data only shared before fork/after join Examples: Web server: fork a new thread for every new connection * As long as the threads are completely independent Merge sort Parallel memory copy 6

bzero with fork/join concurrency void blockzero (unsigned char *p, int length) { int i, j; thread_t threads[nthreads]; struct bzeroparams params[nthreads]; // For simplicity, assumes length is divisible by NTHREADS for (i=0, j=0; i<nthreads; i++, j+=length/nthreads) { params[i]buffer = p + i * length/nthreads; params[i]length = length/nthreads; thread_create_p(&(threads[i]), &go, &params[i]); for (i = 0; i < NTHREADS; i++) { thread_join(threads[i]); Thread data structures Shared State Code Global Variables Thread 1 s Per hread State Thread Control Block (TCB) Information Saved Registers Thread Metadata Thread s Per hread State Thread Control Block (TCB) Information Saved Registers Thread Metadata Heap 7

Thread context Can be classified into two types: Private Shared Shared state Contents of memory (global variables, heap) File system Private state Program counter Registers Classifying program variables int x; void foo() { int y; x = 1; y = 1; main() { int *p; p = (int *)malloc(sizeof(int)); *p = 1; global variable stack variable heap access 8

Classifying program variables (cont d) Addresses of stack variables defined at call-time void foo() { int x; printf( %x, &x); void bar() { int y; foo(); main() { foo(); bar(); // different addresses will get printed Thread control block (TCB) Current state * Ready: ready to run * Running: currently running * Waiting: waiting for resources Registers Status (EFLAGS) Program counter (EIP) 9

Thread lifecycle Init Thread Creation sthread_create() Ready Scheduler Resumes Thread Thread Yield/Scheduler Suspends Thread sthread_yield() Running Thread Exit sthread_exit() Finished Event Occurs 0ther Thread Calls sthread_join() Thread Waits for Event sthread_join() Waiting Implementing threads Thread_create(thread, func, args) Allocate thread control block Allocate stack Build stack frame for base of stack (stub) Put func, args on stack Put thread on ready list Will run sometime later (maybe right away!) stub(func, args): Call (*func)(args) If return, call thread_exit() 10

Pseudo code for thread_create // func is a pointer to a procedure; arg is the argument to be passed to that procedure void thread_create(thread_t *thread, void (*func)(int), int arg) { TCB *tcb = new TCB(); // Allocate TCB and stack thread->tcb = tcb; tcb->stack_size = INITIAL_STACK_SIZE; tcb->stack = new (INITIAL_STACK_SIZE); // Initialize registers so that when thread is resumed, it will start running at stub tcb->sp = tcb->stack + INITIAL_STACK_SIZE; tcb->pc = stub; // Create a stack frame by pushing stub's arguments and start address onto the stack: func, arg *(tcb->sp) = arg; tcb->sp--; *(tcb->sp) = func; tcb->sp--; // Create another stack frame so that thread_switch works correctly thread_dummyswitchframe(tcb); tcb->state = #\readythreadstate#; readylistadd(tcb); // Put tcb on ready list void stub(void (*func)(int), int arg) { (*func)(arg); // Execute the function func() thread_exit(0); // If func() does not call exit, call it here Thread context switch Voluntary Thread_yield Thread_join (if child is not done yet) Involuntary Interrupt or exception Some other thread is higher priority 11

Voluntary thread context switch Save registers on old stack Switch to new stack, new thread Restore registers from new stack Return Exactly the same with kernel threads or user threads Pseudo code for thread_switch // We enter as oldthread, but we return as newthread // Returns with newthread's registers and stack void thread_switch(oldthreadtcb, newthreadtcb) { pushad; // Push general register values onto the old stack oldthreadtcb->sp = %esp; // Save the old thread's stack pointer %esp = newthreadtcb->sp; // Switch to the new stack popad; // Pop register values from the new stack return; 12

Pseudo code for thread_yield void thread_yield() { TCB *chosentcb, *finishedtcb; // Prevent an interrupt from stopping us in the middle of a switch disableinterrupts(); // Choose another TCB from the ready list chosentcb = readylistgetnextthread(); if (chosentcb == NULL) { // Nothing else to run, so go back to running the original thread else { // Move running thread onto the ready list runningthread->state = #\readythreadstate#; readylistadd(runningthread); thread_switch(runningthread, chosentcb); // Switch to the new thread runningthread->state = #\runningthreadstate#; // Delete any threads on the finished list while ((finishedtcb = finishedlist->getnextthread())!= NULL) { delete finishedtcb->stack; delete finishedtcb; enableinterrupts(); A subtlety Thread_create puts new thread on ready list When it first runs, some thread calls thread_switch Saves old thread state to stack Restores new thread state from stack Set up new thread s stack as if it had saved its state in thread_switch returns to stub at base of stack to run func 13

Pseudo code for dummyswitchframe // thread_create must put a dummy frame at the top of its stack: // the return PC & space for pushad to have stored a copy of the // registers This way, when someone switches to a newly created // thread, the last two lines of thread_switch work correctly void thread_dummyswitchframe(newthread) { *(tcb->sp) = stub; // Return to the beginning of stub tcb->sp--; tcb->sp -= SizeOfPopad; Two threads call Yield Thread 1 s instructions Thread 2 s instructions Processor s instructions return from thread_switch return from thread_switch into stub into stub call go call go call thread_yield call thread_yield choose another thread choose another thread call thread_switch call thread_switch save thread 1 state to TCB save thread 1 state to TCB load thread 2 state load thread 2 state return from thread_switch return from thread_switch into stub into stub call go call go call thread_yield call thread_yield choose another thread choose another thread call thread_switch call thread_switch save thread 2 state to TCB save thread 2 state to TCB load thread 1 state load thread 1 state return from thread_switch return from thread_switch return from thread_yield return from thread_yield call thread_yield call thread_yield choose another thread choose another thread call thread_switch call thread_switch save thread 1 state to TCB save thread 1 state to TCB 14

Involuntary thread switch Timer or I/O interrupt Tells OS some other thread should run Simple version End of interrupt handler calls switch() When resumed, return from handler resumes kernel thread or user process Thus, processor context is saved/restored twice (once by interrupt handler, once by thread switch) A quick recap Thread = pointer to instruction + state Process = thread + address space + OS env (open files, etc) Thread encapsulates concurrency; address space encapsulates protection Key aspects: per-thread state picking a thread to run switching between threads The Future: how to share state among threads? how to pick the right thread/process to run? how to communicate between two processes? 15

Threads in the kernel and at user-level Multi-threaded kernel multiple threads, sharing kernel data structures, capable of using privileged instructions Multiprocess kernel Multiple single-threaded processes System calls access shared kernel data structures Multiple multi-threaded user processes Each with multiple threads, sharing same data structures, isolated from other user processes Threads revisited (a) Three processes each with one thread (b) One process with three threads 16

Implementation of processes Potential fields of a PCB Implementation of processes (cont d) Skeleton of what lowest level of OS does when an interrupt occurs 17

Threads (cont d) Each thread has its own stack Threads (cont d) Items shared by all threads in a process Items private to each thread 18

Thread usage A word processor with three threads Thread usage (cont d) A multithreaded Web server 19

Thread usage (cont d) Rough outline of code for previous slide (a) Dispatcher thread (b) Worker thread Implementing threads: roadmap Kernel threads Thread abstraction only available to kernel To the kernel, a kernel thread and a single threaded user process look quite similar Multithreaded processes using kernel threads (Linux, MacOS) Kernel thread operations available via syscall User-level threads Thread operations without system calls 20

Multithreaded OS Kernel Code Kernel Thread 1 Kernel Thread 2 Kernel Thread 3 Process 1 Process 2 Kernel Globals TCB 1 TCB 2 TCB 3 PCB 1 PCB 2 Heap User-Level Processes Process 1 Thread Process 2 Thread Code Code Globals Globals Heap Heap Faster thread/process switch What happens on a timer (or other) interrupt? Interrupt handler saves state of interrupted thread Decides to run a new thread Throw away current state of interrupt handler! Instead, set saved stack pointer to trapframe Restore state of new thread On resume, pops trapframe to restore interrupted thread 21

Multithreaded user processes (Take 1) User thread = kernel thread (Linux, MacOS) System calls for thread fork, join, exit (and lock, unlock, ) Kernel does context switch Simple, but a lot of transitions between user and kernel mode Multithreaded user processes (Take 1) Code Kernel Thread 1 Kernel Thread 2 Kernel Thread 3 Process 1 Process 2 PCB 1 PCB 2 Kernel Globals TCB 1 TCB 2 TCB 3 TCB 1A TCB 1B TCB 2A TCB 2B Heap User-Level Processes Process 1 Thread A Thread B Process 2 Thread A Thread B Code Code Globals Globals Heap Heap 22

Multithreaded user processes (Take 2) Green threads (early Java) User-level library, within a single-threaded process Library does thread context switch Preemption via upcall/unix signal on timer interrupt Use multiple processes for parallelism * Shared memory region mapped into each process Multithreaded user processes (Take 3) Scheduler activations (Windows 8) Kernel allocates processors to user-level library Thread library implements context switch Thread library decides what thread to run next Upcall whenever kernel needs a user-level scheduling decision Process assigned a new processor Processor removed from process System call blocks in kernel 23