Lecture 6. Introduction to Message Passing

Similar documents
Message Passing. Lecture 7. Shared Variables? Shared Variables? Synchronisation. Overview of 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

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

The Abstract Behavioral Specification Language

Models and languages of concurrent computation

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

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

Lecture 8. Linda and Erlang

Part Two - Process Management. Chapter 3: Processes

Concurrent Programming TDA381/DIT390

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

Interprocess Communication

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

Today: Synchronization. Recap: Synchronization

Synchronization problems with semaphores

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

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

Chapter 4: Processes

Homework 12 Due Wednesday, 12/8/10

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

Diagram of Process State Process Control Block (PCB)

System Software Assignment 8 Deadlocks

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

The Big Picture So Far. Chapter 4: Processes

Reminder from last time

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Performance Throughput Utilization of system resources

The Big Picture So Far. Chapter 4: Processes

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

An Introduction to Parallel Systems

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

Chapter 4: Processes. Process Concept. Process State

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

What is a thread anyway?

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

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

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!

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

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

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

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

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

CS370 Operating Systems Midterm Review

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

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

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

Today s topic: Real Time Programming with Ada

Chapter 3: Process Concept

Multitasking / Multithreading system Supports multiple tasks

CHAPTER 3 - PROCESS CONCEPT

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

Chapter 3: Processes

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

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

Lecture 2 Process Management

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

Inheritance and Interfaces

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

Start of Lecture on January 20, Chapter 3: Processes

! 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

Processes. Process Concept

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

Today s topic: Real Time Programming with Ada

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

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

Chapter 3: Processes. Operating System Concepts 8th Edition

G52CON: Concepts of Concurrency

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

Synchronization SPL/2010 SPL/20 1

Processes. Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra

Lecture 11: More on invariants and textual reasoning - examples. K. V. S. Prasad Dept of Computer Science Chalmers University Friday 30 Sep 2016

Chapter 3: Processes. Operating System Concepts 9 th Edition

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

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

Reminder from last time

Introduction to OS Synchronization MOS 2.3

Advances in Programming Languages

Learning from Bad Examples. CSCI 5828: Foundations of Software Engineering Lecture 25 11/18/2014

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

Inter-process communication (IPC)

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

Concurrent Programming

Programming Languages

Last Class: Synchronization

A NOTE ON CORRECTLY GATHERING RESULTS FROM JR S CONCURRENT INVOCATION STATEMENT

Collaboration of Tasks

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

Distributed Programming

Chapter 3: Process Concept

Transcription:

Lecture 6 Introduction to Message Passing

Message Passing Introduction to message passing JR s message passing model Operations Asynchronous message passing Synchronous message passing 2

Shared Variables? So far we 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 3

Shared Variables? Shared state is the main source of synchronisation problem critical section Locks Semaphores Monitors Can we throw away the shared state? Message passing 4

Overview of Message Passing One process sends a message Another process awaits for 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 5

Synchronisation Consider the behaviour of the sender of a message Asynchronous send Send and continue working (e-mail, SMS) Synchronous send Send and wait for the message to be received (fax) Rendezvous / Remote invocation send and wait for reply (phone call) 6

Examples 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 7

Naming 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 8

Naming Three most common disciplines cont. Direct asymmetric Erlang Sockets A Send to B B Receive 9

Naming 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 A Send to C C B Receive from C 10

Message Passing in JR Operations Key JR extension of Java Servicing operations Method Receive statement Input statement Invocations Call Send 11

Message Passing in JR Operations Key JR extension of Java Servicing operations Method Receive statement Input statement Invocations Call Send Today Next lecture 12

Operations 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 13

