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

Size: px
Start display at page:

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

Transcription

1 Lecture 10 Erlang

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

3 Functional Haskell call-by-need (lazy) Expression are evaluated just-in-time Hard to predict memory and execution behaviour Erlang call-by-value (strict) Combinator style OK Direct pattern matching tail recursion Process tail recursion Constant memory usage 3

4 Recursion -module(list_stuff). -export([append/2, reverse/1]). append([x Xs], Ys) -> [X append(xs, Ys)]; average([], Ys) -> Ys. reverse([h T]) -> append(reverse(t), [H]); reverse([]) -> []. 4

5 Tail Recursion -module(list_stuff). -export([reverse/1, append/2]). reverse(xs) -> reverse_a(xs, []). reverse_a([x Xs], Acc) -> reverse_a(xs, [X Acc]); reverse_a([], Acc) -> Acc. %%continues 5

6 Tail Recursion %%continuation append(xs, Ys) -> reverse_a(r?verse(xs),ys). reverse_a(reverse(xs),ys). 6

7 Concurrent Programming Based on Message Passing: Q. What form of synchronisation? A. Asynchronous Q. What form of process naming? A. Direct, asymmetric A send MSG to B B receive anything 7

8 Asynchronous Message Passing Asynchronous send, receive from mailbox send receive Pid! Msg receive Msg-> receive Msg-> Pid! Msg 8

9 Concurrent Spawn a function evaluation in a separate thread Callout Pid = spawn( fun() -> loop(42) end) Function must use constant space Tail recursive fun() -> loop(42) end 9

10 Receive Message Order A receive statement tries to find a match as early in the mailbox as it can msg_1 msg_2 msg_1 msg_3 msg_2 msg_4 S msg_4 S receive msg_3 -> end 10

11 Receive Message Order A receive statement tries to find a match as early in the mailbox as it can msg_1 msg_2 msg_1 msg_4 S msg_4 S receive msg_4 -> msg_2 -> end 11

12 Receive Message Order A receive statement tries to find a match as early in the mailbox as it can msg_1 msg_2 msg_2 msg_4 S msg_4 S receive msg_4 -> _ -> end 12

13 Client-Server Interaction Common asynchronous communication pattern For example: a web server handles requests for web pages from clients (web browsers) client {request, Reg} client {result, Rep} server client client 13

14 Modelling Client-Server Computational server Off-loading heavy mathematical operations For example: factorial -module(math_server). -import(math_stuff,[factorial/1]). -export([start/0]). start() -> spawn(fun() -> loop(0) end). %%continues 14

15 Main Server Loop %%continuation loop(count) -> receive {factorial, From, N} -> Result = factorial(n), From! {result, Result}, loop(count+1); {get_count, From} -> From! {result, Count}, loop(count); stop -> true end. 15

16 Client Interface Encapsulating client access in a function Private channel for receiving replies? %%possible continuation compute_factorial(pid, N) -> Pid!{factorial, self(), N}, receive {result, Result} -> Result end. 16

17 References Private channel for receiving replies? Find the corresponding reply in the mailbox receive {result, Result} -> Result end BIF make_ref() Provides globally unique object different from every other object in the Erlang system including remote nodes 17

18 RPC Client Interface References to uniquely identify messages References allow only matching %%usual continuation compute_factorial(pid, N) -> Ref = make_ref(), Pid!{factorial, self(), Ref, N}, receive {result, Ref, Result} -> Result end. 18

19 RPC Main Server Loop %%continuation loop(count) -> receive {factorial, From, Ref, N} -> Result = factorial(n), From! {result, Ref, Result}, loop(count+1); {get_count, From, Ref} -> From! {result, Ref, Count}, loop(count); stop -> true end. 19

20 Registered Processes register(atom(), pid()) -> true BIF start() -> Pid = spawn(fun() -> loop(0) end), register(math_server, Pid), Pid. sending to a registered process math_server!{factorial, self(), 100}. 20

21 A Generic Server Desired features Proper reply behaviour - RPC Parameterised by the engine F Allows the engine to be upgraded dynamically Robust: does not crash if the engine goes wrong 21

22 More Generic Client-Server Higher-order functions can be used to capture the design pattern of a clientserver in a single module The main engine of a server has type : F:(State,Data) -> {Result,NewState} The state of the server before computing the query The new state of the server after the query 22

23 Main Server Loop loop(state, F) -> receive {request, From, Ref, Data} -> {R, NS} = F(State, Data), From! {result, Ref, R}, loop(ns, F); {update, From, Ref, NewF} -> From! {ok, Ref}, loop(state, NewF); stop -> true end. 23

24 Exceptions Expression evaluation can fail Examples: Bad pattern matching ({} = {1}) **exited: {{badmatch,{1}},[{erl_eval,expr,3}]}** Arithmetic error (1/0). **exited: {badarith, }** Using catch expression catch(1/0) = {'EXIT',{badarith, }} catch(1/1) = 1 24

25 More Robust Main Server Loop loop(state, F) -> receive {request, From, Ref, Data} -> case catch(f(state, Data)) of {'EXIT', Reason} -> From!{exit, Ref, Reason}, loop(state, F); {R, NewState} -> From!{result, Ref, R}, loop(newstate, F) end; 25

