Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 312 Concurrent Programming I Processes and Messages 1.

Similar presentations


Presentation on theme: "Computer Science 312 Concurrent Programming I Processes and Messages 1."— Presentation transcript:

1 Computer Science 312 Concurrent Programming I Processes and Messages 1

2 Mapping 1> lists:map(math:sqrt, [4, 16, 25]). ** exception error: bad function sqrt in function lists:map/2 (lists.erl, line 1224) 2> lists:map(fun(X) -> math:sqrt(X) end, [4, 16, 25]). [2.0, 4.0, 5.0] Must create a fun to pass a function as data, but syntax is complicated

3 Mapping Can use a function reference instead!
1> lists:map(math:sqrt, [4, 16, 25]). ** exception error: bad function sqrt in function lists:map/2 (lists.erl, line 1224) 2> lists:map(fun(X) -> math:sqrt(X) end, [4, 16, 25]). [2.0, 4.0, 5.0] Can use a function reference instead! 3> lists:map(fun math:sqrt/1, [4, 16, 25]). [2.0, 4.0, 5.0]

4 Concurrency-Oriented Programming
Modern programs execute in multiple processes, which may run on separate processors, either within a single computer (multicore) or on different computers across a network The discipline of designing and implementing such programs is called concurrency-oriented programming

5 Shared Memory vs Message-Passing
Processes can share memory, but then must be regulated to avoid synchronization problems (deadlock, lockout, race conditions) Alternatively, each process gets its own private memory, and they communicate by sending and receiving messages Erlang combines the functional model with message passing

6 Just Three Concurrency Primitives
Pid = spawn(Fun) – Creates a new process that executes the function Fun. Pid can then be used to send messages to the process. Pid ! Message – Sends Message to the process Pid. The sender then continues execution. receive ... end – Receives a message. See next slide . . .

7 The Basic Details of receive
Pattern1 [Guard1] -> Expressions1; Pattern2 [Guard2] -> Expressions2; . . . after Time -> AfterExpressions end When a message comes in, receive executes the expression for the first pattern that the message matches. If no match is found, the message goes on a queue and receive waits for another message. The timer is optional.

8 Where Do Messages Come From?
Senders Mailbox Receiver Messages Save queue Messages from senders are placed in the receiver’s mailbox. The receiver removes these and handles them in sequence.

9 Where Do Messages Come From?
Senders Mailbox Receiver Messages Save queue If no messages match, they remain on the save queue and the receiver suspends execution.

10 Where Do Messages Come From?
Senders Mailbox Receiver Messages Save queue If a new message arrives at an empty mailbox, the receiver resumes execution but the saved messages remain on the save queue.

11 Where Do Messages Come From?
Senders Mailbox Receiver Messages Save queue If a message matches, the messages on the save queue are placed back in the mailbox and the timer (if set) is cleared.

12 Where Do Messages Come From?
Senders Mailbox Receiver Messages Save queue If the timer elapses during the wait for a message, its expression is run and the messages on the save queue are moved back to the mailbox.

13 A Simple Greeting Server
% greeting_server0.erl -module(greeting_server0). -export([loop/0]). loop() -> receive hello -> io:format("Hello!~n"), loop(); goodbye -> io:format("Goodbye!~n"), Other -> io:format("Message not recognized: ~p ~n", [Other]), loop() end.

14 Spawn and Send Messages
1> Pid = spawn(fun greeting_server:loop/0). <0.42.0> 2> Pid ! hello. Hello! hello 3> Pid ! goodbye. Goodbye! goodbye 4> Pid ! ken. Message not recognized: ken ken

15 A Simple Greeting Server
% greeting_server0.erl -module(greeting_server0). -export([loop/0]). loop() -> receive hello -> io:format("Hello!~n"), loop(); goodbye -> io:format("Goodbye!~n"), Other -> io:format("Message not recognized: ~p ~n", [Other]), loop() end. Because the server loop is tail-recursive, it can in principle run forever.

16 Client/Server Programming
Many Clients Requests Server Replies The clients and servers are processes, running on the same computer or different computers A server handles a client’s request and replies to the client with the result.

17 Provide a Return Address for the Reply
In the client’s code: Server ! {self(), hello}. self() returns the ID of the currently executing process In the server’s code: loop() -> receive {Client, hello} -> Client ! "Hello!~n", loop(); . . .

18 For next time Remote procedure call


Download ppt "Computer Science 312 Concurrent Programming I Processes and Messages 1."

Similar presentations


Ads by Google