Message Passing. Lecture 7. Shared Variables? Shared Variables? Synchronisation. Overview of Message Passing

Similar documents
Lecture 6. Introduction to Message Passing

G52CON: Concepts of Concurrency Lecture 15: Message Passing. Gabriela Ochoa School of Computer Science & IT

Concurrent Programming. Introduction

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

Chapter 4: Processes. Process Concept

Chapter 4: Processes

Models and languages of concurrent computation

Lecture 9. Part I. Overview of Message Passing. Communication Coupling. Decoupling Blackboard. Decoupling Broadcast. Linda and Erlang.

The Abstract Behavioral Specification Language

Interprocess Communication

Lecture 8. Linda and Erlang

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

Message Passing. Frédéric Haziza Summer Department of Computer Systems Uppsala University

Homework 12 Due Wednesday, 12/8/10

Concurrent Programming TDA381/DIT390

Part Two - Process Management. Chapter 3: Processes

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

Outline. Interprocess Communication. Interprocess Communication. Communication Models: Message Passing and shared Memory.

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

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

Chapter 4: Processes

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

Chapter 4: Processes. Process Concept

Erlang. Functional Concurrent Distributed Soft real-time OTP (fault-tolerance, hot code update ) Open. Check the source code of generic behaviours

Reminder from last time

Deadlock. Lecture 4: Synchronization & Communication - Part 2. Necessary conditions. Deadlock handling. Hierarchical resource allocation

Chapter 4: Processes

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

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

Diagram of Process State Process Control Block (PCB)

Synchronization problems with semaphores

System Software Assignment 8 Deadlocks

The Big Picture So Far. Chapter 4: Processes

Today: Synchronization. Recap: Synchronization

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

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

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

Synchronization II: EventBarrier, Monitor, and a Semaphore. COMPSCI210 Recitation 4th Mar 2013 Vamsi Thummala

Performance Throughput Utilization of system resources

The Big Picture So Far. Chapter 4: Processes

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS370 Operating Systems Midterm Review

An Introduction to Parallel Systems

Process Concept Process Scheduling Operations On Process Inter-Process Communication Communication in Client-Server Systems

Chapter 4: Processes. Process Concept. Process State

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

Chapter 3: Processes

CHAPTER 3 - PROCESS CONCEPT

What is a thread anyway?

re-exam Concurrent Programming tda383/dit390 Date: Time: 14:00 18:00 Place: Maskinhuset (M)

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

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

Start of Lecture on January 20, Chapter 3: Processes

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

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

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

Exercise (could be a quiz) Solution. Concurrent Programming. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - IV Threads

Chapter 3: Processes. Operating System Concepts 8th Edition

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

Inter-process communication (IPC)

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

Chapter 3: Processes. Operating System Concepts 9 th Edition

Advances in Programming Languages

6/20/2018. Lecture 5: Real Time Messages

RPC and RMI. 2501ICT Nathan

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

Collaboration of Tasks

Chapter 3: Process Concept

Multitasking / Multithreading system Supports multiple tasks

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

Chapter 3: Process Concept

Concurrent Programming (RIO) Lesson 9. Ch 8 [BenA 06] Messages Channels Rendezvous RPC and RMI. X=f(..); send X to B. OS kernel.

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

Lecture 2 Process Management

Chapter 3: Processes. Operating System Concepts 9 th Edition

Processes. CSE 2431: Introduction to Operating Systems Reading: Chap. 3, [OSC]

CS4961 Parallel Programming. Lecture 4: Data and Task Parallelism 9/3/09. Administrative. Mary Hall September 3, Going over Homework 1

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

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

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

Processes. Process Concept

Synchronization Principles II

Processes. Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra

Structuring the Computation. Structuring the Computation

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

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03

G52CON: Concepts of Concurrency

Synchronization SPL/2010 SPL/20 1

Other Interprocess communication (Chapter 2.3.8, Tanenbaum)

Recap. Contents. Reenterancy of synchronized. Explicit Locks: ReentrantLock. Reenterancy of synchronise (ctd) Advanced Thread programming.

Reminder from last time

i219 Software Design Methodology 11. Software model checking Kazuhiro Ogata (JAIST) Outline of lecture

Chapter 3: Process Concept

Today s topic: Real Time Programming with Ada

Chapter 3: Process Concept

