20
ACTOR-BASED MODEL FOR ACTOR-BASED MODEL FOR CONCURRENT PROGRAMMING CONCURRENT PROGRAMMING Sander Sõnajalg Sander Sõnajalg

ACTOR-BASED MODEL FOR CONCURRENT PROGRAMMING

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ACTOR-BASED MODEL FOR CONCURRENT PROGRAMMING

ACTOR-BASED MODEL FOR ACTOR-BASED MODEL FOR CONCURRENT PROGRAMMINGCONCURRENT PROGRAMMING

Sander SõnajalgSander Sõnajalg

Page 2: ACTOR-BASED MODEL FOR CONCURRENT PROGRAMMING

ContentsContents

● Introduction to concurrent programming● Shared-memory model vs. actor model● Main principles of the actor model● Actors for light-weight processes● Actors for distributed programming● Importance & perspectives of actor model

Page 3: ACTOR-BASED MODEL FOR CONCURRENT PROGRAMMING

Concurrent programmingConcurrent programming

● = when programs are designed as collection of interacting computational processes that may be executed in parallel (Wikipedia)

● Inter-dependant processes● executing simultaneously● affecting each-other's work● must exchange information to do so

Page 4: ACTOR-BASED MODEL FOR CONCURRENT PROGRAMMING

Concurrent programming:Concurrent programming:motivationmotivation

● Concurrency in local computer● Let part of a local program do something useful in

the background while it is waiting for some input● Clients need to be served in parallel (web server)

● Parallelizing things for distributing● Distributing for inclusion of new resource● Distributing because functional requirements

demand it

Page 5: ACTOR-BASED MODEL FOR CONCURRENT PROGRAMMING

Implementing Concurrent Implementing Concurrent ProgrammingProgramming

● GOAL: Implement a library which would provide necessary abstractions on top of operating system resources to simplify concurrent programming

● What we need?● Units that could be executed in parallel (concurrency

primitives)● Means of communication between these units

Page 6: ACTOR-BASED MODEL FOR CONCURRENT PROGRAMMING

The Traditional Way:The Traditional Way:Threads & Shared MemoryThreads & Shared Memory

● Smallest executable unit is thread● Usually, operating system threads are used● Virtual threading is possible, but less common

(“green threads”)

● Communication is implemented by sharing variables

Page 7: ACTOR-BASED MODEL FOR CONCURRENT PROGRAMMING

Threads & Shared MemoryThreads & Shared Memory

T1 T2

shared memory

thread 1 thread 2

time

x

Aqcuire lock for slot 5,write value of x

1 2 3 4 5 6 7 8 9 . . .

read value of x

check if T1 has written xfail; go back to sleep

go to sleep, wait for x from T1

Page 8: ACTOR-BASED MODEL FOR CONCURRENT PROGRAMMING

● Pros:● Fast

● Cons:● Complicated & error-prone client code● Not extendable to distributed programming● Threads are heavy-weight – not too scalable

● Examples:● Standard concurrency libraries in Java, C#, etc..

The Traditional Way:The Traditional Way:Threads & Shared MemoryThreads & Shared Memory

Page 9: ACTOR-BASED MODEL FOR CONCURRENT PROGRAMMING

The OTHER Way:The OTHER Way:The Actor ModelThe Actor Model

● Smallest executable unit is an actor● An actor is a concurrency primitive that does

not share any resources with other actors

● Communication is implemented by actors sending each-other messages

Page 10: ACTOR-BASED MODEL FOR CONCURRENT PROGRAMMING

Actors: formal definitionActors: formal definition

● Actors are computational agents which map each incoming communication to a triple consisting of: A finite set of communications sent to other actors A new behavior (which will govern the response to

the next communication processed) A finite set of new actors created

(Gul A. Agha, 1985)

Page 11: ACTOR-BASED MODEL FOR CONCURRENT PROGRAMMING

