Clarify my doubt please

Raimo Niskanen raimo@REDACTED
Fri Sep 23 13:45:56 CEST 2005


surindar.shanthi@REDACTED (Surindar Sivanesan) writes:

> Please reply me:
> 1) [1,2,3,4] is a list,whether "surindar" is list.If yes tell me how?(Please
> apologise me if the question is very silly.Im just now started learning
> Erlang).

"surindar" is syntactical sugar for (the same as) [$s,$u,$r,$i,$n,$d,$a,$r]
($s i the ASCII code for the character s).

> 2) give me some details about monitoring&demonitoring with some simple
> examples.

A monitor is a one-directional supervision link. When you have executed
erlang:monitor/2 you are guaranteed to receive a {'DOWN',Mref,_,_,_}
message if the process you tried to monitor did not exist or when it
ceases to exist for any reason.

After you have executed erlang:demonitor/1, you may have a 
{'DOWN',Mref,_,_,_} in the receive queue, but you will not get one later.

You can set any number of monitors on a process from a process and
they are independant of each other. The returned Mref is your handle 
to a specific monitor. To compare with process links where there
can be only one link between any pair of two processes, so you
can not reliably set a link in a library function because the
library function has no idea of whether the process may have
a process link to another process or not.


Example of server start of a server with a registered name:

start_server() ->
    Parent = self(),
    Child = spawn(fun() ->
                      register(my_server_name, self()),
                      Parent ! {self(), registered},
                      server_loop()
                  end),
    Mref = erlang:monitor(process, Child),
    receive
        {Child,registered} ->
            %% Demonitor and cleanup stray DOWN message just in case.
            erlang:demonitor(Mref),
            receive {'DOWN',Mref,_,_,_} -> ok after 0 -> ok end,
            %% Now we know that the server managed to register
            %% its name.
            {ok,Child};
        {'DOWN',Mref,_,_,_} ->
            %% Now we know the server failed to start, most
            %% probably because the register/2 failed, because
            %% another instance of the server was already started.
            {error,already_started}
    end.

server_loop() ->
    %% loop the loop...


> 3)What is meant by group leader in Erlang?

The group leader for an Erlang process corresponds to standard output for 
a Unix process. The group leader is the default receiver of printout
from e.g io:format/2.

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list