News and remarks. CS68 Principles of Programming Languages. Contents today. Concurrency. Declarative concurrency. Threads

Similar documents
Programming Language Concepts, cs2104 Lecture 09 ( )

Declarative Concurrency (CTM 4)

Declarative Concurrency (CTM 4)

Lecture 5: Declarative Programming. The Declarative Kernel Language Machine. September 12th, 2011

CSE 120 Principles of Operating Systems

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Lecture 6: The Declarative Kernel Language Machine. September 13th, 2011

Programming Language Concepts, cs2104 Lecture 04 ( )

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2004

Threading and Synchronization. Fahd Albinali

CMSC 132: Object-Oriented Programming II. Threads in Java

Declarative Computation Model

Computer System Overview

Midterm Exam Amy Murphy 19 March 2003

CS140 Operating Systems Midterm Review. Feb. 5 th, 2009 Derrick Isaacson

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

Lecture Topics. Announcements. Today: Uniprocessor Scheduling (Stallings, chapter ) Next: Advanced Scheduling (Stallings, chapter

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

Lecture 21: Relational Programming II. November 15th, 2011

What s An OS? Cyclic Executive. Interrupts. Advantages Simple implementation Low overhead Very predictable

Lecture 4: Threads; weaving control flow

CS370 Operating Systems Midterm Review

SPIN, PETERSON AND BAKERY LOCKS

Lecture 4: The Declarative Sequential Kernel Language. September 5th 2011

Mozart System Limitations for Version (draft)

Chapter Machine instruction level 2. High-level language statement level 3. Unit level 4. Program level

Exam for 2G1512 DatalogiII, Apr 12th

CS3733: Operating Systems

Announcement. Exercise #2 will be out today. Due date is next Monday

Course Syllabus. Operating Systems

Implementation of Process Networks in Java

Synchronization. Before We Begin. Synchronization. Credit/Debit Problem: Race Condition. CSE 120: Principles of Operating Systems.

Lecture 4: Memory Management & The Programming Interface

Advanced Operating Systems (CS 202) Scheduling (1)

CS 318 Principles of Operating Systems

Operating Systems. Process scheduling. Thomas Ropars.

Notes based on prof. Morris's lecture on scheduling (6.824, fall'02).

6.033 Computer System Engineering

Chapter 13 Topics. Introduction. Introduction

Process Scheduling. Copyright : University of Illinois CS 241 Staff

238P: Operating Systems. Lecture 14: Process scheduling

Written Presentation: JoCaml, a Language for Concurrent Distributed and Mobile Programming

CS558 Programming Languages

cs Java: lecture #6

Lock-Free and Practical Doubly Linked List-Based Deques using Single-Word Compare-And-Swap

Process Description and Control

CSE 120 Principles of Operating Systems

Le L c e t c ur u e e 7 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Multithreading

UNIT:2. Process Management

ECE 574 Cluster Computing Lecture 8

Concurrency. Chapter 5

Lecture 2: Intro to Concurrent Processing. A Model of Concurrent Programming

Today: Synchronization. Recap: Synchronization

Multi-Level Feedback Queues

Performance Throughput Utilization of system resources

Concurrency Terminology

Concurrency. Lecture 14: Concurrency & exceptions. Why concurrent subprograms? Processes and threads. Design Issues for Concurrency.

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

General Objectives: To understand the process management in operating system. Specific Objectives: At the end of the unit you should be able to:

ò mm_struct represents an address space in kernel ò task represents a thread in the kernel ò A task points to 0 or 1 mm_structs

Lecture 2: Intro to Concurrent Processing

Lecture 4: Inter-Process Communication (Jan 27, 2005)

CSCI-GA Operating Systems Lecture 3: Processes and Threads -Part 2 Scheduling Hubertus Franke

Scheduling. Don Porter CSE 306

Lecture Topics. Announcements. Today: Concurrency: Mutual Exclusion (Stallings, chapter , 5.7)

Concurrent and Distributed Systems Introduction

CSE 153 Design of Operating Systems

Yi Shi Fall 2017 Xi an Jiaotong University

MultiThreading. Object Orientated Programming in Java. Benjamin Kenwright

CS4215 Programming Language Implementation

Frequently asked questions from the previous class survey

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

CSC369 Lecture 5. Larry Zhang, October 19,2015

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

Notes on the Exam. Question 1. Today. Comp 104:Operating Systems Concepts 11/05/2015. Revision Lectures (separate questions and answers)