Workflow:Workflow:event-driven + asynchronousevent-driven + asynchronous

A1

time

A2

A3

initialsystemstart create

create

message ymessage x

c1

c2c3

yx

message z

z

Page 12: ACTOR-BASED MODEL FOR CONCURRENT PROGRAMMING

Actors and event-driven Actors and event-driven programmingprogramming

● Rather than explicitly “sleeping” or “waking up”, actors “react” to the “events”● “events” = interactions with other actors = messages

● Actors get passive when they've finished their previous tasks and nobody has sent them new messages of interest

● Passive actors activate immediately when somebody has sent them an interesting message

Page 13: ACTOR-BASED MODEL FOR CONCURRENT PROGRAMMING

Asynchronous message Asynchronous message passingpassing

● Message sending is asynchronous● Each actor has a “mailbox” for storing messages

that are not consumed immediately.● If message arrives when actor is busy working on

a previous message, it gets stored in it's mailbox● If actor arrives at a point where it waits new

messages to continue, it first looks through it's mailbox.● Messages of unsuitable type are ignored● First suitable message allows actor to continue

Page 14: ACTOR-BASED MODEL FOR CONCURRENT PROGRAMMING

.. receive { case Approve() => .. case Cancel() => .. } ..

Answer(7) Approve()Cancel()

Blaa() Blaa()Answer(7)

newest

Asynchronous message Asynchronous message passing:passing:

Example of message receivingExample of message receiving

contents of mailbox

contents of mailbox

newest

CASE A

CASE B

Page 15: ACTOR-BASED MODEL FOR CONCURRENT PROGRAMMING

An exampleAn example

class Counter extends Actor { override def run: unit = loop(0)

def loop(value: int): unit = { Console.println("Value: " + value) receive { case Incr() => loop(value + 1) case Value(p) => p ! value loop(value) case _ => loop(value) } }}

C

time

X

Incr()

Y

value=0

value=1

Value(X)

1

Page 16: ACTOR-BASED MODEL FOR CONCURRENT PROGRAMMING

Actors for light-weight Actors for light-weight processesprocesses

● Thread-based concurrency usually directly mapped to operating system threads● 1 code-level thread = 1 operating system thread

● Operating system threads are expensive!● Most actor-based concurrency libraries don't map

actors directly to operating system threads (“light-weight actors”)

● 1 operating system thread = 1 active actor + unlimited amount of inactive actors

Page 17: ACTOR-BASED MODEL FOR CONCURRENT PROGRAMMING

Actors in distributed Actors in distributed programmingprogramming

● The actor model naturally extends from concurrency inside one computer to concurrency in a distributed network of computers

● Local and remote actors “look the same”● The concurrency framework will deal with the

technical details: message-passing over the network message serialization actor identificators management network connection management

Page 18: ACTOR-BASED MODEL FOR CONCURRENT PROGRAMMING

● Pros:● Event-driven approach is more intuitive● Directly and transparently applicable for

distributed programming● Implements light-weight processes => better

scaling

● Cons:● Message-passing is slower ● Libraries and solutions not yet as “mature”

(e.g. no good frameworks for Java before 2008)

The Actor modelThe Actor model

Page 19: ACTOR-BASED MODEL FOR CONCURRENT PROGRAMMING

Actor model implementationsActor model implementations

● Separate concurrency languages● Erlang● SALSA

● Frameworks● Scala Actor Framework for Scala● Kilim, FunctionalJava for Java● Kamaelia, Eventlet, PARLEY for Python● Dramatis, Revactor for Ruby

Page 20: ACTOR-BASED MODEL FOR CONCURRENT PROGRAMMING

Current importance & Current importance & perspectivesperspectives

● Cuncurrency in today's middleware almost always thread-based

● Hardware evolves towards more parallel architectures

● IT-industry evolves towards workforce being more costly than hardware

● In 2008, actor frameworks exist for all popular programming languages

● Cloud computing (thin clients, etc.)