Threads and Continuations COS 320, David Walker

Similar documents
Concurrent ML. John Reppy January 21, University of Chicago

CSC 539: Operating Systems Structure and Design. Spring 2006

Course: Operating Systems Instructor: M Umair. M Umair

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

OPERATING SYSTEM. Chapter 4: Threads

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

Chapter 5 Concurrency: Mutual Exclusion. and. Synchronization. Operating Systems: Internals. and. Design Principles

Chapter 4: Threads. Operating System Concepts 9 th Edition

Chapter 4: Threads. Operating System Concepts 9 th Edition

Chapter 4: Threads. Chapter 4: Threads

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

CS510 Operating System Foundations. Jonathan Walpole

OPERATING SYSTEMS. UNIT II Sections A, B & D. An operating system executes a variety of programs:

Process Description and Control

Chapter 5 Concurrency: Mutual Exclusion and Synchronization

Lecture 2 Process Management

Chapter 5: Processes & Process Concept. Objectives. Process Concept Process Scheduling Operations on Processes. Communication in Client-Server Systems

CSL373: Lecture 5 Deadlocks (no process runnable) + Scheduling (> 1 process runnable)

IT 540 Operating Systems ECE519 Advanced Operating Systems

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

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

Operating Systems 2 nd semester 2016/2017. Chapter 4: Threads

Processes and More. CSCI 315 Operating Systems Design Department of Computer Science

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

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

Processes and Non-Preemptive Scheduling. Otto J. Anshus

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

CHAPTER 2: PROCESS MANAGEMENT

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

Chapter 3: Process Concept

Chapter 3: Process Concept

Module 4: Processes. Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess Communication

Chapter 3: Processes. Operating System Concepts 8th Edition

Module 4: Processes. Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess Communication

Process Concept. Chapter 4: Processes. Diagram of Process State. Process State. Process Control Block (PCB) Process Control Block (PCB)

Chapter 4: Processes

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

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

CHAPTER 3 - PROCESS CONCEPT

Chapter 3: Process Concept

Diagram of Process State Process Control Block (PCB)

Chapter 3: Processes. Chapter 3: Processes. Process in Memory. Process Concept. Process State. Diagram of Process State

COP 4610: Introduction to Operating Systems (Spring 2014) Chapter 3: Process. Zhi Wang Florida State University

Chapter 4: Processes. Process Concept

Chapter 4: Processes. Process Concept

Processes. CS3026 Operating Systems Lecture 05

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

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

Chapter 4: Processes

IT 540 Operating Systems ECE519 Advanced Operating Systems

Declarative Concurrency (CTM 4)

Chapter 3: Processes

CSCE 313: Intro to Computer Systems

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

3.1 Introduction. Computers perform operations concurrently

Category Item Abstract Concrete

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

Process! Process Creation / Termination! Process Transitions in" the Two-State Process Model! A Two-State Process Model!

Processes. Process Concept

CISC2200 Threads Spring 2015

Chapter 5: Threads. Outline

Chapter 3: Processes. Operating System Concepts 8th Edition, modified by Stewart Weiss

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

Chapter 3: Processes. Operating System Concepts 9 th Edition

Lecture 17: Threads and Scheduling. Thursday, 05 Nov 2009

COP 4610: Introduction to Operating Systems (Spring 2016) Chapter 3: Process. Zhi Wang Florida State University

W4118 Operating Systems. Junfeng Yang

The Big Picture So Far. Chapter 4: Processes

Processes. Electrical and Computer Engineering Stephen Kim ECE/IUPUI RTOS & Apps 1

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

! The Process Control Block (PCB) " is included in the context,

CS420: Operating Systems

Chapter 3: Processes

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

A process. the stack

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

Chapter 4: Threads. Chapter 4: Threads

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

CSC369 Lecture 2. Larry Zhang

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

CSE 4/521 Introduction to Operating Systems

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

Chapter 3: Processes. Operating System Concepts Essentials 2 nd Edition

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

SMD149 - Operating Systems

Processes. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

The Big Picture So Far. Chapter 4: Processes

Concurrent Processing in Client-Server Software

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

Process. Program Vs. process. During execution, the process may be in one of the following states

Processes. Johan Montelius KTH

2. The system of... generally ran one job at a time. These were called single stream batch processing.

SE350: Operating Systems. Lecture 3: Concurrency

Outlook. Process Concept Process Scheduling Operations on Processes. IPC Examples

Lecture 4: Threads; weaving control flow

Processes and Threads

Chapter 3: Process-Concept. Operating System Concepts 8 th Edition,

Processes. Operating System Concepts with Java. 4.1 Sana a University, Dr aimen

Multiple Inheritance. Computer object can be viewed as

Operating System Concepts Ch. 3: Processes

Transcription:

Threads and Continuations COS 320, David Walker

Concurrency Concurrency primitives are an important part of modern programming languages. Concurrency primitives allow programmers to avoid having to specify the order that different pieces of their program execute. Concurrency allows programmers to think about program parts as executing largely independently of one another and only infrequently synchronizing. 1

