Chair of Software Engineering 1 Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan.

Slides:



Advertisements
Similar presentations
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Advertisements

Ch 7 B.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
Intertask Communication and Synchronization In this context, the terms “task” and “process” are used interchangeably.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Monitors Chapter 7. The semaphore is a low-level primitive because it is unstructured. If we were to build a large system using semaphores alone, the.
1 Semaphores and Monitors: High-level Synchronization Constructs.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
Chair of Software Engineering Concurrent Object-Oriented Programming Concurrent Object-Oriented Programming Bertrand Meyer, Piotr Nienaltowski.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chair of Software Engineering 1 Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Chair of Software Engineering 1 Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan.
Chair of Software Engineering 1 Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan.
Concurrency in Ada What concurrency is all about Relation to operating systems Language facilities vs library packages POSIX threads Ada concurrency Real.
Concurrency CS 510: Programming Languages David Walker.
Chapter 11: Distributed Processing Parallel programming Principles of parallel programming languages Concurrent execution –Programming constructs –Guarded.
Monitors CSCI 444/544 Operating Systems Fall 2008.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chair of Software Engineering Concurrent Object-Oriented Programming Concurrent Object-Oriented Programming Bertrand Meyer, Piotr Nienaltowski.
02/25/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
University of Pennsylvania 9/28/00CSE 3801 Concurrent Programming (Critical Regions, Monitors, and Threads) CSE 380 Lecture Note 6 Insup Lee.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings 1.
12/1/98 COP 4020 Programming Languages Parallel Programming in Ada and Java Gregory A. Riccardi Department of Computer Science Florida State University.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrent Languages – Part 1 COMP 640 Programming Languages.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 12 Concurrency can occur at four levels: 1. Machine instruction level 2. High-level language.
1 Programming Languages and the Software Production Process Informal Cardelli’s metrics of programming languages fitness to real-time applications: Economy.
1 Concurrency Architecture Types Tasks Synchronization –Semaphores –Monitors –Message Passing Concurrency in Ada Java Threads.
Internet Software Development Controlling Threads Paul J Krause.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Lecture 8 Page 1 CS 111 Online Other Important Synchronization Primitives Semaphores Mutexes Monitors.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Abstraction ADTs, Information Hiding and Encapsulation.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
Operating Systems Lecture Notes Synchronization Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Operating System Chapter 5. Concurrency: Mutual Exclusion and Synchronization Lynn Choi School of Electrical Engineering.
Synchronicity II Introduction to Operating Systems: Module 6.
“Language Mechanism for Synchronization”
Outline Other synchronization primitives
Other Important Synchronization Primitives
Inter-Process Communication and Synchronization
Concurrency: Mutual Exclusion and Synchronization
Monitors Chapter 7.
Midterm review: closed book multiple choice chapters 1 to 9
Threading And Parallel Programming Constructs
Multithreaded Programming
Critical section problem
Monitors Chapter 7.
Subject : T0152 – Programming Language Concept
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Monitors Chapter 7.
Presentation transcript:

Chair of Software Engineering 1 Concurrent Object-Oriented Programming Arnaud Bailly, Bertrand Meyer and Volkan Arslan

Chair of Software Engineering 2 Lecture 3: Coordination Primitives

Chair of Software Engineering 3 Coordination  Objects have a state.  The set of available routines may depend on the current state.  The Queue exhibits such a non-uniform service.  Activity-centered coordination:  clients coordinate to prevent interfering actions.  Invocations are never delayed.  Objects are input-enabled.  Boundary coordination:  the supplier may delay the acceptance of a call.  Introduction of synchronization code.

Chair of Software Engineering 4 Issues about Synchronization code How to write it? Where to place it? Can it be re-used?

Chair of Software Engineering 5 Activity-Centered Coordination  Multiple Clients to 1 supplier:  the goal is avoiding interference!  1 client accessing the supplier at a time.  This is mutual exclusion.  One can use “traditional” mechanisms:  polling on a shared variable (busy-wait),  using semaphores,  using conditional critical regions, and  exchanging messages among clients.  The synchronization code is on the client side.

Chair of Software Engineering 6 Semaphores [Dijkstra 68]  A semaphore s has:  an integer value val,  two atomic operations wait and signal,  a queue of waiting calls.  When calling s.wait:  if val>0 then do val := val -1 and return,  else suspend and queue this call.  When calling s.signal:  if there is a queued call resume it,  else do val := val + 1.

