Can messages be sent to processes interactively?

Joe Armstrong joe@REDACTED
Fri May 21 10:15:36 CEST 1999


Craig Dickson wrote:

> James Hague wrote:
>
> > I wrote a quickie test process so I can start it up and send messages to
> > it interactively, just so I know that all is well:
> >
> > -module(sample).
> > -export([start/0]).
> >
> > start() -> spawn(sample, server, []).
> >
> > server() ->
> >    receive
> >       stop -> ok;
> >       Msg  ->
> >          io:fwrite("message received: ~w\n", [Msg]),
> >          server(N)
> >   end.
> >
> > This compiles just fine
>
> No, it doesn't. Variable N is unbound, and no function server/1 is defined.
> Furthermore, since you didn't export server/0, you can't spawn it.
>
> If you really want to be sure (interactively) that your process has started,
> have it output something when it starts up.
>
> This ought to compile and work as you expect:
>
> --- cut here ---
> -module(sample).
>
> -export([start/0, server/0]).
>
> start() -> spawn(?MODULE, server, []).
>
> server() ->
>     io:fwrite("server started\n"),
>     receive
>         stop -> io:fwrite("server stopping\n"), ok;
>         Msg -> io:fwrite("message received: ~w\n", [Msg])
>     end.
> --- cut here ---
>
> You should find that you can send messages to this server from the Erlang
> shell.
>
> Craig

Right - but I think I should not point out the difference between spawn and
spawn_link

Suppose I write the following:


-module(sample).
-export([start/0, server/0]).

start() -> spawn_link(sample, server, []).

server() ->
   receive
      stop -> ok;
      Msg  ->
         1 = 2,
         server()
  end.

This contains a deliberate error but it won't be reported
1> P=sample:start().
<0.30.0>
2> P!a
2> .
a
3> P!2.
2

The process died but we saw nothing. P is still a process reference but it
point to crap. P!2 etc.  succeeds.

(X!Y has send and pray semantics :-)

Now change spawn to spawn_link.  "spawn_link" says spawn the process and link
to it for
error handling purposes. Deliver an exit message if something goes wrong. so
...

1> P=sample:start().
<0.30.0>
2> P!1.
1

=ERROR REPORT==== 21-May-1999::10:09:00 ===
<0.30.0> error: {badmatch,2} in sample:server/0
** exited: {{badmatch,2},{sample,server,0}} **
3> P!2.
2
4> process_info(P).
undefined
5>

Line 1> caused the spawned process to be linked to the shell process.

Line 2> Sends a message to P. The process crashes with a badmatch error (i.e. 1
cannot be 2)
Because it is linked to the shell an exit message is sent to the shell. The
shell has a handler
which prints the value of the exit signal.

Line 3> succeeds. P is still a reference to a proceess but the process is not
dead so P ! 2 succeeds

Line 4> you can inspect the "state" of P - it is of course, dead.

It's often a good idea to use spawn_link and lot and spawn sparingly if you're
a beginner
That way you get the default error handling mechanisms - gurus would probably
use
spawn a lot and spawn_link sparingly and make their own error handling
mechanisms.

    /Joe









More information about the erlang-questions mailing list