Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Erlang (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Concurrency and distribution orientation== Erlang's main strength is support for [[Concurrency (computer science)|concurrency]]. It has a small but powerful set of primitives to create processes and communicate among them. Erlang is conceptually similar to the language [[occam (programming language)|occam]], though it recasts the ideas of [[communicating sequential processes]] (CSP) in a functional framework and uses asynchronous message passing.<ref>{{cite journal |title=Erlang |journal=[[Communications of the ACM]] |volume=53 |issue=9 |date=September 2010 |pages=68β75 |doi=10.1145/1810891.1810910 |first=Joe |last=Armstrong |author-link=Joe Armstrong (programmer) |quote=Erlang is conceptually similar to the occam programming language, though it recasts the ideas of CSP in a functional framework and uses asynchronous message passing. |doi-access=free }}</ref> Processes are the primary means to structure an Erlang application. They are neither [[operating system]] [[Process (computing)|processes]] nor [[Thread (computing)|threads]], but [[Light-weight process|lightweight processes]] that are scheduled by BEAM. Like operating system processes (but unlike operating system threads), they share no state with each other. The estimated minimal overhead for each is 300 [[Word (computer architecture)|words]].<ref>{{cite web |title=Erlang Efficiency Guide β Processes |url=http://www.erlang.org/doc/efficiency_guide/processes.html|archive-url=https://web.archive.org/web/20150227174813/http://www.erlang.org/doc/efficiency_guide/processes.html|archive-date=27 February 2015}}</ref> Thus, many processes can be created without degrading performance. In 2005, a benchmark with 20 million processes was successfully performed with 64-bit Erlang on a machine with 16 GB [[random-access memory]] (RAM; total 800 bytes/process).<ref>{{cite web |first=Ulf |last=Wiger |title=Stress-testing erlang |url=https://groups.google.com/group/comp.lang.functional/msg/33b7a62afb727a4f?dmode=source |work=comp.lang.functional.misc |access-date=25 August 2006 |date=14 November 2005}}</ref> Erlang has supported [[symmetric multiprocessing]] since release R11B of May 2006. While [[Thread (computing)|threads]] need external library support in most languages, Erlang provides language-level features to create and manage processes with the goal of simplifying concurrent programming. Though all concurrency is explicit in Erlang, processes communicate using [[message passing]] instead of shared variables, which removes the need for explicit [[Lock (computer science)|locks]] (a locking scheme is still used internally by the VM).<ref>{{cite web |title=Lock-free message queue |url=http://erlang.2086793.n4.nabble.com/Lock-free-message-queue-td2550221.html |access-date=23 December 2013 |archive-date=24 December 2013 |archive-url=https://web.archive.org/web/20131224104549/http://erlang.2086793.n4.nabble.com/Lock-free-message-queue-td2550221.html |url-status=dead }}</ref> [[Inter-process communication]] works via a [[shared-nothing architecture|shared-nothing]] [[asynchronous method dispatch|asynchronous]] [[message passing]] system: every process has a "mailbox", a [[queue (data structure)|queue]] of messages that have been sent by other processes and not yet consumed. A process uses the <code>receive</code> primitive to retrieve messages that match desired patterns. A message-handling routine tests messages in turn against each pattern, until one of them matches. When the message is consumed and removed from the mailbox the process resumes execution. A message may comprise any Erlang structure, including primitives (integers, floats, characters, atoms), tuples, lists, and functions. The code example below shows the built-in support for distributed processes: <syntaxhighlight lang="erlang"> % Create a process and invoke the function web:start_server(Port, MaxConnections) ServerProcess = spawn(web, start_server, [Port, MaxConnections]), % Create a remote process and invoke the function % web:start_server(Port, MaxConnections) on machine RemoteNode RemoteProcess = spawn(RemoteNode, web, start_server, [Port, MaxConnections]), % Send a message to ServerProcess (asynchronously). The message consists of a tuple % with the atom "pause" and the number "10". ServerProcess ! {pause, 10}, % Receive messages sent to this process receive a_message -> do_something; {data, DataContent} -> handle(DataContent); {hello, Text} -> io:format("Got hello message: ~s", [Text]); {goodbye, Text} -> io:format("Got goodbye message: ~s", [Text]) end. </syntaxhighlight> As the example shows, processes may be created on remote nodes, and communication with them is transparent in the sense that communication with remote processes works exactly as communication with local processes. Concurrency supports the primary method of error-handling in Erlang. When a process crashes, it neatly exits and sends a message to the controlling process which can then take action, such as starting a new process that takes over the old process's task.<ref>{{cite web |first=Joe |last=Armstrong |title=Erlang robustness |url=http://www.erlang.org/doc/getting_started/robustness.html|archive-url=https://web.archive.org/web/20150423182840/http://www.erlang.org/doc/getting_started/robustness.html|archive-date=23 April 2015 |access-date=15 July 2010}}</ref><ref>{{cite web |title=Erlang Supervision principles |url=http://www.erlang.org/doc/design_principles/sup_princ.html|archive-url=https://web.archive.org/web/20150206050600/http://www.erlang.org/doc/design_principles/sup_princ.html|archive-date=6 February 2015 |access-date=15 July 2010}}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)