CONCURRENCY AND THE ACTOR MODEL

Similar documents
Executive Summary. It is important for a Java Programmer to understand the power and limitations of concurrent programming in Java using threads.

Chapter 3: Process-Concept. Operating System Concepts 8 th Edition,

Processes. Process Concept

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Processes and Threads

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

Lecture 2 Process Management

Concurrency: State Models & Design Patterns

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

Part Two - Process Management. Chapter 3: Processes

Chapter 4: Threads. Operating System Concepts. Silberschatz, Galvin and Gagne

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

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

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

Chapter 3 Processes. Process Concept. Process Concept. Process Concept (Cont.) Process Concept (Cont.) Process Concept (Cont.)

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

Chapter 4: Processes. Process Concept

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

Chapter 4: Processes

High Performance Computing Course Notes Shared Memory Parallel Programming

What Is A Process? Process States. Process Concept. Process Control Block (PCB) Process State Transition Diagram 9/6/2013. Process Fundamentals

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

Process Concept. Minsoo Ryu. Real-Time Computing and Communications Lab. Hanyang University.

Chapter 3: Process Concept

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

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

OPERATING SYSTEMS. UNIT II Sections A, B & D. An operating system executes a variety of programs:

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008.

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

Process a program in execution; process execution must progress in sequential fashion. Operating Systems

Chapter 3: Processes

Chapter 3: Processes. Operating System Concepts 8th Edition

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

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

Advantages of concurrent programs. Concurrent Programming Actors, SALSA, Coordination Abstractions. Overview of concurrent programming

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

Diagram of Process State Process Control Block (PCB)

Lecture 4: Memory Management & The Programming Interface

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

W4118 Operating Systems. Junfeng Yang

Inter-process communication (IPC)

JAVA CONCURRENCY FRAMEWORK. Kaushik Kanetkar

Scala Actors. Scalable Multithreading on the JVM. Philipp Haller. Ph.D. candidate Programming Methods Lab EPFL, Lausanne, Switzerland

Chapter 4: Processes

Prepared by Prof. Hui Jiang Process. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

The Big Picture So Far. Chapter 4: Processes

Process. Prepared by Prof. Hui Jiang Dept. of EECS, York Univ. 1. Process in Memory (I) PROCESS. Process. How OS manages CPU usage? No.

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

Lecture 24: Java Threads,Java synchronized statement

Notice: This set of slides is based on the notes by Professor Perrone of Bucknell and the textbook authors Silberschatz, Galvin, and Gagne

Chapter 3: Processes. Operating System Concepts Essentials 2 nd Edition

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

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

Chapter 4: Processes

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

Operating Systems 2 nd semester 2016/2017. Chapter 4: Threads

CHAPTER 2: PROCESS MANAGEMENT

Multithreading and Interactive Programs

CS2506 Quick Revision

! How is a thread different from a process? ! Why are threads useful? ! How can POSIX threads be useful?

The Big Picture So Far. Chapter 4: Processes

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

Problems with Concurrency. February 19, 2014

Reminder from last time

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CHAPTER 3 - PROCESS CONCEPT

!! How is a thread different from a process? !! Why are threads useful? !! How can POSIX threads be useful?

Chapter 4: Processes. Process Concept

Programming Safe Agents in Blueprint. Alex Muscar University of Craiova

CSE 4/521 Introduction to Operating Systems

CSCE 313: Intro to Computer Systems

Process Concepts 8/21/2014. CS341: Operating System. Scheduling: Theoretical Analysis

Processes and Threads. Processes and Threads. Processes (2) Processes (1)

I/O Management Software. Chapter 5

COSC243 Part 2: Operating Systems

Concurrent Programming. Copyright 2017 by Robert M. Dondero, Ph.D. Princeton University

Processes COMPSCI 386

Multithreading and Interactive Programs

Parallel Programming Languages COMP360

CS 450 Operating System Week 4 Lecture Notes

Performance Throughput Utilization of system resources

ECE 587 Hardware/Software Co-Design Lecture 07 Concurrency in Practice Shared Memory I

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Techno India Batanagar Department of Computer Science & Engineering. Model Questions. Multiple Choice Questions:

Message Passing Part I

Module 4: Processes. Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess Communication

Module 4: Processes. Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess Communication

Operating Systems Comprehensive Exam. Spring Student ID # 3/16/2006

Outline. Threads. Single and Multithreaded Processes. Benefits of Threads. Eike Ritter 1. Modified: October 16, 2012

CMSC421: Principles of Operating Systems

CS5460: Operating Systems

Processes. Process Scheduling, Process Synchronization, and Deadlock will be discussed further in Chapters 5, 6, and 7, respectively.

Advances in Programming Languages

