Threads and Concurrency

Similar documents
Threads and Concurrency

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

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

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

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

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

Processes and Threads Implementation

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

Processes and Threads Implementation

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Implementation of Processes

ECE232: Hardware Organization and Design

CS 31: Intro to Systems Operating Systems Overview. Kevin Webb Swarthmore College March 31, 2015

System Building. Events, Co-routines, Continuations and Threads - OS (and application) Execution Models. Event Model. Events

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Review of Activation Frames. FP of caller Y X Return value A B C

CS 318 Principles of Operating Systems

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

CS 350 Operating Systems Course Notes

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

CS 318 Principles of Operating Systems

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

CS 350 Operating Systems Course Notes

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

Lecture 5: Procedure Calls

What is a Process? Answer 1: a process is an abstraction of a program in execution

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

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

ECE331: Hardware Organization and Design

ECE260: Fundamentals of Computer Engineering. Supporting Procedures in Computer Hardware

What is a Process? Answer 1: a process is an abstraction of a program in execution

SE350: Operating Systems. Lecture 3: Concurrency

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Processes Prof. James L. Frankel Harvard University. Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved.

SMD149 - Operating Systems

Anne Bracy CS 3410 Computer Science Cornell University

Lecture 5: Concurrency and Threads

Processes. Overview. Processes. Process Creation. Process Creation fork() Processes. CPU scheduling. Pål Halvorsen 21/9-2005

Computer Systems Assignment 2: Fork and Threads Package

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

Arguments and Return Values. EE 109 Unit 16 Stack Frames. Assembly & HLL s. Arguments and Return Values


ECE260: Fundamentals of Computer Engineering

CS 322 Operating Systems Practice Midterm Questions

CSE Lecture In Class Example Handout

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

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

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

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

University of Waterloo CS350 Midterm Examination Model Solution

Computer Architecture. Chapter 2-2. Instructions: Language of the Computer

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

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Lecture 5: Procedure Calls

Anne Bracy CS 3410 Computer Science Cornell University

Lecture 5. Announcements: Today: Finish up functions in MIPS

Computer Systems II. First Two Major Computer System Evolution Steps

MIPS Functions and Instruction Formats

Processes and Non-Preemptive Scheduling. Otto J. Anshus

CSE 120 Principles of Operating Systems

Functions in MIPS. Functions in MIPS 1

Storage in Programs. largest. address. address

EECS 482 Introduction to Operating Systems

Instruction Set Architectures (4)

EE 109 Unit 15 Subroutines and Stacks

Why use an Operating System? Operating System Definition

CSC369 Lecture 2. Larry Zhang

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

Lecture 4: Threads; weaving control flow

CSC369 Lecture 2. Larry Zhang, September 21, 2015

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

Virtual Machines & the OS Kernel

EN164: Design of Computing Systems Lecture 11: Processor / ISA 4

What is a Process? Answer 1: a process is an abstraction of a program in execution

There are 10 total numbered pages, 5 Questions. You have 60 minutes. Budget your time carefully!

Process Concepts. CSC400 - Operating Systems. 3. Process Concepts. J. Sumey

CSE 120 Principles of Operating Systems

Virtual Machines & the OS Kernel

Operating System Concepts Ch. 5: Scheduling

Operating Systems and Networks Assignment 2

Processes. Dr. Yingwu Zhu

Compiling Techniques

Hakim Weatherspoon CS 3410 Computer Science Cornell University

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

CS 326: Operating Systems. Process Execution. Lecture 5

Preview. Process Scheduler. Process Scheduling Algorithms for Batch System. Process Scheduling Algorithms for Interactive System

Today. Putting it all together

Anne Bracy CS 3410 Computer Science Cornell University

a) Do exercise (5th Edition Patterson & Hennessy). Note: Branches are calculated in the execution stage.

Synchronization 1. Synchronization

Process Scheduling Queues

Fast Interrupts. Krste Asanovic, UC Berkeley / SiFive Inc. (Chair) Kevin Chen, Andes (Vice-Chair)

Pebbles Kernel Specification September 26, 2004