Chair of Software Engineering 7 Semaphore Implemented in Java class Semaphore { private int val; public Semaphore (int n) {val = n;} public synchronized sem_wait () { // P if (val <= 0) this.wait (); val = val – 1; } public synchronized sem_signal () { // V this.notify (); //no effect if no thread is waiting val = val + 1; }

Chair of Software Engineering 8 Semaphore as a Mutex in Java class TOOLBOX{ private int resource, number; private Semaphore s; public Example (int n) {number = n; s=new Semaphore (1);} private void ex1() { s.sem_wait(); // critical section resource = 10; resource += 1; s.sem_signal (); // end of critical section } private void ex2() { s.sem_wait(); // critical section resource = 4; resource += 3; s.sem_signal (); // end of critical section } public static void main () { new Example (1).ex1; new Thread (new Runnable (public void run() {ex2();})); }

Chair of Software Engineering 9 Conditional Critical Region  [Brinch Hansen 72] and [Hoare 72].  Group variables into resources: resource r is v, w,…,z;  The critical region instruction is: region r when Condition then StatementList end  Executes StatementList only when:  resources are available,  Condition is verified.  Resources are held during execution and freed on termination.

Chair of Software Engineering 10 Using CCR in SCOOP-Like Syntax class TOOLBOX // producer-consumer variation create make feature make (aa, ab, ac: STREAM) is do a:=aa; b:=ab; c:=ac end merge is do from until false loop do_merge (a, b, c) end do_merge (a, b, c: STREAM) is require not a.empty and not b.empty do c.put_string (a) c.put_string (b) end

Chair of Software Engineering 11 Using Message-passing  Solving variants of the consensus problem.  Implementing distributed mutual exclusion.  Confere the DSM exercise.  Many other solutions:  distributed semaphores,  solutions tolerating faults…

Chair of Software Engineering 12 Boundary Coordination  The synchronization code is in the supplier class.  Possible solutions:  live routines (POOL, ABCL/1),  monitors (Ada95),  delay Queues (Hybrid [Nierstrasz 92]),  behavior abstractions (ACT++[Kafura & Lee 89]),  Enable Sets (Rosette [Tomlinson & Singh 89]),  method guards (Guide [Hagimont & al. 94]),  path expressions (Procol [Bos & Laffra 90]).  Along the way, find solutions to control intra-object concurrency.

Chair of Software Engineering 13 Monitors  A monitor is a language unit.  It exhibits entries that can be called from outside.  Only one entry executes at any time.  Operations to interrupt and resume execution.  Not unlike Java monitors.  This allows clients to synchronize.  All the synchronization code is enclosed inside the monitor.  (A form of) modularity is obtained.  Idea: chosen classes could yield monitor objects!  Protected objects in Ada 95.

Chair of Software Engineering 14 Semaphore in Ada 95 protected type Semaphore (Initial : Natural :=0) is entry Wait; -- also known as P procedure Signal; -- also known as V private Value : Natural := Initial; end Semaphore; protected body Semaphore is entry Wait when Value > 0 is begin Value := Value – 1; end Wait; procedure Signal is begin Value := Value + 1; end Signal; end Semaphore;

Chair of Software Engineering 15 Differences with Java  Java uses condition variables.  Ada uses conditional wait (no notify()!).  Java allows non-synchronized methods.  Ada enforces synchronization among all entries.  Java has one waiting queue per object.  Ada has one waiting queue per entry.  Java’s are queues are unordered.  Ada queues are FIFO.  In Java, which object is notify’ed is unknown.  In Ada, it is the head of the queue.  In Java, re-entrant calls are allowed.

Chair of Software Engineering 16 The Eggshell Model  In Ada, the notified task gets to be executed immediately (immediate resumption).  Tasks inside the eggshell have priority over the ones outside.  There is a requeue statement (even between distinct objects). Protected object Entry1 Entry2 c1 c4 c2 c3

Chair of Software Engineering 17 Delay Queues  Each Object executes one routine at a time.  Explicit management of queues, with queue objects.  Each routine (entry) is linked to a queue.  A queue Q can be either closed or open.  There are methods Q.open() and Q.close().  Very similar to include/exclude primitives.

Chair of Software Engineering 18 BBuffer Using Hybrid Primitives class BBUFFER is public interface: put (t: OBJECT); OBJECT get(); implementation: private putQ, getQ : DELAYQUEUE; Boolean isFull, isEmpty; put (t: OBJECT) link PutQ is … getQ.open (); if (isFull) then PutQ.close(); end; OBJECT: get () link getQ is … putQ.open () if (isEmpty) then getQ.close (); end; end BBUFFER;

Chair of Software Engineering 19 Behavior Abstractions  Classes have a behavior section.  behavior associates enabled routines to states.  become give transitions between states.  The state description is close to the interface.  Related to Actor languages.

Chair of Software Engineering 20 BBuffer using ACT++ Primitives class BBUFFER is public interface: … // as before behavior: empty= {put} partial= {put, get} full= {get} implementation: Boolean isFull, isEmpty; put (t: OBJECT) is … if (isFull) then become full; else become partial; end; OBJECT: get () is … if (isEmpty) then become empty; else become partial; end; end BBUFFER;

Chair of Software Engineering 21 Enable Sets  Enable sets are first-class objects.  An enable set is a set of (allowed) method names.  The both method allows combinations of sets. class BBUFFER is public interface: … // as before implementation: Enable empty () {return enable(put);} Enable partial () {return both(full(),empty());} Enable full() {return enable(get)} Boolean isFull, isEmpty; // the rest is identical to the ACT++ code. end BBUFFER;

Chair of Software Engineering 22 Variation on Enable Sets  A become statement can call next of class EnableSet. class BBUFFER is public interface: … // as before implementation: private static EnableSet: next() is if isFull() return new EnableSet(get); if isEmpty() return new EnableSet(put); return new EnableSet(put, get); end put (t: OBJECT) is … become next(); end; OBJECT: get () is … become next(); end; end BBUFFER;

Chair of Software Engineering 23 Method Guards (Guide)  Each method has a guard that enables/disables it.  Guards define (implicitly) the possible states.  After each method execution guards are evaluated.  There is no explicit transitions in the code! class BBUFFER is public interface: … // as before guards: put: !isFull() get: !isEmpty() implementation: put (t: OBJECT) is … /* no code for transitions */ end; OBJECT get is … /* no code for transitions */ end; end BBUFFER;

Chair of Software Engineering 24 Path Expressions (Procol)  A path expression is a regular expression.  It defines a finite-state automaton.  The disjunction represents non-determinism. class BBUFFER3 is public interface: … // as before path: (put, get|(put,get|(put,get)*)*)* implementation: put (t: OBJECT) is … /* no code for transitions */ end; OBJECT get is … /* no code for transitions */ end; end BBUFFER;

Chair of Software Engineering 25 Controlling intra-object concurrency  In GUIDE, for a method m the guards can feature:  invoked (m), started (m), completed (m),  current (m) = started (m) – completed (m)  pending (m) = invoked (m) – started (m)  Compatibility annotations [Löhr 92] tag methods that can be executed in parallel with others.  In CEiffel:  no annotation implies mutual exlusion,  “foo is --|| bar-- …” foo and bar can execute in parallel,  “foo is --||-- …” no restriction in parallelism.

Chair of Software Engineering 26 Achieving Modularity [OOSC, Chap 3]  A module is a programming artifact that is:  autonomous,  self-contained.  In O-O languages: a module is a class.  Respects the Linguistic Modular Unit and Open- Closed Principles.  Respects the information hiding rule.  Relevance can be assessed with criteria:  composability and decomposability,  understandability,  continuity.  If successful, obtain components.

Chair of Software Engineering 27 Boundary Coordination  Tends to break encapsulation:  clients aware of supplier’s state,  supplier implementation details do matter.  Tends to make re-use harder. class Client inherits Thread { private BBuffer b; … public void run() { b.notFull.sem_wait(); b.put(new Object()); b.notEmpty.signal(); } class BBuffer { public Semaphore notFull, notEmpty; public void put(o: Object) { … } public Object get() { … }

Chair of Software Engineering 28 Activity-centered coordination  Makes supplier behavior Encapsulated.  Convenience of re-use may vary.  Synchronization code can be:  interwoven with functional code, or  isolated from it.  Synchronization code should be Separable.

Chair of Software Engineering 29 To be continued…  Inheritance anomaly  Contracts for concurrent objects