[erlang-questions] Newbie question can't explain behaviour of my program (tutorial exercise masterslave)
Emil Hellman
emil.hellman@REDACTED
Tue Jun 23 13:26:20 CEST 2009
This is somewhat of a wild guess so more experienced ppl can correct me if
I'm misstaken (I have no erlang shell here to verify it). I think that the
Erlang shell doesn't print out the io:formats when you are writing a
command. That is, the calls to io:format that occurs before the call
to masterslave:start(5). returns gets printed. The ones that occur after
that only gets printed after you do something on the command line. Could try
writing the output to a file instead of the shell and then I guess this
pause (it's not a pause but it seems that way in the shell) would not occur.
Or you could try executing any other command like:
1 + 1.
and then the other io:formats would get printed. Again this is somewhat of a
guess... it was a few years since I did any Erlang :).
- Emil H
Since it was a while I did any Erlang development at all I might be wrong
about this.
On Tue, Jun 23, 2009 at 10:24 AM, Folkert Hendrix <fjmhendrix@REDACTED>wrote:
> Hi,
>
> Thanks for your explanation of:
> >>
> >> In the process view of toolbar:start(). I only see 4 slaves created.
> >> When i give the command masterslave:to_slave(hello,2) then the rest of
> >> the slaves are created and can i see the registered master. Slave 2
> >> responds but it wasn't created yet?
> >
> > When your master process is created it is in the init function it starts
> > executing. There it calls the init_slave function, when the counter
> reaches
> > 0 all slaves have been started and the master process enters the
> loop_master
> > function. Before this occurs the master never handles any of the messages
> it
> > has received. However, the message you send it by calling to_slave is
> still
> > in the master process message box. So after the master has created all
> the
> > slave processes it starts handling its messages. Therefore when the
> master
> > handles the message that it should forward the message to the given slave
> > process, that slave process is guaranteed to already have been started.
>
> OK, I agree, but it doesn't explain why I only see the output of Slave
> 5 till Slave 3 (in init_slave(N, Slaves) ->) and then nothing (I can
> wait forever) . Only when I enter a command in the console, the other
> slaves write their output. Is the instruction io:format("Slave: ~w
> with Pid: ~w created~n",[N, Pid]), during init_slaves blocking? If i
> remove the io:format part, the program creates all processes
> instantly. With the io:format insruction only for processes are
> created. (Computer with 4 cores)?
>
>
> >> this is the output i get:
> >> (erlide@REDACTED)6> masterslave:start(5).
> >> Slave: 5 with Pid: <0.97.0> created
> >> true
> >> Slave: 4 with Pid: <0.98.0> created
> >> Slave: 3 with Pid: <0.99.0> created
> >> (erlide@REDACTED)7> masterslave:to_slave(hello,2).
> >> Slave: 2 with Pid: <0.101.0> created
> >> Slave: 1 with Pid: <0.106.0> created
> >> Slave 2 recieved the message: hello
> >> {message_to_slave,{hello,2}}
> >> (erlide@REDACTED)8> masterslave:to_slave(die,1).
> >> {message_to_slave,{die,1}}
> >> Slave 1 with old PID <0.106.0> restarted with new Pid: <0.108.0>
> >> (erlide@REDACTED)9> masterslave:stop().
> >> Master going downstop
> >> (erlide@REDACTED)10>
> >>
>
>
> >>
> >> When the master stop, the slaves also stop, but i expected the slave
> >> received the exit signal and should write a message.
> >
> > I believe this is because the slaves is never told to trap exit signals.
> >
> > Hope that helps!
> > - Emil H
>
> Yes, it works when the slaves trap the exit signal.
>
> Regards,
> Folkert
>
More information about the erlang-questions
mailing list