Process. Operating Systems (Fall/Winter 2018) Yajin Zhou ( Zhejiang University

What is a Class Diagram? A diagram that shows a set of classes, interfaces, and collaborations and their relationships

What is a Class Diagram? Class Diagram. Why do we need Class Diagram? Class - Notation. Class - Semantic 04/11/51

Favoring Isolated Mutability The Actor Model of Concurrency. CSCI 5828: Foundations of Software Engineering Lecture 24 04/11/2012

Transcription:

Message Passing Lecture 7 Introduction to Message Passing Introduction to message passing JR s message passing model Operations Asynchronous message passing Synchronous message passing 2 Shared Variables? Shared Variables? So far we have considered synchronisation mechanisms based on shared variables Concurrent programs require hardware in which processors share memory SMP What about NUMA or similar architectures? Networked (distributed) architectures are not based on shared memory Message passing is the natural model for distributed systems and alike Shared state is the main source of synchronisation problems critical section Locks Semaphores Monitors Can we throw away the shared state? Message passing 3 4 Overview of Message Passing Synchronisation One process sends a message Another process awaits a message We will consider two dimensions of this approach: What form of synchronisation is required What form of process naming is involved in message passing Consider the behaviour of the sender of a message Synchronous send Send and wait for the message to be received (fax) Asynchronous send Send and continue working (e-mail, SMS) Rendezvous / Remote invocation send and wait for reply (phone call) 5 6 1

Examples Naming JR combines all three types of MP Erlang has asynchronous MP Ada has rendezvous Previously used at Chalmers as a main teaching language Java has libraries Sockets asynchronous message passing RMI can be seen as synchronous MP Normal method invocation can also be seen as synchronous MP How do sender and receiver refer to each other when message passing is used? Three most common disciplines Direct symmetric A Send to B B Receive from A 7 8 Naming Naming Three most common disciplines cont. Direct asymmetric Erlang (actor languages) Sockets A B Three most common disciplines cont. Indirect Naming a process is not always convenient Naming an intermediary can be more flexible A channel, service name, or a mailbox Potentially many-to-many communication Send to B Receive A B Send to C C Receive from C 9 10 Message Passing in JR Operations Operations Key JR extension of Java Servicing operations Method Receive statement Input statement Invocations Call Send Generalisation of methods Syntax: keyword op private op void buy(); public op E acquire(int n); Specifies parameter and return types Parameter names are unimportant Can be serviced in several ways 11 12 2

Servicing Operations 1 Servicing Operations 1 Methods Full syntax Methods Shorthand private op void buy(); private void buy() { window.flash("buy @ Cremona!"); JR.nap(buy_pause); private op void buy() { window.flash("buy @ Cremona!"); JR.nap(buy_pause); 13 14 Op-methods Call Op-methods Send Ordinary Java method call Cremona c = new Cremona(); c.buy(); Asynchronous send statement Starts a new process Runs the servicing method in the new process The return value is discarded The caller continues execution independently Explicit call statement call c.buy(); Cremona c = new Cremona(); send c.buy(); 15 16 Op-methods: Send vs Call Dynamic Process Creation Operation op( ) serviced by a method Process declarations are only a shorthand op( ) op( ) private process buy { //Code private op void buy(); private void buy() { //Code public Constructor( ) { send buy(); 17 18 3

Process Families Process families are also a shorthand private process buy ((quantifier),, (quantifier)) { //Code public Constructor( ) { for (quantifier) for (quantifier) send buy(); Servicing Operations 2 Message queues channels No corresponding method, but Unbounded buffer of messages Return type must be void send call op void channel( ) receive inni 19 20 Channels Receive Channels Send receive statement send statement receive op(x1,, xn); send op(exp1,, expn); Wait for a message on the named channel op Atomically remove the first message and put the fields of the message into the variables x1,, xn Evaluate the expressions exp1,,expn and produce a message M Atomically append M to the end of the named channel op Send is a non-blocking action Asynchronous message passing 21 22 Example 1 Example 2 private op void ch(int x); Book notation chan ch(int) private op void ch1(int x); private op void ch2(int x); private process p { send ch(1); send ch(2); Two sends from the same source implies private process p { send ch1(1); send ch2(2); private process r { send ch1(3); send ch2(4); private process q { int x,y; receive ch(x); receive ch(y); x will get 1, and y will get 2 private process q { int x,y; receive ch1(x); receive ch1(y); private process s { int x,y; receive ch2(x); receive ch2(y); 23 24 4

Expressive Power This is not a pipe René Magritte Semaphores and monitors Equally expressive Any synchronisation with await statement Asynchronous message passing vs. XXX Can we implement semaphores? Can we implement monitors? Important theoretical question An illustrative example, but not normal practice Implementing a low-level language construct in a highlevel language is not normally a good idea 25 26 This is not a Semaphore Expressive Power Semaphore sem s = N P(s); V(s); Channel op void s(); for(int x=0;x<n;x++) send s(); receive s(); send s(); Semaphores are asynchronous channels without values JR (message passing) is implemented using monitors in Java The same expressive power Can implement any await statement Important theoretical result 27 28 Barrier Synchronisation Revisited Barrier Synchronisation Exercise 2 N processes must wait for the slowest before continuing with the next activity Widely used in parallel programming waiting for others to finish p1 p2 p3 p4 continue with next activity Ball freezing with two semaphores? op sem void done done(); = 0; op sem void go go(); = 0; process ball ((int i=0;i<4;i++)) i i { //move send done(); receive go(); A fast ball can steal go process coordinator { for(int i=0;i<4;i++) receive done(); for(int i=0;i<4;i++) send go(); 29 30 5

Barrier Synchronisation Exercise 2 Operation Capabilities Ball freezing with N+1 semaphores op void done(); cap void() go[]; //some init process ball ((int i=0;i<4;i++)) i i { //move send done(); process coordinator { receive go[i](); for(int i=0;i<4;i++) receive done(); for(int i=0;i<4;i++) send go[i](); A reference to an operation Just as an ordinary Java object reference Usage Variables Passing as parameters Dynamic operation creation Example: private op void buy(); private cap void() ref = buy; 31 32 Operation Capabilities Operation Capabilities Example Dynamic operation creation Array of operations (semaphores) private cap void() go[]; public Ball() { go = new cap void()[4]; for(int i=0;i<4;i++) go[i] = new op void(); Capabilities can be tested for equality Both == and!= work Only the type signatures must match Special cases Capabilities are references null is a valid capability value Special operation value noop is also provided Infinite sink Receiving from noop blocks forever 33 34 Client-Server Interaction Simple Client-Server Model Common asynchronous communication pattern For example: a web server handles requests for web pages from clients (web browsers) client client request result server client client First attempt process server { receive request( ); // process request send result( ); Many clients: Who gets the result? op void request( ); op void result( ); process client { send request( ); // possibly do // something else receive result( ); 35 36 6

A Private Operation Private Channel We need to pass a reference to a private reply channel Operation capabilities are operation references op void request(cap void(resulttype) res, ); Each client needs to create a private operation as a reply channel process server { cap void(resulttype) replychannel; receive request(replychannel, ); // process request send replychannel( ); process client { op void myreplychannel(resulttype); send request(myreplychannel, ); // possibly do something else receive myreplychannel( ); 37 38 Resource Allocation Single Resource Allocation A controller controls access to copies of some resource Clients make requests to take (acquire) or return (release) one resource A request should only succeed if there is a resource available, Otherwise the request must block Adapt the passing the condition solution with explicit queue of requests instead of condition variable public class ResourceAllocator<E> { public enum Request {Allocate, Release; public op void request(cap void(e), Request, E); private Queue<E> units = new ArrayDeque<E>(); private Queue<cap void(e)> pending = new ArrayDeque<cap void(e)>(); //next slide 39 40 Resource Allocation Channels Call private process server { cap void(e) rc; Request action; E unit; receive request(rc, action, unit); if (action == Request.Allocate) if (units.isempty()) pending.add(rc); dd( else send rc(units.remove()); else if (pending.isempty()) units.add(unit); else send (pending.remove())(unit); call statement call op(exp1,, expn); Evaluate the expressions exp1,,expn and produce a message M Atomically append M to the end of the named channel op, and Wait until the message is received Synchronous message passing 41 42 7

Channels: Send vs Call Operation op( ) serviced by a channel Channels: Send vs Call Operation op( ) serviced by a channel 43 44 Sieve of Eratosthenes Architecture Starting with the sequence 2,3,4, A pipeline of sieves (filters) is arranged in a line Each filter outputs the first number received from left (a prime!) For each subsequent number received from the left: discard it if divisible by the first number pass to the right otherwise nums N+1 pipeline channels One shared output channel filter1 filter2 filtern print output eat 45 46 Constructor and Print The Ends of the Pipeline public Sieve(int N) { this.n = N; pipeline = new cap void(int)[n+1]; for(int i=0;i<(n+1);i++) pipeline[i] = new op void(int); public process nums { for(int i=3;i<(20*n);i+=2) { call pipeline[0](i); public process print { int number; receive output(number); System.out.println(number); public process eat { int number; while (true) receive pipeline[n](number); 47 48 8

The Filters Summary public process filter((int i=0;i<n;i++)) { int prime, number; op( ) receive pipeline[i](prime); call output(prime); receive pipeline[i](number); if (number%prime > 0) call pipeline[i+1](number); op( ) 49 50 Summary Operations Methods Channels Invocations Asynchronous Synchronous Next time Remote invocation / Rendezvous 51 9