26 RPC Client Interface Generic client with exception passing request(pid, Data) -> Ref = make_ref(), Pid!{request, self(), Ref, Data}, receive {result, Ref, Result} -> Result; {exit, Ref, Reason} -> exit(reason) end. 26

27 Generic Server Instance -module(math_server). -import(math_stuff,[factorial/1]). -export([start/0]). start() -> g_server:start(math_server, 0, fun fx/2). fx(count, {factorial, N}) -> Result = factorial(n), {Result, Count+1}; fx(count, get_count) -> {Count, Count}. 27

28 Generic Server Instance Encapsulated client interface compute_factorial(pid, N) -> g_server:request(pid, {factorial, N}). get_count(pid) -> g_server:request(pid, get_count). 28

29 Classic Exercise Expressive power? Asynchronous message passing Yes, we know this already But this time we have direct naming Does this complicate matters? Somewhat. 29

30 Semaphore as Asynchronous Channel 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 30

31 Modelling Semaphores A process has to simulate a semaphore -module(semaphore). -export([make/1, p/1, v/1]). make(permits) -> spawn(fun() -> loop(permits) end). p(sem) -> receive? -> v(sem) -> Sem!release. 31

32 Modelling Semaphores A process has to simulate a semaphore -module(semaphore). -export([make/1, p/1, v/1]). make(permits) -> spawn(fun() -> loop(permits) end). p(sem) -> receive? -> v(sem) -> Sem!release. 32

33 Modelling Semaphores Force a response from the semaphore modelling process %%possible continuation p(sem) -> Sem!{acquire, self()}, receive acquired -> ok end. 33

34 Modelling Semaphores In fact we can do it better Using the RPC request pattern %% a better continuation p(sem) -> Ref = make_ref(), Sem!{acquire, self(), Ref}, receive {acquired, Ref} -> ok end. 34

35 Semaphore Process Loop The semaphore modelling process as a finite state machine acquire release acquire Permits>0 receive acquire -> release -> Permits==0 receive release -> release 35

36 Semaphore Process FSM Implementing FSM One function per state loop(0) -> loop_release(); loop(permits) -> loop_both(permits). loop_release() -> receive release -> loop_both(1) end. %%continues 36

37 Semaphore Process FSM %%continuation loop_both(permits) -> receive {acquire, Pid, Ref} -> Pid!{acquired, Ref}, if (Permits-1)==0 -> loop_release(); true -> loop_both(permits-1) end; release -> loop_both(permits+1) end. 37