Lecture #7: Implementing Mutual Exclusion

Producing Production Quality Software. Lecture 12: Concurrent and Distributed Programming Prof. Arthur P. Goldberg Fall, 2004

Object Oriented Programming and Design in Java. Session 18 Instructor: Bert Huang

Work Stealing. in Multiprogrammed Environments. Brice Dobry Dept. of Computer & Information Sciences University of Delaware

Interprocess Communication By: Kaushik Vaghani

1995 Paper 10 Question 7

CPU Scheduling. The scheduling problem: When do we make decision? - Have K jobs ready to run - Have N 1 CPUs - Which jobs to assign to which CPU(s)

Lecture 25: Board Notes: Threads and GPUs

Synchronization I. Jo, Heeseung

Introduction to OS Synchronization MOS 2.3

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Comp 204: Computer Systems and Their Implementation. Lecture 25a: Revision Lectures (separate questions and answers)

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

CPU Scheduling. The scheduling problem: When do we make decision? - Have K jobs ready to run - Have N 1 CPUs - Which jobs to assign to which CPU(s)

Animation Part 2: MoveableShape interface & Multithreading

G52CON: Concepts of Concurrency

CIS 1.5 Course Objectives. a. Understand the concept of a program (i.e., a computer following a series of instructions)

Don t write anything in the field marked Eventuell ekstra ID. For each subtask, choose the answer you think is the most correct one.

Last Class: Processes

Concurrent Programming

Lecture 5: Synchronization w/locks

Result: original lp in main is. Goal was.

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

Transcription:

News and remarks CS68 Principles of Programming Languages Håkan Jonsson Dept of Computer Science and Electrical Engineering Luleå University of Technology, Sweden and Computer Science, Dartmouth College http://www.cs.dartmouth.edu/~hj Apart from the Oz Browser there is a also the Oz Panel that shows runtime information. Open the Oz panel from emacs: Either menu option, or C-. C-. s Extra help on HW2 posted in Blackboard. A version of ToHTML to test your Parse. The label "doc" and the parenthesis surrounding the list in 2a and 2b should not be there(!) The list should be the sole input to Parse. doc([ ]) should be [ ] October 15, 2007 CS68 Lecture 9 - Håkan Jonsson Page 2 Contents today Declarative concurrency. Threads October 15, 2007 CS68 Lecture 9 - Håkan Jonsson Page 3 Concurrency By concurrency is meant that several activities executes simultaneously. Network and disk I/O, multiple processes, unix pipelines, The Search for Extra- Terrestrial Intelligence (SETI), etc. Concurrency in programming enables us to organize our programs into indepent parts. Crucial questions: How are actions synchronized? How are data communicated? In Oz, concurrency is achieved by means of threads. The basic syntax is: thread <S> Informal semantics: Evaluate <S> in a separate thread; execution of the main program (and other threads) continues while <S> is being evaluated. Examples: Delay and Cmap. October 15, 2007 CS68 Lecture 9 - Håkan Jonsson Page 4

Concurrency Each thread is executed as a single program. Same semantics as we have already seen. In fact, all programs we have seen so far have been executed as a (single) thread. The meaning of the simultaneous execution of several threads is explained in terms of an execution state with multiple semantic stacks (one per thread) that all share the same single-assignment store. Multiple reads can happen truly simultaneously. Multiple writes happen in some order. Depencies among threads induce constraints upon order of execution, but otherwise the order in which statements execute across threads is undetermined. Example: CFib Fibonacci with threads October 15, 2007 CS68 Lecture 9 - Håkan Jonsson Page 5 October 15, 2007 CS68 Lecture 9 - Håkan Jonsson Page 6 Execution of threads Thread semantics Formally speaking, the abstract machine model is exted. Executions states will have a multi-set MST of semantics stacks. One stack per thread. A multi-set is a set in which an element can occur more than once (multi-sets are also called bags). Machine state: (MST,!) where MST={ST1,ST2,,STn} is a multi-set and! a singleassignment store. October 15, 2007 CS68 Lecture 9 - Håkan Jonsson Page 7 October 15, 2007 CS68 Lecture 9 - Håkan Jonsson Page 8