Chapter 3: Processes

Chapter 3: Process Concept

Chapter 3: Process Concept

Chapter 4: Processes. Process Concept. Process State

Lecture 4: Process Management

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

Transcription:

CONCURRENCY AND THE ACTOR MODEL COMP 319 University of Liverpool slide 1

Concurrency review Multi-tasking - Task - Processes with isolated address space - Programming examples - Unix fork() processes - IPC via - Pipes, FIFOs - Shared memory - Fork() returns - -1 for error - 0 for child - >0 for parent (pid of parent) slide 2

Unix fork example main() { pid_t pid = fork(); if (pid == 0) // child { // Code only executed by child process else if (pid < 0) // fork failed { exit(1); else // parent { // Code only executed by parent process slide 3

Threads and processes Process 1 Process 2 Process 3 slide 4

Standard concurrency in Java review Threads - Shared memory, flow of execution - Uses Thread or Runnable Mutex - Monitors Thread synchronization - wait - notify slide 5

Green Threads and threads Green threads - Scheduled and switched by the JVM Native threads - Supported by the OS - Scheduling depends on OS When we look at Actor languages concurrency the difference is important slide 6

Concurrency and deadlock Thread Thread 1 wait Thread 2 JVM wait() notify() slide 7

Deadlock (circular dependency) Code thread 1 object1.wait() object2.signal() Code thread 2 object2.wait() object3.signal() Code thread 3 object3.wait() object1.signal() object1 wait() object2 wait() object1 wait() slide 8

Synchronization deadlock public void transfermoney(account fromaccount, Account toaccount, int amounttotransfer) { synchronized (fromaccount) { synchronized (toaccount) { if (fromaccount.hassufficientbalance(amounttotrans fer) { fromaccount.debit(amounttotransfer); toaccount.credit(amounttotransfer); transfermoney(accountone, accounttwo, amount); transfermoney(accounttwo, accountone, amount); slide 9

Double lock issue public static Singleton getinstance() { if (instance == null) { synchronized(singleton.class) { //1 if (instance == null) //2 instance = new Singleton(); //3 return instance; slide 10

Deadlock as race condition errors Very hard to test for public void transfermoney(account fromaccount, Account toaccount, int amounttotransfer) { synchronized (fromaccount) { Pre-emption required here synchronized (toaccount) { if (fromaccount.hassufficientbalance(amounttotrans fer) { fromaccount.debit(amounttotransfer); toaccount.credit(amounttotransfer); slide 11

Fairness Multiple threads waiting for - Synchronization locks - Notifications Java does not guarantee - To wake up threads dependent in waiting time - Not to starve threads out slide 12

Summary Thread shared models are prone to - Deadlocks - Unfairness - Data corruption issues - Hard to test code (race conditions) Notice this is - Inherent in the model - Must be dealt with by careful design decisions slide 13

Current Trends Powerful multi-core architecture Large amounts of memory High speed networks Easy to support - Many threads - Fast message Hard to design - Complex concurrent systems Hardware is powerful/software is complex slide 14

Actor model Much simplified approach to concurrency Each actor - Receives messages in an inbox - Sends messages to other Actors The actor contains state BUT - State is not shared between actors - Messages sent are immutable and owned by the receiver slide 15

Actor model Each Actor has an independent thread Each Actor s thread processes messages out of the inbox There is no need to share data and therefore no need for deadlock The Actor can - Create new Actors - Send message to other Actors slide 16

Actors and Networks Actors don t have to on the same system This helps with scaling/redundancy etc. Actor Actor Actor Internet Actor Actor slide 17

Java and state sharing If objects are not shared then call by reference is not allowed Message mymess=new Message( Hello! ); myactor.sendmessage(mymess); // shared state // Replace with call by value.. Message mymess=new Message( Hello! ); myactor.sendmessage(mymess.clone()); // N.B. clone is garbage collected after call terminates slide 18

Safe Messaging Some Java Actor frameworks allow - Access to other Actors mailboxes - Memory sharing via messages Solutions - Cloning - Immutable objects (e.g. Strings) - Linear types - pointera=pointerb (pointerb now invalid) slide 19

Messaging handling issues Zero copy - Pass by reference - Very fast - Keep object as immutable - Use scalar types - Not useful with remote actors Full copy (deep copy) - Can be very slow (often the bottle neck for the application) slide 20

Message handling All messages are sent asynchronously - Non-blocking mode Message do not have to be buffered Messages do not have to arrive in order they were sent (datagram support) Addressing - Actor s address (mailing address) - Addresses can come from - Message received - Know since this Actor created the Actor slide 21

Scheduling and fair-scheduling The choice of which actor gets to execute next and for how long is done by a part of the system called the scheduler An actor is non-blocked if it is processing a message or if its mailbox is not empty, otherwise the actor is blocked A scheduler is fair if it does not starve a non-blocked actor, i.e. all nonblocked actors eventually execute Fair scheduling makes it easier to reason about programs and program composition Otherwise some correct program (in isolation) may never get processing time when composed with other programs slide 22

Message fairness All messages are assumed to be delivered All messages are assumed to be processed (eventually) This is to help avoid deadlock and starvation slide 23

Location Transparency Addressing is the same wherever - The sender is (local or remote) - The receiver is (local or remote) Location Transparency examples - Mobile MISDN - Object Request Broker (CORBA) Useful technology - Brokers, Anycasting and multicasting slide 24

Object Request Broker slide 25

Locality of reference Bring Actors closer together - Possibly on the same machine Why - Cut down bandwidth, failures and latency (delays) How - Process migration - Pick closer Actors first Contradicts? - Location transparancy slide 26

Transparent Migration Movement of Actors Features - Capture state - Creation of new Actor - Deletion of old Actor - Restoring state - Handling addressing All this should be transparent to - All application code slide 27

Mobility Strong or Weak (Fuggetta et al) Strong - Movement of code + execution state Weak - Movement of code + optional initialized state - Can be used to move idle actor slide 28

Mobility uses Locality of reference - Moving Actors closer Redundancy - Fail over to other hardware Load balancing - Moving Actor to less loaded component Re-configuration - Moving to new hardware - Moving to mobile client slide 29

Synchronization and Actors Actor sends messages, with return address Senders - Monitors for mailbox for reply Standard service in - Scala - Actor Foundry - many more.. slide 30

Synchronisation constraints Examples - Buffer that fills up - Service is offline at certain hours Solution - Defer the processing of the message - Store new message in saved message queue - Every time new message received, checked saved queue slide 31

Synchronisation constraints Scalar Example @Disable(messageName = "put") public Boolean disableput(integer x) { if (bufferready) { return (tail == buffersize); else { return true; slide 32

Actor languages and frameworks Actor foundry - Safe (by-copy) as well as efficient (zero-copy) messaging - Message ordering using Local Synchronization Constraints - Pattern-matching (Multiple Dispatch) - Fair scheduling - Mobility - Location independence slide 33

Erlang Ericsson in house language Functional approach Very high performance Hot swap module handling Message passing Location independence No classes, no OO slide 34

Erlang and variables and FP Not really variables - A=5 - if A is unbounded 5 assigned to A - B=A - Ok - A=10 - ERROR A is already bound to 5 - A=5 - Just returns 5 slide 35

Referential transparency Every time you call a function with the same arguments - You get the same return value Feature of Erlang and other FP languages Gives higher levels of code stability due to state not being used in functions slide 36

Tail recursion Allows you to call a recursively call a method without pushing anything on to the stack function a(state) { a(new_state); // does not let // stack grow, like a loop slide 37

Scala Function + OOP Integrated with Java JDK - Call any JDK method from scala Full support for Actor programming Highly scalable Dynamic Typing Lots of inference - Example var message= Hello - No need for String type definition slide 38

Scala Hello World object HelloWorld { def main(args: Array[String]): Unit= { var hello="hello World..."; val myvalue="freddy"; println(hello+myvalue); No need for static, object defines singleton class instance slide 39

Scala standard class definition class Person(val surname: String,val forename: String) { var weight=0f; // weight of person def fn: String = forename + surname def setweight(w : Float) : Unit = { this.weight=w; def getweight(metric: Boolean) : Double = { if (metric) { weight // returns value else { var imperial=weight*2.2 imperial // returns value slide 40

Extra constructors def this(surname: String ) = { this(surname,null) this slide 41

Actor example class EmailMessage(val message: String) { slide 42

Actor Example import scala.actors.actor import scala.actors.actor._ class EmailClient(val username: String) extends Actor { def act() { while (true) { receive { case incoming: EmailMessage => println("got message for " + username + "message is " + incoming.message); slide 43

Actor Example import scala.actors.actor class Mailbox(userclient: Actor) extends Actor { def act() { // actor thread val a=1; val b=1; var incoming=new EmailMessage("Hello"); while (true) { receive { case incoming : EmailMessage => println("got message now sending"+incoming.message); userclient! incoming slide 44

Actor Example object Main { def main(args: Array[String]) : Unit = { val client=new EmailClient("Seb"); val mb=new Mailbox(client); mb.start(); client.start(); mb! new EmailMessage("Hello how are you?"); slide 45

Summary Actors can - Remove dead lock - Improve - Scalability, redundancy - Allow for full mobility of code - Be local or remote - Create new actors slide 46