17.01.2015 Views

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

367<br />

specified. When the <strong>in</strong>it/1 function returns it is then that the start or start_l<strong>in</strong>k function used<br />

to start the behaviour process returns <strong>and</strong> unblocks the caller. This is to enable<br />

determ<strong>in</strong>istic startup of many processes such that when the start up function unblocks <strong>and</strong> a<br />

process id is returned that the process is ready <strong>and</strong> fully <strong>in</strong>itialized. A str<strong>in</strong>g of dependent<br />

processes can be started up each be<strong>in</strong>g sure the processes it depends on are fully <strong>in</strong>itialized.<br />

These properties are requisite for develop<strong>in</strong>g coherent <strong>and</strong> predictable concurrent systems.<br />

This start up <strong>and</strong> <strong>in</strong>itialization is accomplished via a library called proc_lib. Below is an<br />

implementation that blocks the startup function until the <strong>in</strong>it function, executed by the newly<br />

spawned process, has f<strong>in</strong>ished its execution.<br />

Code List<strong>in</strong>g 14.4 – proc_lib example<br />

-module(my_server).<br />

-export([start_l<strong>in</strong>k/0]).<br />

-export([<strong>in</strong>it/1]).<br />

start_l<strong>in</strong>k() -><br />

proc_lib:start_l<strong>in</strong>k(my_server, <strong>in</strong>it, [self()]). #1<br />

<strong>in</strong>it(Parent) -><br />

register(MODULE, self()),<br />

proc_lib:<strong>in</strong>it_ack(Parent, {ok, self()}), #2<br />

loop().<br />

loop() -><br />

receive<br />

Msg -><br />

h<strong>and</strong>le_msg(Msg),<br />

loop()<br />

end.<br />

At code annotation #1 proc_lib:start_l<strong>in</strong>k/3 is used to spawn a process. The function<br />

takes the arguments Module, Function <strong>and</strong> Arguments which <strong>in</strong>dicate which function to<br />

spawn with <strong>and</strong> how to spawn that function. In this case my_server:<strong>in</strong>it(self()) is called.<br />

Self is the process id of the call<strong>in</strong>g process, the one that is to be l<strong>in</strong>ked to, the one that is<br />

do<strong>in</strong>g the actual spawn<strong>in</strong>g. Pass<strong>in</strong>g this parent pid to the child process via the <strong>in</strong>it function<br />

allows the code at code annotation #2 to function. After all the registration steps have been<br />

accomplished by the <strong>in</strong>it/1 function the proc_lib:<strong>in</strong>it_ack function is called to send a message<br />

back to the parent process <strong>in</strong>dicat<strong>in</strong>g that the proc_lib:start_l<strong>in</strong>k or start function can<br />

unblock.<br />

PROCESS LOOPS<br />

As mentioned earlier processes are just functions that loop, which means loop recursively.<br />

This means that they better be properly tail recursive or the stack will grow to consume<br />

©Mann<strong>in</strong>g Publications Co. Please post comments or corrections to the Author Onl<strong>in</strong>e forum:<br />

http://www.mann<strong>in</strong>g-s<strong>and</strong>box.com/forum.jspaforumID=454

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!