CSC302 2011S Programming Languages
[Skip to Body]
Admin:
[Front Door]
[Schedule]
[Handouts]
[Honesty]
[Piazzza]
Current:
[Current Outline]
[Current EBoard]
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Languages:
[Clojure]
[Erlang]
[Haskell]
[Io]
[Prolog (GNU)]
[Ruby]
[Scala]
Misc:
[SamR]
[CSC302 2007S]
[7L7W]
Summary: We conclude our exploration of the Erlang programming language, focusing on multitasking.
Prerequisites: The first Erlang lab. The second Erlang lab. Tate, Sections 6.4-6.5.
Contents:
a. Create a directory for the lab.
b. Open a browser window on Tate's examples, in case you want to try any of them.
Try Tate's doctor example. You will also need to load the roulette library.
Just Let It Die
In Tate's roulette example,
a process kills itself when it receives a signal to do so. But, as we've
heard elsewhere in the chapter, the error handling model in Erlang is often
more like just let it die when something goes wrong
.
a. What do you expect to have happen if a process dies because of an error
(e.g., multiplying two lists) rather than through an explict call to
exit? E.g., Will the monitor receive a message and, if so,
what message do you expect it to receive?
b. Check your answer experimentally.
A typical strategy for viruses is to have multiple processes running, each of which notices when the others have died and restarts them. That strategy is obviously valuable for any robust multi-process system.
Write a simple program with two cooperating processes (plus a controller
to start the processes and send signals to them) in which each process is
responsible for monitoring the other process and restarting it if it crashes.
Here's a simple producer-consumer program that I found at
http://www.builderau.com.au/program/soa/Synchronous-message-passing-in-Erlang/0,339024614,339281535,00.htm
%% Taken from
%% http://www.builderau.com.au/program/soa/Synchronous-message-passing-in-Erlang/0,339024614,339281535,00.htm
-module(prodcon).
-export([start/0, consumer/0, producer/3]).
producer(_, _, 0) -> true;
producer(Me, Server, N) ->
Server ! {Me, N},
producer(Me, Server, N-1).
consumer() ->
receive
{Them, N} ->
io:format("~s ~w~n", [Them, N]),
consumer()
end.
start() ->
Server = spawn(prodcon, consumer, []),
spawn(prodcon, producer, ['A', Server, 10]),
spawn(prodcon, producer, ['B', Server, 5]),
io:format("finished start~n", []).
a. What do you expect this program to do? (E.g., if you compile the
file and then call prodcon:start().
b. Check your answer experimentally.
c. What happens when the producer reaches the base case? In particular, does the process continue, or does it die?
Rewrite the previous program so that producers take only their name and server
as parameters and respond to messages of the form send numbers from
N down to 1 to the server
. E.g., we might write
3> Server = spawn(prodcon, consumer, []).
4> p1 = spawn(prodcon, producer, ['A', Server]).
5> p2 = spawn(prodcon, producer, ['B', Server]).
6> p1 ! {spew, 10}.
7> p2 ! {spew, 5}.
8> p1 ! {spew, 7}.
Erlang lets you register a process with a name, using
register(Name,Pid).
a. What do you expect to have happen if you register two live processes with the same name? (E.g., if you created two producers similar to those in the previous exercise.)
b. Check your answer experimentally.
a. Experimentally determine whether you can link more than one listener to the same process.
b. If you can link more than one listener, determine what happens when a process dies. Do both get notified? Does only one?
If you find yourself with extra time, work on the current homework assignment.
Tuesday, 1 March 2011 [Samuel A. Rebelsky]
Wednesday, 2 March 2011 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CSC302/2011S/Labs/erlang-3.html.
[Skip to Body]
Admin:
[Front Door]
[Schedule]
[Handouts]
[Honesty]
[Piazzza]
Current:
[Current Outline]
[Current EBoard]
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Languages:
[Clojure]
[Erlang]
[Haskell]
[Io]
[Prolog (GNU)]
[Ruby]
[Scala]
Misc:
[SamR]
[CSC302 2007S]
[7L7W]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Mon Apr 25 08:06:44 2011.
The source to the document was last modified on Wed Mar 2 09:53:26 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC302/2011S/Labs/erlang-3.html.
A PDF version of this document may be found at
http://www.cs.grinnell.edu/~rebelsky/Courses/CSC302/2011S/Labs/erlang-3.pdf
You may wish to
validate this document's HTML
;
;