Structuring the Computation. Structuring the Computation

Similar documents
Informatica 3. Marcello Restelli. Laurea in Ingegneria Informatica Politecnico di Milano 9/15/07 10/29/07

Performance Throughput Utilization of system resources

1 Process Coordination

Concept of a process

CS3733: Operating Systems

Synchronization: Basics

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

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

Chapter 6: Process Synchronization

CSE Traditional Operating Systems deal with typical system software designed to be:

Synchronization: Basics

CS510 Operating System Foundations. Jonathan Walpole

Chapter 7 Control I Expressions and Statements

G52CON: Concepts of Concurrency

Monitors; Software Transactional Memory

Synchronization Spinlocks - Semaphores

Lecture 9: Control Flow

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Programming in Parallel COMP755

Monitors; Software Transactional Memory

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

Midterm on next week Tuesday May 4. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 9

Process Management And Synchronization

Synchronization Principles

Threads and Continuations COS 320, David Walker

Semaphores. To avoid busy waiting: when a process has to wait, it will be put in a blocked queue of processes waiting for the same event

Semaphores. Semaphores. Semaphore s operations. Semaphores: observations

Expressions vs statements

Concurrent Programming

message passing model

G Programming Languages - Fall 2012

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

Introduction to OS Synchronization MOS 2.3

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

UNIT 3

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Java Monitors. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

COP4020 Programming Assignment 1 - Spring 2011

Concurrency: a crash course

Carnegie Mellon Concurrency and Synchronization

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

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

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Chapter 6: Process Synchronization

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data

Semaphores. Otto J. Anshus University of {Tromsø, Oslo}

Process Synchronization

Exception handling. Exceptions can be created by the hardware or by software: Examples. Printer out of paper End of page Divide by 0

Models of concurrency & synchronization algorithms

Chapter 7: Process Synchronization!

Chapter 7: Process Synchronization. Background. Illustration

CS370 Operating Systems

A Deterministic Concurrent Language for Embedded Systems

A Deterministic Concurrent Language for Embedded Systems

Critical Section Problem: Example

Java Threads. COMP 585 Noteset #2 1

Process Synchronization: Semaphores. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

Concurrent Programming

Announcements. Scope, Function Calls and Storage Management. Block Structured Languages. Topics. Examples. Simplified Machine Model.

Process Synchronization

Interprocess Communication By: Kaushik Vaghani

05-concurrency.txt Tue Sep 11 10:05: , Fall 2012, Class 05, Sept. 11, 2012 Randal E. Bryant

Silberschatz and Galvin Chapter 6

Defensive Programming

Queues. CITS2200 Data Structures and Algorithms. Topic 5

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Concurrent Programming using Threads

Dept. of CSE, York Univ. 1

Chapter 13 Topics. Introduction. Introduction

Semaphore Use In Synchronization

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

LECTURE 17. Expressions and Assignment

Process Coordination

G Programming Languages Spring 2010 Lecture 8. Robert Grimm, New York University

CHAPTER 6: PROCESS SYNCHRONIZATION

Concurrency - Topics. Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads

Operating Systems CMPSC 473. Synchronization February 26, Lecture 12 Instructor: Trent Jaeger

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

Parallel Programming Languages COMP360

Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify

Multitasking / Multithreading system Supports multiple tasks

3. Java - Language Constructs I

UNIX Input/Output Buffering

Process Synchronization

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Module 6: Binary Trees

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

Lecture 8: September 30

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

Operating Systems ECE344

Semaphores (and Eventcounts) Otto J. Anshus University of {Tromsø, Oslo}

Concurrency Control. Synchronization. Brief Preview of Scheduling. Motivating Example. Motivating Example (Cont d) Interleaved Schedules

Enforcing Mutual Exclusion Using Monitors

Concurrency: Mutual Exclusion and

Francesco Nidito. Programmazione Avanzata AA 2007/08

Chapter 7: Process Synchronization. Background

Lecture 6. Process Synchronization

Transcription:

2016-06-10 Structuring the Computation Structuring the Computation

2016-06-10 Structuring the Computation 1 Expressions infix notation prefix notation postfix notation a (b + c) a + b c a b c + operator associativity and operator preceedence a + b c corresponds to a + (b c) (Pascal, C,... ) a = b<c corresponds to (a = b)<c (Pascal) a == b<c corresponds to a == (b<c) (C) conditional expressions (a>b)? a : b if a>b then a else b case x of 1 => f1(y) 2 => f2(y) => g(y) (C) (ML) (ML)