Concurrency Reasons for using concurrency: Improved program structure Sometimes easier to think about program as collection of independent units running in concurrently Eg: implement a GUI as a collection of jobs waiting for user events; each job is a separate thread that reacts to GUI events independently Performance: hide latency involved with I/O or simply allow program to run in parallel, if the underlying platform is a multiprocessor platform. 2

Concurrency Reasons not to using concurrency: It is often significantly harder to write programs correctly because you must worry about a completely new correctness criterion: proper ordering and interleaving of program statements. The most common basic unit of concurrency within programs is the thread 3

Threads An operating system process is a collection of code together with some storage executing in its own protection domain. Two processes do not share data directly. A thread consists of: a bit of code a stack Some storage shared with other threads in the same process. 4

Implementing Threads Operating systems often support both processes and threads. Posix, for example, is a standard for implementing threads that is supported by Linux. Options for implementing language-level (ie: Java or ML) threads One-to-one: Each Java thread is directly mapped to an OS thread Many-to-one: All Java threads for a particular program are mapped to a single OS thread Many-to-many: Some number of Java threads are mapped to an OS thread; many OS threads are used for single program. 5

Implementing Threads: One-to-one Each Java thread is directly mapped to an OS thread. Often also called a kernel threads solution. Con: Relatively high overhead for context switch as any context switch involves a system call through the kernel. Con: Often can only handle a relatively small number of threads. Pro: Operating system can schedule threads as best suits the underlying hardware. If the underlying hardware has multiple processors, threads will run in parallel. Pro: Easy for compiler writer. 6

Implementing Threads: Many-to-one All Java threads run within a single OS thread. Often also called a user threads solution. Pro: Context switch is barely more than an indirect procedure call. Pro: Some implementation strategies can handle a huge number of threads. Pro: Performance and behavior more consistent across platforms. Con: No parallelism. Con: Compiler writer must implement their own scheduler. 7

Implementing Threads: Many-to-many Multiple Java threads run within a single OS thread. Multiple OS threads also used. Pro: Most pros of many-to-one and oneto-one (parallelism, some threads have lowoverhead, can scale to many more threads) Con: Implementation most complex. 8

ML Threads SML/NJ (Reppy s Concurrent ML) has a very rich collection of primitives for dealing with threads: creation, suspension, termination, communication, synchronization a very lightweight user-threads implementation within SML/NJ itself using the special ML primitives callcc and throw 9

ML Threads Create new threads dynamically using fork. Create a new child thread. Parent thread continues. Kill threads using exit. Can be called explicitly. Implicit exit upon termination. 10

ML Threads Cede control to another thread by calling yield. A coroutine hand-off to the scheduler itself. No need to specify next routine to run. Scheduler resumes some ready thread: Maintain a ready queue of threads available to run. Select a routine from the ready queue and resume it. 11

A Simple Thread Interface A simple interface for user-level threads: signature THREADS = sig exception NoMoreThreads val fork : (unit -> unit) -> unit val yield : unit -> unit val exit : unit -> a end 12

Producer-Consumer Threads Naïve implementation: structure Client = struct end open Threads val buffer : int ref = ref (~1) fun producer (n) = (buffer := n ; yield () ; producer (n+1)) fun consumer () = (print (Int.toString (!buffer)); yield (); consumer()) fun run () = (fork (consumer); producer 0) 13

Producer-Consumer Threads This only works if: Scheduler is a strict FIFO. There are only two threads that alternate execution on yield. There are no asynchronous interrupts. 14

Producer-Consumer Threads Avoiding reliance on some scheduling assumptions: structure Client = struct end open Threads val buffer : int option ref = ref NONE (* avoid trampling on our own feet *) fun producer (n) = (case!buffer of NONE => (buffer := SOME n ; yield() ; producer (n+1)) SOME => (yield (); producer (n))). 15

Producer-Consumer Threads structure Client = struct end. (* avoid repeatedly printing the same value *) fun consumer () = (case!buffer of NONE => (yield (); consumer()) SOME n => (print (Int.toString n); buffer := NONE; yield(); consumer())) fun run () = (fork (consumer); producer 0) 16

An Aside: Java Threads The threads interface to Java is not the same, but there are a lot of similarities. Thread.start(r) is fork (where r is an object with a run method) Thread.stop() is exit Thread.suspend() is yield 17

Channels Channels carry values of an arbitrary type: type a chan Channels are created by the channel primitive: val channel : unit -> a chan Out-of-use channels are recycled by the garbage collector. 18

Synchronous Send and Receive Synchronous (blocking) operations for communicating on channels: val send : a chan * a -> unit val recv : a chan -> a The send operation blocks its thread until the message has been received; the recv blocks its thread until a matching send has occurred. The sender and receiver synchronize on the channel. This is sometimes called a rendezvous. 19

Dataflow Networks A stream is an infinite sequence of values. Idea: a channel represents a stream. Read from a stream by receiving on a channel. Generate a new stream by allocating a new channel and forking a thread that sends the appropriate values on the channel. Write functions from streams to streams by forking threads that read input channels and write on output channels. This is called a dataflow, or Kahn-MacQueen, network. 20

