CONCURRENCY AND THE ACTOR MODEL COMP 319 University
- Slides: 46
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) COMP 319 © University of Liverpool slide 2
Unix fork example main() { pid_t p. ID = fork(); if (p. ID == 0) // child { // Code only executed by child process else if (p. ID < 0) // fork failed { exit(1); } else // parent { // Code only executed by parent process } COMP 319 © University of Liverpool slide 3
Threads and processes Process 1 COMP 319 Process 2 © University of Liverpool 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 COMP 319 © University of Liverpool 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 COMP 319 © University of Liverpool slide 6
Concurrency and deadlock Thread 1 wait() JVM notify() Thread 2 COMP 319 © University of Liverpool slide 7
Deadlock (circular dependency) Code thread 1 object 1. wait() object 2. signal() object 1 wait() Code thread 2 object 2. wait() object 3. signal() object 2 wait() Code thread 3 object 3. wait() object 1. signal() COMP 319 object 1 wait() © University of Liverpool slide 8
Synchronization deadlock public void transfer. Money(Account from. Account, Account to. Account, int amount. To. Transfer) { synchronized (from. Account) { synchronized (to. Account) { if (from. Account. has. Sufficient. Balance(amount. To. Trans fer) { from. Account. debit(amount. To. Transfer); to. Account. credit(amount. To. Transfer); } transfer. Money(account. One, account. Two, amount); } } transfer. Money(account. Two, account. One, amount); } COMP 319 © University of Liverpool slide 9
Double lock issue public static Singleton get. Instance() { if (instance == null) { synchronized(Singleton. class) { //1 if (instance == null) //2 instance = new Singleton(); //3 } } return instance; } COMP 319 © University of Liverpool slide 10
Deadlock as race condition errors • Very hard to test for… public void transfer. Money(Account from. Account, Account to. Account, int amount. To. Transfer) { synchronized (from. Account) { Pre-emption required here synchronized (to. Account) { if (from. Account. has. Sufficient. Balance(amount. To. Trans fer) { from. Account. debit(amount. To. Transfer); to. Account. credit(amount. To. Transfer); } } COMP 319 © University of Liverpool 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 COMP 319 © University of Liverpool 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 COMP 319 © University of Liverpool 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 • • COMP 319 © University of Liverpool 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 COMP 319 © University of Liverpool 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 COMP 319 © University of Liverpool slide 16
Actors and Networks • Actors don’t have to on the same system • This helps with scaling/redundancy etc. Actor Internet Actor COMP 319 © University of Liverpool slide 17
Java and state sharing • If objects are not shared then call by reference is not allowed • Message my. Mess=new Message(“Hello!”); • my. Actor. send. Message(my. Mess); // shared state • // Replace with call by value. . • Message my. Mess=new Message(“Hello!”); my. Actor. send. Message(my. Mess. clone()); // N. B. clone is garbage collected after call terminates COMP 319 © University of Liverpool 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 - pointer. A=pointer. B (pointer. B now invalid) COMP 319 © University of Liverpool 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) COMP 319 © University of Liverpool 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 COMP 319 © University of Liverpool 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 COMP 319 © University of Liverpool 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 COMP 319 © University of Liverpool 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 COMP 319 © University of Liverpool slide 24
Object Request Broker COMP 319 © University of Liverpool 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 COMP 319 © University of Liverpool 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 COMP 319 © University of Liverpool 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 COMP 319 © University of Liverpool 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 COMP 319 © University of Liverpool 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. . COMP 319 © University of Liverpool 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 COMP 319 © University of Liverpool slide 31
Synchronisation constraints • Scalar Example @Disable(message. Name = "put") public Boolean disable. Put(Integer x) { if (buffer. Ready) { return (tail == buffer. Size); } else { return true; } } COMP 319 © University of Liverpool 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 COMP 319 © University of Liverpool slide 33
Erlang • • Ericsson in house language Functional approach Very high performance Hot swap module handling Message passing Location independence No classes, no OO COMP 319 © University of Liverpool 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 COMP 319 © University of Liverpool 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 COMP 319 © University of Liverpool 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 } COMP 319 © University of Liverpool 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 COMP 319 © University of Liverpool slide 38
Scala Hello World object Hello. World { def main(args: Array[String]): Unit= { var hello="Hello World. . . "; val my. Value="Freddy"; println(hello+my. Value); } } No need for static, object defines singleton class instance COMP 319 © University of Liverpool slide 39
Scala standard class definition class Person(val surname: String, val forename: String) { var weight=0 f; // weight of person def fn: String = forename + surname def set. Weight(w : Float) : Unit = { this. weight=w; } def get. Weight(metric: Boolean) : Double = { if (metric) { weight // returns value } else { var imperial=weight*2. 2 imperial // returns value } } © University of Liverpool slide 40 } COMP 319
Extra constructors def this(surname: String ) = { this(surname, null) this } COMP 319 © University of Liverpool slide 41
Actor example class Email. Message(val message: String) { } COMP 319 © University of Liverpool slide 42
Actor Example import scala. actors. Actor. _ class Email. Client(val username: String) extends Actor { def act() { while (true) { receive { case incoming: Email. Message => println("Got message for " + username + "message is " + incoming. message); } } COMP 319 © University of Liverpool 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 Email. Message("Hello"); while (true) { receive { case incoming : Email. Message => println("Got message now sending"+incoming. message); userclient ! incoming } } COMP 319 © University of Liverpool slide 44
Actor Example object Main { def main(args: Array[String]) : Unit = { val client=new Email. Client("Seb"); val mb=new Mailbox(client); mb. start(); client. start(); mb ! new Email. Message("Hello how are you? "); } } COMP 319 © University of Liverpool slide 45
Summary • Actors can - Remove dead lock - Improve - Scalability, redundancy - Allow for full mobility of code - Be local or remote - Create new actors COMP 319 © University of Liverpool slide 46
- Actor model concurrency
- Mathews v. eldridge (1976)
- Art. 319 cp
- Art. 319 cp
- Art. 319 cp
- Cpsc 319
- Cpsc 319 u of c
- Cwa 319
- 87,539,319 = 2283 + _ _ _ 3
- Hsci 319
- Me 319
- H length
- Me 319
- Pc 319
- Actors in foreign policy
- Actor model microservices
- Actor model languages
- Rational actor model
- Concurrency control and recovery in database systems
- Apa yang dimaksud dengan recovery
- Ccs milner
- Safety and liveness in concurrency
- Transactions and concurrency control in distributed systems
- Transaction and concurrency control in dbms
- Log 50
- Special segments in triangles practice
- Transaction management and concurrency control
- Robert pattinson is an american actor and musician
- Strengths and weaknesses of an actor
- English comic actor and film director
- English comic actor and film director
- English comic actor and film director
- A scientist performs an experiment, and an actor performs a
- Point of concurrency of a triangle
- Definition of point of concurrency
- Concurrency in web applications
- Incenter conjecture
- Ue4 critical section
- Unix concurrency mechanisms
- Pitfalls of lock based protocol
- Concurrency control in distributed databases
- Concurrency control in distributed transactions
- Ada concurrency
- Advanced state
- Concurrency
- Explain subprogram level concurrency with an example.
- Concurrent lines medians and altitudes