2016-06-10 Structuring the Computation 2 Conditional Statements if x>0 then if x<10 then x := 0 else x := 1000 if x>0 then begin if x<10 then x := 0 end else x := 1000 if x>0 then if x<10 then x := 0 end else x := 1000 end if a then S1 else if b then S2 else if c then S3 else S4 end switch (operator) { case + : result = operand1 + operand2; break; case - : result = operand1 - operand2; break; default: break; } case OPERATOR is when + => result := operand1 + operand2; when => result := operand1 operand2; when others => null; end case

2016-06-10 Structuring the Computation 3 Loops for var := lower to upper do statement for (int i = 0; i<10; i++){...} for var in discrete range loop body end loop (Pascal) (C++) (Ada) while condition do statement while (expression) statement; while condition loop loop body end loop (Pascal) (C) (Ada) repeat statement until condition do statement while (expression); loop statement; exit when condition end loop (Pascal) (C) (Ada) A: loop... loop... exit A;... end loop... end loop A;

2016-06-10 Structuring the Computation 4 Routines procedure p(var x: T; y: Q; function f(z: R): integer); void proc(int* x, int y) { *x = *x + y; } void proc(int& x, int y) { x = x + y; } alias problem: aliases reduce readability and prevent optimizations u := x + z + f(x,y) + f(x,y) + x + z but, aliases are also a source of expressiveness and flexibility

2016-06-10 Structuring the Computation 5 Exception Handling in Ada predefined exceptions: Constraint Error Program Error Storage Error Tasking Error user defined exception (example): explicit raise of an exception (example): Help: exception; raise Help; begin block with exception handling... statements... exception when Help =>... statements... when Constraint Error =>... statements... when others =>... statements... end;

2016-06-10 Structuring the Computation 6 Implementation of Exception Handling compiler assigns unique name to exception if an exception occurs, search handler in outer blocks within routine, if not found, propagate exception to calling routine (along the dynamic link) each activation record holds pointer to a static table, each table entry associates an exception with a handler alternative implementation: global exception table (no cost without exception), binary search on ip addresses to find handler (costs when exception occurs) exceptions can be propagated to the outside of the scope

2016-06-10 Structuring the Computation 7 Exception Handling in C++ arbitrary data can be propagated as exceptions throwing an exception: throw Help(MSG1); declaration of propagated exceptions in functions (depricated): void foo() throw(help, Zerodivide); special functions: unexpected() terminate() when propagating wrong exception, depricated when no exception handler found

2016-06-10 Structuring the Computation 8 Example in C++ class Help {... }; class Zerodivide {... };... try {... } catch(help& msg) { switch(msg.kind) { case MSG1:...; case MSG2:...;... } } catch(zerodivide& z) {... }

2016-06-10 Structuring the Computation 9 Exception Handling in Java propagated exceptions must be declared (except RuntimeException, Error): void foo() throws Help; usual try-catch-finally block: try {... } catch(exception1 ex) {... } catch(exception2 ex) {... }... finally {... } // exception in finally supresses that in try try with resources (since Java 7): try (Reader r = new FileReader(path)) {... } // r closed; exception in try suppresses that from closing

2016-06-10 Structuring the Computation 10 Exception Handling in ML exception Neg fun fact(n) = if n<0 then raise Neg else if n = 0 then 1 else n fact(n 1) fun fact 0(n) = fact(n) handle Neg => 0;

2016-06-10 Structuring the Computation 11 Exception Handling in Eiffel try_several_methods is local i: INTEGER -- automatically initialized to 0 do try_method(i); rescue i := i + 1; if i < max_trials then retry -- retry the execution of try_several_methods end end

2016-06-10 Structuring the Computation 12 Pattern Matching datatype day = Mon Tue Wed Thu Fri Sat Sun fun day off(sun) = true day off(sat) = true day off( ) = false fun reverse(nil) = nil reverse(head::tail) = reverse(tail) @ [head] fun rev(nil) = nil rev(0::tail) = [0] @ [tail] rev(head::tail) = rev(tail) @ [head]

