[erlang-questions] Newbie question can't explain behaviour of my program (tutorial exercise masterslave)
Tue Jun 23 10:24:24 CEST 2009
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:
>> ()6> masterslave:start(5).
>> Slave: 5 with Pid: <0.97.0> created
>> Slave: 4 with Pid: <0.98.0> created
>> Slave: 3 with Pid: <0.99.0> created
>> ()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
>> ()8> masterslave:to_slave(die,1).
>> Slave 1 with old PID <0.106.0> restarted with new Pid: <0.108.0>
>> ()9> masterslave:stop().
>> Master going downstop
>> 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.
More information about the erlang-questions