Thread semantics Execution semantics: Let (MST,!) be the execution state. Then, if all stacks of MST are empty, halt. The program has now terminated. Otherwise, wait until at least one element of MST is runnable. This could, but must not be, an indefinite wait. Choose a runnable stack ST from MST. Let MST' = MST \ {ST}. Pop the top element (<S>,E) of ST and evaluate it according to the rules. This gives a new stack ST and a new store!. Let (MST' " {ST'},! ) be the new machine state. Redo. Notice that all active stacks share a single-assignment store. The semantics describe an implementation that will work on a 1-processor computer, but permit execution by a true multiprocessor. October 15, 2007 CS68 Lecture 9 - Håkan Jonsson Page 9 Scheduling Ideally, threads should seem like running concurrently, even if we are simulating concurrency on a single processor system. The way a runnable thread is chosen affects this. If we choose "unfairly", some threads may seem like not running at all (or much slower than needed/expected). Starvation: A thread is runnable, but never gets chosen. Fair: Every runnable thread will eventually be chosen. Typical scheduling approaches: Time slicing using timers (could be hardware interrupts) and priority levels. Dead-lock, all threads susped: thread 1 is susped on X; next line of code will bind Y. thread 2 is susped on Y; next line of code will bind X. October 15, 2007 CS68 Lecture 9 - Håkan Jonsson Page 10 Order of execution October 15, 2007 CS68 Lecture 9 - Håkan Jonsson Page 11 Easy separation of tasks Example 1: In a client for a MMORPG (Massively Multiplayer Online Role Playing Game) we could put the graphics computation, the networking processing, the internal modeling (computing what actually happens, making predictions, and coping with uncertain information), and the interaction with the player in separate threads. Problem: Balancing the share of processor power given to each thread so that the game becomes realistic. Without threads a program like this could be programmed as a single loop in which all task are carried out in sequence. This doesn t work in practice since some operations might block and others take unacceptable long to complete (causing others to wait). In the concurrent declarative model, breaking up a sequential program into threads gives a program with an equivalent semantics but, possibly, with another order of execution. October 15, 2007 CS68 Lecture 9 - Håkan Jonsson Page 12

Coroutines: Many applications can be modeled as communicating sequential processes. A coroutine is a component much like a procedure that runs forever once started and that every now and then transfers control to other co-routines and goes to sleep until control is returned. A coroutine decides on its own when to run or not. Communication is crucial between coroutines. Typical applications are so called producer - consumer and client -server applications. Example October 15, 2007 CS68 Lecture 9 - Håkan Jonsson Page 13 Table 4.2 This example makes use of the Thread module. Drawback: Threads need to know about each other. The activation and suspension of threads must be programmed. Next lecture we will learn about other, better, ways to produce clientserver and producer-consumer applications. October 15, 2007 CS68 Lecture 9 - Håkan Jonsson Page 14 Components We know how to start a thread; but how do we wait for a thread to finish? Solution: Create an unbound variable Done, In thread 1: Bind Done at exit. In thread 2: Call {Wait Done} to wait until Done is bound. Wait is easy to implement: proc {Wait V} case V of x then skip else skip October 15, 2007 CS68 Lecture 9 - Håkan Jonsson Page 15 October 15, 2007 CS68 Lecture 9 - Håkan Jonsson Page 16

This idea generalizes to multiple threads; either call Wait many times, or introduce multiple variables: local T 1 T 2 T n in thread T 1 = done thread T 2 = T 1 thread T n = T n-1 {Wait T n } Notice how this "cascades ; each thread waits for the previous ones to bind their variable (and terminate). More generally, this synchronizes n threads, executing a procedure each, so they all finish before Barrier returns: Creates a new thread each recursive step. {Barrier [ proc {$} <stmt> 1 proc {$} <stmt> 2... proc {$} <stmt> n ] } proc {Barrier Ps} fun {BarrierLoop Ps L} case Ps of P Pr then M in thread {P} M=L {BarrierLoop Pr M} [] nil then L S={BarrierLoop Ps unit} in {Wait S} Creates a new unbound variable each recursive step. October 15, 2007 CS68 Lecture 9 - Håkan Jonsson Page 17 October 15, 2007 CS68 Lecture 9 - Håkan Jonsson Page 18 Nondeterminism If there are more than one thread that can bind a variable we have observable nondeterminism. The meaning of such a program deps on which binding is carried out first. This is not decided by the programming language but deps on the policy of the thread scheduler. Example Next lecture (Wednesday) Homework 2 due. Read Ch. 4.3 October 15, 2007 CS68 Lecture 9 - Håkan Jonsson Page 19 October 15, 2007 CS68 Lecture 9 - Håkan Jonsson Page 20