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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

10<br />

That’s all. (Although the spawn function has some other variants, this is the simplest.) This<br />

will start a separate process which will pr<strong>in</strong>t the text “erlang!” on the console, <strong>and</strong> then quit.<br />

In chapter 2 we will get <strong>in</strong>to details about the <strong>Erlang</strong> language <strong>and</strong> its syntax, but for the<br />

time be<strong>in</strong>g we hope you will simply be able to get the gist of our examples without further<br />

explanation. One of the strengths of <strong>Erlang</strong> is that it is generally pretty easy to underst<strong>and</strong><br />

the code even if you’ve never seen the language before. Let’s see if you agree.<br />

1.1.5 – How processes talk<br />

Processes need to do more than spawn <strong>and</strong> run however—they need to communicate. <strong>Erlang</strong><br />

makes this communication quite simple. The basic operator for send<strong>in</strong>g a message is !,<br />

pronounced “bang”, <strong>and</strong> it is used on the form “Dest<strong>in</strong>ation ! Message”. This is message<br />

pass<strong>in</strong>g at its most primitive, like mail<strong>in</strong>g a postcard. <strong>OTP</strong> takes process communication to<br />

another level, <strong>and</strong> we will be div<strong>in</strong>g <strong>in</strong>to all that is good with <strong>OTP</strong> <strong>and</strong> messag<strong>in</strong>g later on,<br />

but for now, let’s marvel at the simplicity of communicat<strong>in</strong>g between two <strong>in</strong>dependent <strong>and</strong><br />

concurrent processes illustrated <strong>in</strong> the follow<strong>in</strong>g snippet..<br />

run() -><br />

Pid = spawn(fun p<strong>in</strong>g/0),<br />

Pid ! self(),<br />

receive<br />

pong -> ok<br />

end.<br />

p<strong>in</strong>g() -><br />

receive<br />

From -> From ! pong<br />

end.<br />

Take a m<strong>in</strong>ute <strong>and</strong> look at the code above. You can probably underst<strong>and</strong> it without any<br />

previous knowledge of <strong>Erlang</strong>. Po<strong>in</strong>ts worth not<strong>in</strong>g are: another variant of the spawn<br />

function, that here gets just a s<strong>in</strong>gle reference to “the function named p<strong>in</strong>g that takes zero<br />

arguments” (fun p<strong>in</strong>g/0); <strong>and</strong> also the function self() that produces the identifier of the<br />

current process, which is then sent on to the new process so that it knows where to reply.<br />

That’s it <strong>in</strong> a nutshell: process communication. Every call to spawn yields a fresh process<br />

identifier that uniquely identifies the new child process. This process identifier can then be<br />

used to send messages to the child. Each process has a “process mailbox” where <strong>in</strong>com<strong>in</strong>g<br />

messages are stored as they arrive, regardless of what the process is currently busy do<strong>in</strong>g,<br />

<strong>and</strong> are kept there until it decides to look for messages. The process may then search <strong>and</strong><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!