sign in
Home | Updates | Pages | Users | Admin | Help

erlangstudy2

Erlang Study – Concurrent Programming

Processes

In Erlang we call each thread of execution a process. The Erlang BIF spawn is used to create a new process:

spawn(Module, Exported_Function, List of Arguments).

%% Sample
-module(tut14).
-export([start/0, say_something/2]).
...
start() ->
    spawn(tut14, say_something, [hello, 3]).
...
Note that a function used in this way by spawn to start a process must be exported from the module (i.e. in the -export at the start of the module). spawn returns a process identifier, or pid, which uniquely identifies the process.

Message Passing

The receive construct is used to allow processes to wait for messages from other processes. It has the format:
receive
   pattern1 ->
       actions1;
   ....
   patternN
       actionsN
end.

Note: no ”;” before the end.

Messages between Erlang processes are simply valid Erlang terms. I.e. they can be lists, tuples, integers, atoms, pids etc.

Operator ”!” is used to send messages. The syntax of ”!” is:
Pid ! Message

I.e. Message (any Erlang term) is sent to the process with identity Pid.

Registered Process Names

Sometimes processes which need to know each others identities are started completely independently of each other. Erlang thus provides a mechanism for processes to be given names so that these names can be used as identities instead of pids. This is done by using the register BIF:
register(some_atom, Pid)
For example:
-module(tut16a).
-export([start/0, ping/1, pong/0]).

ping(0) ->
    pong ! finished,
    io:format("ping finished~n", []);

ping(N) ->
    pong ! {ping, self()},
    receive
        pong ->
            io:format("Ping received pong~n", [])
    end,
    ping(N - 1).

pong() ->
    receive
        finished ->
            io:format("Pong finished~n", []);
        ping ->
            io:format("Pong received ping~n", []),
            ping ! pong,
            pong()
    end.

start() ->
    register(pong, spawn(tut16, pong, [])),
    register(ping, spawn(tut16, ping, [3])).

Distributed Programming

The distributed Erlang implementation provides a basic security mechanism to prevent unauthorized access to an Erlang system on another computer. Erlang systems which talk to each other must have the same magic cookie. The easiest way to achieve this is by having a file called .erlang.cookie in your home directory on all machines which on which you are going to run Erlang systems communicating with each other.
$ cd
$ cat > .erlang.cookie
this_is_very_secret
$ chmod 400 .erlang.cookie
When you start an Erlang system which is going to talk to other Erlang systems, you must give it a name, eg:
erl -sname my_name 
If you want to experiment with distributed Erlang, but you only have one computer to work on, you can start two separate Erlang systems on the same computer but give them different names.

Each Erlang system running on a computer is called an Erlang node.

Note: erl -sname assumes that all nodes are in the same IP domain and we can use only the first component of the IP address, if we want to use nodes in different domains we use -name instead, but then all IP address must be given in full

Erlang pids contain information about where the process executes so if you know the pid of a process, the ”!” operator can be used to send it a message if the process is on the same node or on a different node.

A difference is how we send messages to a registered process on another node:
{pong, Pong_Node} ! {ping, self()},
We use a tuple {registered_name,node_name} instead of just the registered_name .

An Erlang process will (conceptually) run until it does a receive and there is no message which it wants to receive in the message queue. “conceptually” because the Erlang system shares the CPU time between the active processes in the system.

A process terminates when there is nothing more for it to do, i.e. the last function it calls simply returns and doesn’t call another function. Another way for a process to terminate is for it to call exit/1 .

he BIF whereis(RegisteredName) checks if a registered process of name RegisteredName exists and return the pid of the process if it does exist or the atom undefined if it does not.
whereis(mess_client)

Powered by JunebugWiki v0.0.31 Page last edited by stoyan on March 12, 2007 12:23 PM (diff)
[readonly] Version 4 «oldernewer»currentversions