2016-06-10 Structuring the Computation 13 Nondeterminism and Backtracking A if B or C or D. C if E and F and G. D if I or H.

2016-06-10 Structuring the Computation 14 Event Driven Computation example of trigger in database: on event when condition do action on insert in EMPLOYEE when TRUE do emp number++

2016-06-10 Structuring the Computation 15 Scheme of Event Driven Computation event handler 1 ր external event event queue dispatch event handler 2 ց event handler 3

2016-06-10 Structuring the Computation 16 Scheme of Actors message to 1 event queue actor 1 message to 2 event queue actor 3 message to 3 event queue actor 3

2016-06-10 Structuring the Computation 17 Coroutines int i = 0; unit client { unit main { unit next { int stop =...; resume client; int step(){...};... }... while(i!= stop) { while(true) {... i += step(); resume next; resume client; } } } }

2016-06-10 Structuring the Computation 18 Process Example: Producer and Consumer process producer { process consumer { while (true) { while (true) { produce an element; take element from buffer; append it to the buffer; operate on it; } } } }

2016-06-10 Structuring the Computation 19 Buffer Operations void append(int x) { int remove() { count++; count--; int i = next_in(); int j = next_out(); buffer[i] = x; return buffer[j]; } } race conditions occur when executed concurrently, synchronization necessary

2016-06-10 Structuring the Computation 20 Declaration of Processes in Ada task type SERVER is entry NEXT REQUEST(NR: in REQUEST); entry SHUT DOWN; end SERVER; type SERVER PTR is access SERVER; MY SERVER: SERVER; task CHECKER is entry CHECK(T: in TEXT); entry CLOSE; end CHECKER; HIS SERVER PTR: SERVER PTR := new SERVER;

2016-06-10 Structuring the Computation 21 Semaphores low-level synchronization mechanism based on a counters two atomic actions on each semaphore: P(s): V(s): if s>0 then s = s 1 else suspend current process if there is a process suspended on the semaphore then wake up process else s = s + 1 before entering a critical section controlled by s invoke P(s), on exit invoke V(s)

2016-06-10 Structuring the Computation 22 Using Semaphores var buffer buf; semaphore mutex = 1; in = 0; spaces = buf.size(); process producer { process consumer { int i; int j; while(true) { while(true) { produce(i); P(in); P(spaces); P(mutex); P(mutex); j = buf.remove(); buf.append(i); V(mutex); V(mutex); V(spaces); V(in); consume(i); } } } }

2016-06-10 Structuring the Computation 23 Using a Monitor in Concurrent Pascal type fifostorage = monitor var contents: array[1..n] of integer; total: 0..n; in, out: 1..n; sender, receiver: queue; procedure entry append (item: integer) begin if total = n then delay(sender); contents[in] := item; in := (in mod n) + 1; total := total + 1; continue(receiver) end; procedure entry remove (var item: integer) begin if total = 0 then delay(receiver); item := contents[out]; out := (out mod n) + 1; total := total - 1; continue(sender) end; begin total := 0; in := 1; out := 1 end

2016-06-10 Structuring the Computation 24 Synchronization of Processes via Monitor type fifostorage =... previous slide...; type producer = process (storage: fifostorage) var element: integer; begin cycle...; storage.append(element);... end end; type consumer = process (storage: fifostorage) var datum: integer; begin cycle...; storage.remove(datum);... end end; var meproducer: producer; youconsumer: consumer; buffer: fifostorage; begin meproducer(buffer); youconsumer(buffer); end

2016-06-10 Structuring the Computation 25 Using a Monitor in Java public class IntBuffer100 { private int[] cont = new int[100]; private int in = 0, out = 0, total = 0; } public synchronized void append(int item) { while (total >= 100) try { wait(); } catch(interruptedexception e){} cont[in] = item; total++; if (++in >= 100) in = 0; notifyall(); } public synchronized int remove() { while (total <= 0) try { wait(); } catch(interruptedexception e){} int temp = cont[out]; total--; if (++out >= 100) out = 0; notifyall(); return temp; }