Servicing Operations 1 Methods Full syntax private op void buy(); private void buy() { while (true) { window.flash("buy @ Cremona!"); JR.nap(buy_pause); 14

Servicing Operations 1 Methods Shorthand private op void buy() { while (true) { window.flash("buy @ Cremona!"); JR.nap(buy_pause); 15

Op-methods Call Ordinary Java method call... Cremona c = new Cremona();... c.buy();... Explicit call statement... call c.buy();... 16

Op-methods Send 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... Cremona c = new Cremona();... send c.buy();... 17

Dynamic Process Creation Process declarations are only a shorthand private process buy { //Code private op void buy(); private void buy() { //Code public Constructor( ) { send buy(); 18

Process Families Process families are also a shorthand private process buy ((quantifier),, (quantifier)) { //Code public Constructor( ) { for (quantifier) for (quantifier) send buy(); 19

Op-methods: Send vs Call Operation op( ) serviced by a method call op( ) send op( ) op( ) op( ) 20

Servicing Operations 2 Message queues channels No corresponding method, but Unbounded buffer of messages Return type must be void send op void channel( ) receive inni call 21

Channels Receive receive statement receive op(x1,..., xn); 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 22

Channels Send send statement send op(exp1,..., expn); 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 23

Example 1 private op void ch(int x); Book notation chan ch(int) private process p { send ch(1); send ch(2); private process q { int x,y; receive ch(x); receive ch(y); Two sends from the same source implies x will get 1, and y will get 2 24

Example 2 private op void ch1(int x); private op void ch2(int x); private process p { send ch1(1); send ch2(2); private process q { int x,y; receive ch1(x); receive ch1(y); private process r { send ch1(3); send ch2(4); private process s { int x,y; receive ch2(x); receive ch2(y); 25

Expressive Power 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 high-level language is not normally a good idea 26

There is No Semaphore Semaphore sem s = N P(s); V(s); op void s(); for(int x=0;x<n;x++) send s(); receive s(); send s(); Channel 27

Expressive Power 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 28

Barrier Synchronisation Revisited N processes must wait for the slowest before continuing with the next activity Widely used in parallel programming p1 p2 p3 p4 waiting for others to finish continue with next activity 29

Barrier Synchronisation Exercise 2 Ball freezing with two semaphores? op void done(); op void go(); process ball ((int i=0;i<4;i++)) { //move send done(); process coordinator { receive go(); for(int i=0;i<4;i++) receive done(); for(int i=0;i<4;i++) send go(); 30

Barrier Synchronisation Exercise 2 Ball freezing with two semaphores? op void done(); op void go(); process ball ((int i=0;i<4;i++)) { //move send done(); process coordinator { receive go(); for(int i=0;i<4;i++) receive done(); for(int i=0;i<4;i++) A fast ball send go(); can steal go 31

Barrier Synchronisation Exercise 2 Ball freezing with N+1 semaphores op void done(); cap void() go[]; //some init process ball ((int i=0;i<4;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](); 32

Operation Capabilities 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; 33

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(); 34

Operation Capabilities 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 35

Client-Server Interaction Common asynchronous communication pattern For example: a web server handles requests for web pages from clients (web browsers) client request client result server client client 36

Simple Client-Server Model First attempt op void request( ); op void result( ); process server { while (true) { receive request( ); // process request send result( ); process client { send request( ); // possibly do // something else receive result( ); 37

Simple Client-Server Model First attempt op void request( ); op void result( ); process server { while (true) { receive request( ); // process request send result( ); process client { send request( ); Many clients: // possibly do Who gets the // something else result? receive result( ); 38

A Private Operation 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 39

Private Channel process server { while (true) { receive request(replychannel, ); // process request send replychannel( ); process client { op void myreplychannel(resulttype); send request(myreplychannel, ); // possibly do something else receive myreplychannel( ); 40

Resource Allocation Single 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 41

Resource Allocation 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 42

Resource Allocation private process server { cap void(e) rc; Request action; E unit; while (true) { receive request(rc, action, unit); if (action == Request.Allocate) if (units.isempty()) pending.add(rc); else send rc(units.remove()); else if (pending.isempty()) units.add(unit); else send (pending.remove())(unit); 43

call statement Channels Call 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 44

Channels: Send vs Call Operation op( ) serviced by a channel call op( ) send op( ) receive op( ) receive op( ) call op( ) send op( ) 45

Channels: Send vs Call Operation op( ) serviced by a channel call op( ) send op( ) call op( ) send op( ) receive op( ) receive op( ) 46

Sieve of Eratosthenes 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 47

Architecture nums N+1 pipeline channels One shared output channel filter1 filter2 filtern print output eat 48

Constructor and Print 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 print { int number; while (true) { receive output(number); System.out.println(number); 49

The Ends of the Pipeline public process nums { for(int i=3;i<(20*n);i+=2) { call pipeline[0](i); public process eat { int number; while (true) receive pipeline[n](number); 50

The Filters public process filter((int i=0;i<n;i++)) { int prime, number; receive pipeline[i](prime); call output(prime); while (true) { receive pipeline[i](number); if (number%prime > 0) call pipeline[i+1](number); 51

Summary call op( ) call op( ) op( ) receive op( ) send op( ) send op( ) op( ) receive op( ) 52

Operations Methods Channels Invocations Asynchronous Synchronous Summary Next time Remote invocation / Rendezvous 53