Example: Flip-Flops as Dataflow Networks Modelling a digital circuit as a dataflow network: Model a wire as a stream of boolean values (a Boolean-valued channel). Model a gate as a stream transducer (a thread). Example: RS latch built from two nor gates. 21

Example: Flip-Flops Constantly high or low wires: fun constantly b = let val C = channel() fun loop () = (send (C, b); loop()) in fork loop ; C end 22

Example: Flip-Flops Nor-gate, with unit delay: fun nor gate (A, B) = let val C = channel() fun nor (a, b) = not (a orelse b) fun loop (a, b) = (send (C, nor (a, b)); loop (recv A, recv B)) in fork (fn () => loop (false, false)); C end 23

Example: Flip-Flops Split a wire: fun split A = let val B1 = channel() val B2 = channel() fun loop a = (send (B1, a); send (B2, a); loop (recv A)) in fork (fn () => loop (recv A)); (B1, B2) end 24

Example: Flip-Flops Copy, with unit delay: fun copy (A, B) = let fun loop a = (send (B, a); loop (recv A)) in fork (fn () => loop false) end 25

Example: Flip-Flops Build an RS latch, using backpatching to implement loopback: fun RS ff (S, R) = let val X = channel () val Y = channel () val X = nor gate (S, Y ) val Y = nor gate (X, R) val (Y1, Y2) = split Y in copy (X, X ); copy (Y1, Y ); Y2 end 26

Example: Flip-Flops Test harness: fun test ff n () = let val R = constantly false val S = constantly true val Y = RS ff (S, R) in print (tostring (take n Y)) end 27

Implementing Threads User-level threads can be implemented directly in SML via a (user-level) scheduler we write our selves. Scheduler maintains a ready queue of runnable threads. Threads can explicitly yield control back to the scheduler. Concurrency is implemented by interleaving thread execution, rather than by running them in parallel. What kind of data structure/operations will we use to implement the thread objects themselves? 28

Implementing Threads Threads may be implemented using first-class continuations. A continuation has some state (values of local variables) and represents a point in the instruction stream (a unit of control). That is all the thread is! To suspend the current thread, the current continuation is seized using callcc. To start another thread, a continuation is activated using throw. 29

Callcc and Throw To seize a continuation use callcc: callcc : ( a cont -> a) -> a callcc (fn k => e). To throw a value to a continuation: throw : a cont -> a -> b throw k v. 30

Callcc and Throw Problem: multiply the integers in a list, stopping early on zero. bind an escape point for the re- Solution: turn. fun mult list (l:int list):int = callcc (fn ret => let fun mult nil = 1 mult (0:: ) = throw ret 0 mult (n::l) = n * mult l in mult l end ) 31

Implementing Threads A basic thread signature: signature THREADS = sig val yield : unit -> unit val exit : unit -> a val fork : (unit -> unit) -> unit end CML threads are similar, but include thread id s and wait. 32

Implementing User-Level Threads A thread is a unit cont. Maintain a ready queue of threads. Yield saves current continuation and dispatches to another one in the queue. Exit throws away current continuation and dispatches. Fork runs a new thread with given code; suspends parent. 33

User-Level Threads structure Threads :> THREADS = struct end open SMLofNJ.Cont exception NoRunnableThreads type thread = unit cont val readyqueue : thread Queue.queue = Queue.mkQueue() fun dispatch () = let val t = Queue.dequeue readyqueue handle Queue.Dequeue => raise NoRunnableThreads in throw t () end. 34

Implementing User-Level Threads structure Threads :> THREADS = struct. fun exit () = dispatch() fun enqueue t = Queue.enqueue (readyqueue, t) fun fork f = callcc (fn parent => (enqueue parent; f (); exit())) end fun yield () = callcc (fn parent => (enqueue parent; dispatch())) 35

Asynchronous Channels Simple message-passing interface: signature CHAN = sig type a chan val channel : unit -> a chan val send : ( a chan * a) -> unit val recv : a chan -> a end 36

Asynchronous Channels Main ideas: A channel consists of a pair of queues, one for senders, one for receivers. The implementation of (asynchronous) send and receive matches senders with receivers. 37

Asynchronous Channels structure ASynchChan :> CHAN = struct structure Q = Queue datatype a chan = CH of { sendq : a Q.queue recvq : a cont Q.queue } fun channel () = CH { sendq = Q.mkQueue(), recvq = Q.mkqueue() }... end 38

Asynchronous Channels structure AsynchChan :> CHAN = struct... fun send (CH { sendq, recvq }, msg) = if (Q.isEmpty recvq) then Q.enqueue (sendq, msg) else callcc (fn k => Q.enqueue (rdyq, k); throw (Q.dequeue recvq) msg)... end 39

Asynchronous Channels structure AsynchChan :> CHAN = struct... fun recv (CH { sendq, recvq }) = if (Q.isEmpty sendq) then callcc (fn k => Q.enqueue (recvq, k); dispatch()) else Q.dequeue sendq end 40

Summary Multi-threaded programs are all the rage. Threads can be mapped to individual OS processes, in which case they may execute in parallel. Threads can also be implemented at user level. SML/NJ provides first-class continuations, mechanism that can be used to implement threads directly, as well as many other cool things. 41