2016-06-10 Structuring the Computation 26 Using a Protected Type (Monitor) in Ada protected type Fifo_Storage is entry Append (Item: Integer); entry Remove (Item: out Integer); private Contents: array(1..100) of Integer; In, Out: Integer range 1..100 := 1; Total: Integer range 0..100 := 0; end Fifo_Storage; protected body Fifo_Storage is entry Append (Item: Integer) when Total < 100 is begin Contents(In) := Item; In := (In mod 100) + 1; Total := Total + 1; end Append; entry Remove (Item: out Integer) when Total > 0 is begin Item := Contents(Out); Out := (Out mod 100) + 1; Total := Total - 1; end Remove; end Fifo_Storage;

2016-06-10 Structuring the Computation 27 Rendezvous in Ada task Buffer_Handler is entry Append (I: Integer); entry Remove (I: out Integer); end Buffer_Handler; task body Buffer_Handler is Cont: array(1..100) of Integer; In, Out: Integer range 1..100 := 1; Total: Integer range 0..100 := 0; begin loop select when Total < 100 => accept Append(I: Integer) do Cont(In) := I end; In := (In mod 100) + 1; Total := Total + 1; or when Total > 0 => accept Remove(I: out Integer) do I := Cont(Out) end; Out := (Out mod 100) + 1; Total := Total - 1; end select; end loop; end Buffer_Handler;

2016-06-10 Structuring the Computation 28 State Management by Operating System RUNNING READY QUEUE CONDITION QUEUE(x) CONDITION QUEUE(y)

2016-06-10 Structuring the Computation 29 Operations of Operating System dispatch module in operating system is ADT supporting these operations: enqueue: Queue Descriptor Queue dequeue: Queue Queue Descriptor empty: Queue Boolean each clock interrupt executes this operation: Suspend and Select() { RUNNING = process status; READY QUEUE.enqueue(RUNNING); RUNNING = READY QUEUE.dequeue(); process status = RUNNING; }

2016-06-10 Structuring the Computation 30 Implementation of Semaphore Suspend on Condition(c) { RUNNING = process status; CONDITION QUEUE(c).enqueue(RUNNING); RUNNING = READY QUEUE.dequeue(); process status = RUNNING; } Awaken(c) { RUNNING = process status; READY QUEUE.enqueue(RUNNING); READY QUEUE.enqueue(CONDITION QUEUE(c).dequeue()); RUNNING = READY QUEUE.dequeue(); process status = RUNNING; }

2016-06-10 Structuring the Computation 31 Implementation of Monitor mutual exclusion by disabling interrupts while being in the monitor Continue(c) { RUNNING = process status (interrupts enabled, program counter set to return point from monitor call); READY QUEUE.enqueue(RUNNING); if not CONDITION QUEUE(c).empty() { READY QUEUE.enqueue(CONDITION QUEUE(c).dequeue()); } RUNNING = READY QUEUE.dequeue(); process status = RUNNING; }

2016-06-10 Structuring the Computation 32 Implementation of Rendezvous (1) each entry has descriptor with these fields: O: Boolean; true if entry is open W: waiting queue (task descriptors of callers) T: descriptor of task owning entry I: pointer to first instruction of accept body Call Entry(e) { RUNNING = process status; DESCR(e).W.enqueue(RUNNING); if DESCR(e).O { for all entries oe of DESCR(e).T do { oe.o = false; } RUNNING = DESCR(e).T; RUNNING.ip = DESCR(e).I; } else { RUNNING = READY QUEUE.dequeue(); } process status = RUNNING; }

2016-06-10 Structuring the Computation 33 Implementation of Rendezvous (2) At End Of Accept Body(e) { RUNNING = process status; READY QUEUE.enqueue(DESCR(e).W.dequeue()); READY QUEUE.enqueue(RUNNING); RUNNING = READY QUEUE.dequeue(); process status = RUNNING; } Execute Accept Statement(e) { if DESCR(e).W.empty() { DESCR(e).O = true; DESCR(e).T = process status; RUNNING = READY QUEUE.dequeue(); process status = RUNNING; } else simply continue executing the accept body }

2016-06-10 Structuring the Computation 34 Implementation of Rendezvous (3) Execute Select Statement() { LOE = list of open entries in selection; if LOE is empty { raise Program Error; } else { if DESCR(e).W.empty() (for all e in LOE) { for all e in LOE do { DESCR(e).O = true; DESCR(e).T = process status; } RUNNING = READY QUEUE.dequeue(); process status = RUNNING; } else { choose an e with not DESCR(e).W.empty(); } } proceed execution from instruction DESCR(e).I; }