38 Using Semaphores A silly little example -module(semuser). -export([run/0]). -import(semaphore,[p/1,v/1]). run() -> Mutex = semaphore:make(1), spawn(fun () -> loop(["hi, Ho ], Mutex) end), spawn(fun () -> loop([ Chee, Tah ], Mutex) end). %%continues 38

39 Using Semaphores %%continuation loop(msg, Mutex) -> timer:sleep(500), p(mutex), io:format( My string is ~p.~n, [Msg]), timer:sleep(500), v(mutex), loop(msg, Mutex). 39

40 Selective Receive Clauses can have guards Guards must be composed from terminating functions (BIFs) loop(permits) -> receive {acquire,pid,ref} when Permits>0 -> Pid!{acquired, Ref}, loop(permits-1); release -> loop(permits+1) end. 40

41 Semaphores, last words Before you get the wrong idea Implementing semaphores using Erlang, Java, is for comparison purposes only It is not a natural or good programming style in a language with high level concurrency primitives! Nevertheless, the semaphore implementation showed certain patterns Finite state machine Selective receive 41

42 Distributed Execution Start Erlang node erl -sname node Start another Erlang node erl -sname other Send messages between nodes It just works For example to send to a registered process {process, node@localhost}!message 42

43 Distributed Example Start the math_server on one node For example on Run: math_server:start(). Now, on another node we can off-load heavy factorial computation For example on Run: math_server:compute_factorial( 42). 43

44 Conclusions Erlang Functional Concurrent Distributed Basics 44

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

Erlang. Functional Concurrent Distributed Soft real-time OTP (fault-tolerance, hot code update ) Open. Check the source code of generic behaviours Lecture 9 Erlang Erlang Functional Concurrent Distributed Soft real-time OTP (fault-tolerance, hot code update ) Open Check the source code of generic behaviours 2 Functional Haskell call-by-need (lazy)

More information

concurrent programming XXL

concurrent programming XXL concurrent programming XXL Industrial Use of Erlang Introduction Karol Ostrovský (karol.ostrovsky@gmail.com) Motivation Machine Ericsson Blade System 3 sub-racks 14 blades 2 routers 12 compute nodes 6

More information

Message-passing concurrency in Erlang

Message-passing concurrency in Erlang Message-passing concurrency in Erlang Lecture 8 of TDA383/DIT390 (Concurrent Programming) Carlo A. Furia Chalmers University of Technology University of Gothenburg SP3 2016/2017 Today s menu Actors and

More information

Introduction to Erlang. Franck Petit / Sebastien Tixeuil

Introduction to Erlang. Franck Petit / Sebastien Tixeuil Introduction to Erlang Franck Petit / Sebastien Tixeuil Firstname.Lastname@lip6.fr Hello World % starts a comment. ends a declaration Every function must be in a module one module per source file source

More information

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

Lecture 9. Part I. Overview of Message Passing. Communication Coupling. Decoupling Blackboard. Decoupling Broadcast. Linda and Erlang. Lecture 9 Part I Linda and Erlang Linda Overview of Message Passing Communication Coupling One process sends a message Another process awaits for a message We will consider two dimensions of this approach:

More information

Lecture 8. Linda and Erlang

Lecture 8. Linda and Erlang Lecture 8 Linda and Erlang Part I Linda 2 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

More information

An Introduction to Erlang

An Introduction to Erlang An Introduction to Erlang Part 2 - Concurrency Richard Carlsson Processes P1 fib(0) -> 1; fib(1) -> 1; fib(n) when N > 0 -> fib(n-1) + fib(n-2). Whenever an Erlang program is running, the code is executed

More information

Erlang. Joe Armstrong

Erlang. Joe Armstrong Erlang Joe Armstrong Though OOP came from many motivations, two were central. The large scale one was to find a better module scheme for complex systems involving hiding of details, and the small scale

More information

Erlang: distributed programming

Erlang: distributed programming Erlang: distributed May 11, 2012 1 / 21 Fault tolerance in Erlang links, exit signals, system process in Erlang OTP Open Telecom Platform 2 / 21 General idea Links Exit signals System processes Summary

More information

Erlang: An Overview. Part 2 Concurrency and Distribution. Thanks to Richard Carlsson for most of the slides in this part

Erlang: An Overview. Part 2 Concurrency and Distribution. Thanks to Richard Carlsson for most of the slides in this part Erlang: An Overview Part 2 Concurrency and Distribution Thanks to Richard Carlsson for most of the slides in this part Processes P1 fib(0) -> 1; fib(1) -> 1; fib(n) when N > 0 -> fib(n-1) + fib(n-2). Whenever

More information

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

exam Concurrent Programming tda383/dit390 Date: Time: 14:00 18:00 Place: Maskinhuset (M) exam Concurrent Programming tda383/dit390 Date: 2016-03-19 Time: 14:00 18:00 Place: Maskinhuset (M) Responsible Michał Pałka 0707966066 Result Available no later than 2016-04-12 Aids Max 2 books and max

More information

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

re-exam Concurrent Programming tda383/dit390 Date: Time: 14:00 18:00 Place: Maskinhuset (M) re-exam Concurrent Programming tda383/dit390 Date: 2016-08-22 Time: 14:00 18:00 Place: Maskinhuset (M) Responsible Michał Pałka 031 772 10 79 Result Available no later than 2016-09-12 Aids Max 2 books

More information

CS390 Principles of Concurrency and Parallelism. Lecture Notes for Lecture #5 2/2/2012. Author: Jared Hall

CS390 Principles of Concurrency and Parallelism. Lecture Notes for Lecture #5 2/2/2012. Author: Jared Hall CS390 Principles of Concurrency and Parallelism Lecture Notes for Lecture #5 2/2/2012 Author: Jared Hall This lecture was the introduction the the programming language: Erlang. It is important to understand

More information

Reminder from last time

Reminder from last time Concurrent systems Lecture 5: Concurrency without shared data, composite operations and transactions, and serialisability DrRobert N. M. Watson 1 Reminder from last time Liveness properties Deadlock (requirements;

More information

Robust Erlang (PFP Lecture 11) John Hughes

Robust Erlang (PFP Lecture 11) John Hughes Robust Erlang (PFP Lecture 11) John Hughes Genesis of Erlang Problem: telephony systems in the late 1980s Digital More and more complex Highly concurrent Hard to get right Approach: a group at Ericsson

More information

Erlang. Joe Armstrong.

Erlang. Joe Armstrong. Erlang Joe Armstrong joe.armstrong@ericsson.com 1 Who is Joe? Inventor of Erlang, UBF, Open Floppy Grid Chief designer of OTP Founder of the company Bluetail Currently Software Architect Ericsson Current

More information

Lecture 6. Introduction to Message Passing

Lecture 6. Introduction to Message Passing 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?

More information

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

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

More information

The Actor Model, Part Two. CSCI 5828: Foundations of Software Engineering Lecture 18 10/23/2014

The Actor Model, Part Two. CSCI 5828: Foundations of Software Engineering Lecture 18 10/23/2014 The Actor Model, Part Two CSCI 5828: Foundations of Software Engineering Lecture 18 10/23/2014 1 Goals Cover the material presented in Chapter 5, of our concurrency textbook In particular, the material

More information

The Actor Model. CSCI 5828: Foundations of Software Engineering Lecture 13 10/04/2016

The Actor Model. CSCI 5828: Foundations of Software Engineering Lecture 13 10/04/2016 The Actor Model CSCI 5828: Foundations of Software Engineering Lecture 13 10/04/2016 1 Goals Introduce the Actor Model of Concurrency isolation, message passing, message patterns Present examples from

More information

Erlang functional programming in a concurrent world

Erlang functional programming in a concurrent world KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional programming in a concurrent world Johan Montelius and Vladimir Vlassov Erlang Concurrent Oriented Programming processes have state communicate using

More information

Erlang functional programming in a concurrent world Johan Montelius and Vladimir Vlassov

Erlang functional programming in a concurrent world Johan Montelius and Vladimir Vlassov KTH ROYAL INSTITUTE OF TECHNOLOGY Erlang functional programming in a concurrent world Johan Montelius and Vladimir Vlassov Erlang Concurrent Oriented Programming processes have state communicate using

More information

MapReduce in Erlang. Tom Van Cutsem

MapReduce in Erlang. Tom Van Cutsem MapReduce in Erlang Tom Van Cutsem 1 Context Masters course on Multicore Programming Focus on concurrent, parallel and... functional programming Didactic implementation of Google s MapReduce algorithm

More information

Erlang 101. Google Doc

Erlang 101. Google Doc Erlang 101 Google Doc Erlang? with buzzwords Erlang is a functional concurrency-oriented language with extremely low-weight userspace "processes", share-nothing messagepassing semantics, built-in distribution,

More information

Parallel Programming in Erlang (PFP Lecture 10) John Hughes

Parallel Programming in Erlang (PFP Lecture 10) John Hughes Parallel Programming in Erlang (PFP Lecture 10) John Hughes What is Erlang? Haskell Erlang - Types - Lazyness - Purity + Concurrency + Syntax If you know Haskell, Erlang is easy to learn! QuickSort again

More information

DISTRIBUTED SYSTEMS [COMP9243] Lecture 1.5: Erlang INTRODUCTION TO ERLANG BASICS: SEQUENTIAL PROGRAMMING 2. Slide 1

DISTRIBUTED SYSTEMS [COMP9243] Lecture 1.5: Erlang INTRODUCTION TO ERLANG BASICS: SEQUENTIAL PROGRAMMING 2. Slide 1 DISTRIBUTED SYSTEMS [COMP9243] THE ERLANG ENVIRONMENT Slide 1 Lecture 1.5: Erlang ➀ Introduction ➁ Basics: Sequential programming ➂ Concurrent programming Slide 3 unix% erl 1> 1 + 2. 3 2> c(demo). {ok,demo}

More information

An Introduction to Erlang. Richard Carlsson

An Introduction to Erlang. Richard Carlsson An Introduction to Erlang Richard Carlsson Erlang Buzzwords Functional (strict) Single-assignment Dynamically typed Concurrent Distributed Message passing Soft real-time Fault tolerant No sharing Automatic

More information

Starting the System & Basic Erlang Exercises

Starting the System & Basic Erlang Exercises Starting the System & Basic Erlang Exercises These exercises will help you get accustomed with the Erlang development and run time environments. Once you have set up the Erlang mode for emacs, you will

More information

An Introduction to Erlang

An Introduction to Erlang Erlang Solutions Ltd An Introduction to Erlang From behind the trenches GOTO Copenhagen May 13 th, 2011 Francesco Cesarini Founder, Technical Director @FrancescoC francesco@erlang-solutions.com So Here

More information

Experiments in OTP-Compliant Dataflow Programming

Experiments in OTP-Compliant Dataflow Programming Experiments in OTP-Compliant Dataflow Programming Introducing Erlang Services Platform (Erlang/SP) San Francisco Erlang Factory, March 21, 2013 Jay Nelson Twitter: @duomark Email: Jay@duomark.com Manycore

More information

message passing model

message passing model message passing model PRODUCER-CONSUMER PROBLEM buffer producer consumer void producer(void) Two semaphores empty (i.v. 1) full (i.v. O) { while (TRUE) { ; P (empty);

More information

Producer Consumer in Ada

Producer Consumer in Ada Concurrent Systems 8L for Part IB Handout 3 Dr. Steven Hand Example: Active Objects A monitor with an associated server thread Exports an entry for each operation it provides Other (client) threads call

More information

One process sends a message. We will consider two. dimensions of this. message passing. PPHT07 Linda

One process sends a message. We will consider two. dimensions of this. message passing. PPHT07 Linda Lecture 9 Linda and Erlang Part I Linda Overview of Mes sage Passing One process sends a message Another process awaits for a message We will consider two approach: dimensions of this What form of synchronisation

More information

FRANCESCO CESARINI. presents ERLANG/OTP. Francesco Cesarini Erlang

FRANCESCO CESARINI. presents ERLANG/OTP. Francesco Cesarini Erlang FRANCESCO CESARINI presents Francesco Cesarini Erlang Solutions ERLANG/OTP @FrancescoC francesco@erlang-solutions.com www.erlang-solutions.com WHAT IS SCALABILITY? WHAT IS (MASSIVE) CONCURRENCY? WHAT

More information

Introduction to Erlang. Franck Petit / Sebastien Tixeuil

Introduction to Erlang. Franck Petit / Sebastien Tixeuil Introduction to Erlang Franck Petit / Sebastien Tixeuil Firstname.Lastname@lip6.fr Erlang History Erlang: Ericsson Language Designed to implement large-scale realtime telecommunication switching systems

More information

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions PROGRAMMING IN HASKELL CS-205 - Chapter 6 - Recursive Functions 0 Introduction As we have seen, many functions can naturally be defined in terms of other functions. factorial :: Int Int factorial n product

More information

sample exam Concurrent Programming tda383/dit390 Sample exam March 2016 Time:?? Place: Johanneberg

sample exam Concurrent Programming tda383/dit390 Sample exam March 2016 Time:?? Place: Johanneberg sample exam Concurrent Programming tda383/dit390 Sample exam March 2016 Time:?? Place: Johanneberg Responsible Michał Pałka 0707966066 Result Available no later than?-?-2016 Aids Max 2 books and max 4

More information

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

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data Lecture 4 Monitors Summary Semaphores Good news Simple, efficient, expressive Passing the Baton any await statement Bad news Low level, unstructured omit a V: deadlock omit a P: failure of mutex Synchronisation

More information

Advanced Functional Programming, 1DL Lecture 2, Cons T Åhs

Advanced Functional Programming, 1DL Lecture 2, Cons T Åhs Advanced Functional Programming, 1DL450 2012 Lecture 2, 2012-11-01 Cons T Åhs Higher order functions hof() -> F = fun(x) -> X * X + 1 end, L = lists:map(f, [1, 2, 3], G = fun([]) -> nil; ([_ _]) -> cons

More information

Concurre Concurr nt n Systems 8L for Part IB Handout 3 Dr. Steven Hand 1

Concurre Concurr nt n Systems 8L for Part IB Handout 3 Dr. Steven Hand 1 Concurrent Systems 8L for Part IB Handout 3 Dr. Steven Hand 1 Concurrency without shared data The examples so far have involved threads which h can arbitrarily read & write shared data A key need for mutual

More information

Lecture 5: Erlang 1/31/12

Lecture 5: Erlang 1/31/12 Principles of Concurrency and Parallelism Lecture 5: Erlang 1/31/12 Example Suppose we have N threads (or processes, tasks) that form a ring Each thread communicates with its neighbor forwarding a message

More information

Erlang - functional programming in a concurrent world. Johan Montelius KTH

Erlang - functional programming in a concurrent world. Johan Montelius KTH Erlang - functional programming in a concurrent world Johan Montelius KTH 1 Erlang Concurrent Oriented Programming processes have state communicate using message passing access and location transparent

More information

The case for reactive objects

The case for reactive objects The case for reactive objects Johan Nordlander, Luleå Univ. och Technology (with Mark Jones, Andrew Black, Magnus Carlsson, Dick Kieburtz all (ex) OGI) Links meeting, April 6 1 Links killer apps Web services...

More information

Metaprogramming and symbolic execution for detecting runtime errors in Erlang programs

Metaprogramming and symbolic execution for detecting runtime errors in Erlang programs Metaprogramming and symbolic execution for detecting runtime errors in Erlang programs Emanuele De Angelis 1, Fabio Fioravanti 1, Adrián Palacios 2, Alberto Pettorossi 3 and Maurizio Proietti 4 1 University

More information

Concurrent & Distributed Systems Supervision Exercises

Concurrent & Distributed Systems Supervision Exercises Concurrent & Distributed Systems Supervision Exercises Stephen Kell Stephen.Kell@cl.cam.ac.uk November 9, 2009 These exercises are intended to cover all the main points of understanding in the lecture

More information

Programming Paradigms

Programming Paradigms PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 1/31 Programming Paradigms Unit 16 Erlang Modules, Functions and Control Structures J. Gamper Free University of Bozen-Bolzano Faculty

More information

Reminder from last ;me

Reminder from last ;me Concurrent systems Lecture 5: Concurrency without shared data; transac;ons Dr Robert N. M. Watson 1 Reminder from last ;me Liveness proper;es Deadlock (requirements; resource alloca;on graphs; detec;on;

More information

Reminder from last time

Reminder from last time Concurrent systems Lecture 7: Crash recovery, lock-free programming, and transactional memory DrRobert N. M. Watson 1 Reminder from last time History graphs; good (and bad) schedules Isolation vs. strict

More information

Dealing with Issues for Interprocess Communication

Dealing with Issues for Interprocess Communication Dealing with Issues for Interprocess Communication Ref Section 2.3 Tanenbaum 7.1 Overview Processes frequently need to communicate with other processes. In a shell pipe the o/p of one process is passed

More information

Concurrency: State Models & Design Patterns

Concurrency: State Models & Design Patterns Concurrency: State Models & Design Patterns Practical Session Week 02 1 / 13 Exercises 01 Discussion Exercise 01 - Task 1 a) Do recent central processing units (CPUs) of desktop PCs support concurrency?

More information

Lecture 8: September 30

Lecture 8: September 30 CMPSCI 377 Operating Systems Fall 2013 Lecture 8: September 30 Lecturer: Prashant Shenoy Scribe: Armand Halbert 8.1 Semaphores A semaphore is a more generalized form of a lock that can be used to regulate

More information

Outline Part 2. Semaphores. Semaphore Usage. Objectives: Administrative details:

Outline Part 2. Semaphores. Semaphore Usage. Objectives: Administrative details: Outline Part 2 Objectives: Higher level synchronization mechanisms Classic problems in concurrency Administrative details: 56 Semaphores Well-known synchronization abstraction Defined as a non-negative

More information

Programming Paradigms Written Exam (6 CPs)

Programming Paradigms Written Exam (6 CPs) Programming Paradigms Written Exam (6 CPs) 06.07.2016 First name Student number Last name Signature Instructions for Students Write your name and student number on the exam sheet and on every solution

More information

CSCI 201L Written Exam #2. 10% of course grade

CSCI 201L Written Exam #2. 10% of course grade Name Final Score ID Extra Credit Section (circle one): MW 3:30-6:30 CSCI 201L Written Exam #2 10% of course grade 1. Anonymous Inner Classes In lecture we walked through the following: 1. Having two classes

More information

Programming Paradigms

Programming Paradigms PP 2017/18 Unit 15 Concurrent Programming with Erlang 1/32 Programming Paradigms Unit 15 Concurrent Programming with Erlang J. Gamper Free University of Bozen-Bolzano Faculty of Computer Science IDSE PP

More information

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 22: Remote Procedure Call (RPC)

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 22: Remote Procedure Call (RPC) CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2002 Lecture 22: Remote Procedure Call (RPC) 22.0 Main Point Send/receive One vs. two-way communication Remote Procedure

More information

An Introduction to Erlang

An Introduction to Erlang Erlang Solutions Ltd An Introduction to Erlang From behind the trenches GOTO Amsterdam Amsterdam, May 25 th 2012 Francesco Cesarini Founder, Technical Director @FrancescoC francesco@erlang-solutions.com

More information

Beautiful Concurrency with Erlang

Beautiful Concurrency with Erlang Beautiful Concurrency with Erlang Kevin Scaldeferri OSCON 23 July 2008 6 years at Yahoo, building large high-concurrency distributed systems Not an expert, don t use it professionally Dabbled, liked it,

More information

What's wrong with Semaphores?

What's wrong with Semaphores? Next: Monitors and Condition Variables What is wrong with semaphores? Monitors What are they? How do we implement monitors? Two types of monitors: Mesa and Hoare Compare semaphore and monitors Lecture

More information

Remote Invocation. 1. Introduction 2. Remote Method Invocation (RMI) 3. RMI Invocation Semantics

Remote Invocation. 1. Introduction 2. Remote Method Invocation (RMI) 3. RMI Invocation Semantics Remote Invocation Nicola Dragoni Embedded Systems Engineering DTU Informatics 1. Introduction 2. Remote Method Invocation (RMI) 3. RMI Invocation Semantics From the First Lecture (Architectural Models)...

More information

CS 31: Intro to Systems Misc. Threading. Kevin Webb Swarthmore College December 6, 2018

CS 31: Intro to Systems Misc. Threading. Kevin Webb Swarthmore College December 6, 2018 CS 31: Intro to Systems Misc. Threading Kevin Webb Swarthmore College December 6, 2018 Classic thread patterns Agenda Pthreads primitives and examples of other forms of synchronization: Condition variables

More information

Concurrent Programming. Introduction

Concurrent Programming. Introduction Concurrent Programming Introduction Team Lecturer: Alejandro Russo Course issues tda382@googlegroups.com https://groups.google.com/forum/#!forum/tda382 Other issues: russo@chalmers.se Assistants Staffan

More information

Erlang. An introduction. Paolo Baldan Linguaggi e Modelli per il Global Computing AA 2016/2017

Erlang. An introduction. Paolo Baldan Linguaggi e Modelli per il Global Computing AA 2016/2017 Erlang An introduction Paolo Baldan Linguaggi e Modelli per il Global Computing AA 2016/2017 Erlang, in a slogan Declarative (functional) language for concurrent and distributed systems Erlang = Functions

More information

Iterators. Lecture 24: Iterators & Exceptions. Implementing Iterators. When Good Programs Go Bad! - where! Abstract over control structures (in Clu)!

Iterators. Lecture 24: Iterators & Exceptions. Implementing Iterators. When Good Programs Go Bad! - where! Abstract over control structures (in Clu)! Iterators Lecture 24: Iterators & Exceptions CSC 131 Fall, 2014 Kim Bruce Abstract over control structures (in Clu) - where for c : char in string_chars(s) do string_chars = iter (s : string) yields (char);

More information

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst Operating Systems CMPSCI 377 Spring 2017 Mark Corner University of Massachusetts Amherst What is a Monitor? Ties data and the synchronization operations together Monitors guarantee mutual exclusion, i.e.,

More information

Performance Throughput Utilization of system resources

Performance Throughput Utilization of system resources Concurrency 1. Why concurrent programming?... 2 2. Evolution... 2 3. Definitions... 3 4. Concurrent languages... 5 5. Problems with concurrency... 6 6. Process Interactions... 7 7. Low-level Concurrency

More information

Operating Systems ECE344

Operating Systems ECE344 Operating Systems ECE344 Ding Yuan Announcement & Reminder Lab 0 mark posted on Piazza Great job! One problem: compilation error I fixed some for you this time, but won t do it next time Make sure you

More information

Programming Paradigms

Programming Paradigms PP 2017/18 Unit 18 Summary of Basic Concepts 1/13 Programming Paradigms Unit 18 Summary of Basic Concepts J. Gamper Free University of Bozen-Bolzano Faculty of Computer Science IDSE PP 2017/18 Unit 18

More information

Lecture 19: Functions, Types and Data Structures in Haskell

Lecture 19: Functions, Types and Data Structures in Haskell The University of North Carolina at Chapel Hill Spring 2002 Lecture 19: Functions, Types and Data Structures in Haskell Feb 25 1 Functions Functions are the most important kind of value in functional programming

More information

Erlang and Concurrency. André Pang Rising Sun Research

Erlang and Concurrency. André Pang Rising Sun Research Erlang and Concurrency André Pang Rising Sun Research Games. Unreal 3 engine. Amazing graphics. (Unreal 3 video demo.) 2004/2005? Still state-of-the-art. What does that game have to do with this talk?

More information

Logic Programming II & Revision

Logic Programming II & Revision Logic Programming II & Revision Gerardo Schneider Department of Informatics University of Oslo 1 Some corrections (1) hsiblings(x,y) :- child(x,parent), child(y,parent), X \== Y, child(x,parent1), child(y,parent2),

More information

Scenes From the Language Struggle in Toronto, Ontario. Mike Nolta

Scenes From the Language Struggle in Toronto, Ontario. Mike Nolta Scenes From the Language Struggle in Toronto, Ontario Mike Nolta Ch-Ch-Changes OpenMP & MPI are fine for now, but we need language support for parallelism. Corbato s Law: The number of lines of code a

More information

Concurrent Programming TDA381/DIT390

Concurrent Programming TDA381/DIT390 Chalmers GÖTEBORGS UNIVERSITET Josef Svenningsson, Computer Science and Engineering Concurrent Programming TDA381/DIT390 Saturday, October 23, 8.30 12.30, M. (including example solutions to programming

More information

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430 Implementing Mutual Exclusion Sarah Diesburg Operating Systems CS 3430 From the Previous Lecture The too much milk example shows that writing concurrent programs directly with load and store instructions

More information

DS 2009: middleware. David Evans

DS 2009: middleware. David Evans DS 2009: middleware David Evans de239@cl.cam.ac.uk What is middleware? distributed applications middleware remote calls, method invocations, messages,... OS comms. interface sockets, IP,... layer between

More information

Kent Academic Repository

Kent Academic Repository Kent Academic Repository Full text document (pdf) Citation for published version Cesarini, Francesco and Thompson, Simon (2010) Erlang Behaviours: Programming With Process Design Patterns. In: Central

More information

Monitors; Software Transactional Memory

Monitors; Software Transactional Memory Monitors; Software Transactional Memory Parallel and Distributed Computing Department of Computer Science and Engineering (DEI) Instituto Superior Técnico March 17, 2016 CPD (DEI / IST) Parallel and Distributed

More information

Opera&ng Systems ECE344

Opera&ng Systems ECE344 Opera&ng Systems ECE344 Lecture 6: Synchroniza&on (II) Semaphores and Monitors Ding Yuan Higher- Level Synchroniza&on We looked at using locks to provide mutual exclusion Locks work, but they have some

More information

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

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017 Operating Systems Lecture 4 - Concurrency and Synchronization Adrien Krähenbühl Master of Computer Science PUF - Hồ Chí Minh 2016/2017 Mutual exclusion Hardware solutions Semaphores IPC: Message passing

More information

Topic 5: Examples and higher-order functions

Topic 5: Examples and higher-order functions CITS 3242 Programming Paradigms Topic 5: Examples and higher-order functions This lecture includes some larger examples in F# and introduces higher-functions. 1 Examples: append Now that we have lists,

More information

Erlang: An Overview. Part 1 Sequential Erlang. Thanks to Richard Carlsson for the original version of many slides in this part

Erlang: An Overview. Part 1 Sequential Erlang. Thanks to Richard Carlsson for the original version of many slides in this part Erlang: An Overview Part 1 Sequential Erlang Thanks to Richard Carlsson for the original version of many slides in this part Erlang buzzwords Functional (strict) Single-assignment Dynamically typed Concurrent

More information

The University of Texas at Arlington

The University of Texas at Arlington The University of Texas at Arlington Lecture 6: Threading and Parallel Programming Constraints CSE 5343/4342 Embedded Systems II Based heavily on slides by Dr. Roger Walker More Task Decomposition: Dependence

More information

Fall 2018 Lecture N

Fall 2018 Lecture N 15-150 Fall 2018 Lecture N Tuesday, 20 November I m too lazy to figure out the correct number Stephen Brookes midterm2 Mean: 79.5 Std Dev: 18.4 Median: 84 today or, we could procrastinate lazy functional

More information

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph Topics Functional Programming Christian Sternagel Harald Zankl Evgeny Zuenko Department of Computer Science University of Innsbruck WS 2017/2018 abstract data types, algebraic data types, binary search

More information

Monitors & Condition Synchronization

Monitors & Condition Synchronization Chapter 5 Monitors & Condition Synchronization monitors & condition synchronization Concepts: monitors: encapsulated data + access procedures mutual exclusion + condition synchronization single access

More information

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

G52CON: Concepts of Concurrency Lecture 15: Message Passing. Gabriela Ochoa School of Computer Science & IT G52CON: Concepts of Concurrency Lecture 15: Message Passing Gabriela Ochoa School of Computer Science & IT gxo@cs.nott.ac.uk Content Introduction and transition Recapitulation on hardware architectures

More information

COSE212: Programming Languages. Lecture 4 Recursive and Higher-Order Programming

COSE212: Programming Languages. Lecture 4 Recursive and Higher-Order Programming COSE212: Programming Languages Lecture 4 Recursive and Higher-Order Programming Hakjoo Oh 2016 Fall Hakjoo Oh COSE212 2016 Fall, Lecture 4 September 27, 2016 1 / 21 Recursive and Higher-Order Programming

More information

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

PROGRAMMING IN HASKELL. Chapter 2 - First Steps PROGRAMMING IN HASKELL Chapter 2 - First Steps 0 The Hugs System Hugs is an implementation of Haskell 98, and is the most widely used Haskell system; The interactive nature of Hugs makes it well suited

More information

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

PROGRAMMING IN HASKELL. Chapter 2 - First Steps PROGRAMMING IN HASKELL Chapter 2 - First Steps 0 Glasgow Haskell Compiler GHC is the leading implementation of Haskell, and comprises a compiler and interpreter; The interactive nature of the interpreter

More information

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

Synchronization. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University Synchronization Design, Spring 2011 Department of Computer Science Synchronization Basic problem: Threads are concurrently accessing shared variables The access should be controlled for predictable result.

More information

Synchronization: Basics

Synchronization: Basics Synchronization: Basics CS 485G6: Systems Programming Lecture 34: 5 Apr 6 Shared Variables in Threaded C Programs Question: Which variables in a threaded C program are shared? The answer is not as simple

More information

Concept of a process

Concept of a process Concept of a process In the context of this course a process is a program whose execution is in progress States of a process: running, ready, blocked Submit Ready Running Completion Blocked Concurrent

More information

main read Identical static threads Dynamic threads with Concurrent and

main read Identical static threads Dynamic threads with Concurrent and CHAPTER 3: CONCURRENT PROCESSES AND PROGRAMMING Chapter outline Thread implementations Process models The client/server model Time services Language constructs for synchronization Concurrent programming

More information

CSCI 201L Final Written SOLUTION. 13% of course grade

CSCI 201L Final Written SOLUTION. 13% of course grade SOLUTION 13% of course grade 1. Generics C++ has had templates long before Java even existed as a language. When Java was created, there were no templates or generics. It wasn t until many years and many

More information

Functional Programming Lecture 13: FP in the Real World

Functional Programming Lecture 13: FP in the Real World Functional Programming Lecture 13: FP in the Real World Viliam Lisý Artificial Intelligence Center Department of Computer Science FEE, Czech Technical University in Prague viliam.lisy@fel.cvut.cz 1 Mixed

More information

ENGR 3950U / CSCI 3020U UOIT, Fall 2012 Quiz on Process Synchronization SOLUTIONS

ENGR 3950U / CSCI 3020U UOIT, Fall 2012 Quiz on Process Synchronization SOLUTIONS Name: Student Number: SOLUTIONS ENGR 3950U / CSCI 3020U (Operating Systems) Quiz on Process Synchronization November 13, 2012, Duration: 40 Minutes (10 questions and 8 pages, 45 Marks) Instructor: Dr.

More information

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

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs CSE 451: Operating Systems Winter 2005 Lecture 7 Synchronization Steve Gribble Synchronization Threads cooperate in multithreaded programs to share resources, access shared data structures e.g., threads

More information

<Urban.Boquist. com> Rev PA

<Urban.Boquist. com> Rev PA FP in industry - Erlang Urban Boquist Ericsson AB com> 1 Outline Who Am I Mobile Telecommunications Networks Packet Core Network GPRS & SGSN Use of Erlang in SGSN SGSN Design

More information

The University of Texas at Arlington

The University of Texas at Arlington The University of Texas at Arlington Lecture 10: Threading and Parallel Programming Constraints CSE 5343/4342 Embedded d Systems II Objectives: Lab 3: Windows Threads (win32 threading API) Convert serial

More information

erlang module Erlang Programming - Lecture 8

erlang module Erlang Programming - Lecture 8 erlang module Erlang Programming - Lecture 8 Saurabh Barjatiya Rekall Software Pvt. Ltd. 29 June, 2014 Contents erlang module Auto-imported Built-In Functions (BIFs) BIFs allowed in guard test 1 erlang

More information