Common Problems on Homework

Course Administration

Implementing Procedure Calls

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

Anne Bracy CS 3410 Computer Science Cornell University

W4118 Operating Systems. Junfeng Yang

Transcription:

Threads and Concurrency 1 Threads and Concurrency key concepts threads, concurrent execution, timesharing, context switch, interrupts, preemption reading Three Easy Pieces: Chapter 26 (Concurrency and Threads) Threads and Concurrency 2 What is a Thread? Threads provide a way for mers to express concurrency in a. A normal sequential consists of a single thread of execution. In threaded concurrent s there are multiple threads of execution, all occuring at the same time.

Threads and Concurrency 3 Key ideas from the examples: OS/161 Threaded Concurrency Examples A thread can create new threads usingthread fork New theads start execution in a function specified as a parameter to thread fork The original thread (which calledthread fork and the new thread (which is created by the call tothread fork) proceed concurrently, as two simultaneous sequential threads of execution. All threads share access to the s global variables and heap. Each thread s function activations are private to that thread. Threads and Concurrency 4 OS/161 s Thread Interface create a new thread: int thread_fork( const char *name, struct proc *proc, void (*func) (void *, unsigned long), void *data1, unsigned long data2 ); terminate the calling thread: void thread_exit(void); volutarily yield execution: void (void); // name of new thread // thread s process // new thread s function // function s first param // function s second param Seekern/include/thread.h

Threads and Concurrency 5 Why Threads? Reason #1: parallelism exposed by threads enables parallel execution if the underlying hardware supports it. s can run faster Reason #2: parallelism exposed by threads enables better processor utilization if one thread has to block, another may be able to run Threads and Concurrency 6 Review: Sequential Program Execution stack data code memory SP PC CPU register contents The Fetch/Execute Cycle 1. fetch instruction PC points to 2. decode and execute instruction 3. advance PC

Threads and Concurrency 7 MIPS Registers num name use num name use 0 z0 always zero 24-25 t8-t9 temps (caller-save) 1 at assembler reserved 26-27 k0-k1 kernel temps 2 v0 return val/syscall # 28 gp global pointer 3 v1 return value 29 sp stack pointer 4-7 a0-a3 subroutine args 30 s8/fp frame ptr (callee-save) 8-15 t0-t7 temps (caller-save) 31 ra return addr (for jal) 16-23 s0-s7 saved (callee-save) Seekern/arch/mips/include/kern/regdefs.h Threads and Concurrency 8 Review: The Stack FuncA FuncB FuncC stack growth FuncA() {... FuncB();... } FuncB() {... FuncC();... }

Threads and Concurrency 9 Concurrent Program Execution (Two Threads) SP PC Thread 2 CPU register contents T1 stack T2 stack data code memory SP PC Thread 1 CPU register contents Conceptually, each thread executes sequentially using its private register contents and stack. Threads and Concurrency 10 Implementing Concurrent Threads Option 1: multiple processors, multiple cores, hardware multithreading per core P processors, C cores per processor, M multhreading degree per core P CM threads can execute simultaneously separate register set for each running thread, to hold its execution context Option 2: timesharing multiple threads take turns on the same hardware rapidly switch from thread to thread so that all make progress In practice, both techniques can be combined.

Threads and Concurrency 11 Timesharing and Context Switches When timesharing, the switch from one thread to another is called a context switch What happens during a context switch: 1. decide which thread will run next (scheduling) 2. save register contents of current thread 3. load register contents of next thread Thread context must be saved/restored carefully, since thread execution continuously changes the context Threads and Concurrency 12 Context Switch on the MIPS (1 of 2) /* See kern/arch/mips/thread/switch.s */ switchframe_switch: /* a0: address of switchframe pointer of old thread. */ /* a1: address of switchframe pointer of new thread. */ /* Allocate stack space for saving 10 registers. 10*4 = 40 */ addi sp, sp, -40 sw ra, 36(sp) /* Save the registers */ sw gp, 32(sp) sw s8, 28(sp) sw s6, 24(sp) sw s5, 20(sp) sw s4, 16(sp) sw s3, 12(sp) sw s2, 8(sp) sw s1, 4(sp) sw s0, 0(sp) /* Store the old stack pointer in the old thread */ sw sp, 0(a0)

Threads and Concurrency 13 Context Switch on the MIPS (2 of 2) /* Get the new stack pointer from the new thread */ lw sp, 0(a1) nop /* delay slot for load */ /* Now, restore the registers */ lw s0, 0(sp) lw s1, 4(sp) lw s2, 8(sp) lw s3, 12(sp) lw s4, 16(sp) lw s5, 20(sp) lw s6, 24(sp) lw s8, 28(sp) lw gp, 32(sp) lw ra, 36(sp) nop /* delay slot for load */ /* and return. */ j ra addi sp, sp, 40 /* in delay slot */.end switchframe_switch Threads and Concurrency 14 What Causes a Context Switches? the running thread calls thread yield running thread voluntarily allows other threads to run the running thread calls thread exit running thread is terminated the running thread blocks, via a call to wchan sleep more on this later... the running thread is preempted running thread involuntarily stops running

Threads and Concurrency 15 OS/161 Thread Stack after Voluntary Context Switch (thread yield()) () stack frame stack frame saved thread context (switchframe) stack growth Threads and Concurrency 16 Thread States preemption or () ready got resource or event (wchan_wakeone/all()) dispatch blocked running need resource or event (wchan_sleep()) running: currently executing ready: ready to execute blocked: waiting for something, so not ready to execute.

Threads and Concurrency 17 Preemption without preemption, a running thread could potentially run forever, without yielding, blocking, or exiting preemption means forcing a running thread to stop running, so that another thread can have a chance to implement preemption, the thread library must have a means of getting control (causing thread library code to be executed) even though the running thread has not called a thread library function this is normally accomplished using interrupts Threads and Concurrency 18 Review: Interrupts an interrupt is an event that occurs during the execution of a interrupts are caused by system devices (hardware), e.g., a timer, a disk controller, a network interface when an interrupt occurs, the hardware automatically transfers control to a fixed location in memory at that memory location, the thread library places a procedure called an the normally: 1. create a to record thread context at the time of the interrupt 2. determines which device caused the interrupt and performs device-specific processing 3. restores the saved thread context from the and resumes execution of the thread

Threads and Concurrency 19 OS/161 Thread Stack after in Interrupt interrupt handling stack growth Threads and Concurrency 20 Preemptive Scheduling A preemptive scheduler imposes a limit, called the scheduling quantum on how long a thread can run before being preempted. The quantum is an upper bound on the amount of time that a thread can run. It may block or yield before its quantum has expired. Periodic timer interrupts allow running time to be tracked. If a thread has run too long, the timer preempts the thread by callingthread yield. The preempted thread changes state from running to ready, and it is placed on the ready queue. OS/161 threads use preemptive round-robin scheduling.

Threads and Concurrency 21 OS/161 Thread Stack after Preemption interrupt handling () stack frame () stack frame saved thread context (switchframe) stack growth Threads and Concurrency 22 Two-Thread Example (Part 1) Thread 1 is running, thread two had previously yielded voluntarily.

Threads and Concurrency 23 Two-Thread Example (Part 2) INTERRUPT! A time interrupt occurs! Interrupt handler runs. Threads and Concurrency 24 Two-Thread Example (Part 3) Interrupt handler decides Thread 1 quantum has expired.

Threads and Concurrency 25 Two-Thread Example (Part 4) Scheduler chooses Thread 2 to run. Context switch. Threads and Concurrency 26 Two-Thread Example (Part 5) Thread 2 context is restored.

Threads and Concurrency 27 Two-Thread Example (Part 6) thread yield finishes, Thread 2 resumes. Threads and Concurrency 28 Two-Thread Example (Part 7) Later, Thread 2 yields again. Scheduler chooses Thread 1.

Threads and Concurrency 29 Two-Thread Example (Part 8) Thread 1 context is restored, resumes. Threads and Concurrency 30 Two-Thread Example (Part 9) Interrupt handler restores state from and returns.