A few months ago, an article on Erlang in OFY reminded me that I had never got around to exploring Erlang. Erlang is fascinating because among other features, it was designed
These features make it natural for Erlang to use multiple cores of a processor. It also makes it easy to develop a distributed system.
You can get started with tutorials on the erlang.org site. Learning the syntax of a new language is not hard. Since it is a functional programming language, you may face a couple of challenges when moving from an imperative or object oriented language.
Even if you have not done the tutorials, you should not find it hard to read Erlang code. So, consider a function, n_echo, which echoes a message N times. Write the following code in a text file, cp.erl:
The syntax of the format function is similar to the print function in Python 3, with ~ replacing \.
In the Erlang shell(erl command), type the following
As you would expect, you get one printed 3 times followed by two printed 3 times. In order to run these functions concurrently, you can spawn processes. Define a convenience function, Spawn_echo:
The parameters of spawn are the module name, the function name and the list of parameters. Now, you can use this convenience function to spawn a list of processes:
The foreach function will apply Spawn_echo function to each element of the list. Now, you will find that one, two and three are printed 3 times each but running concurrently. Programming concurrency in Erlang is about as easy as it can be.
Now, consider the example of two programs, ping and pong. Ping sends a message to pong and waits for a reply. Pong is waiting for a message and responds after receiving a message. Add these methods to the cp.erl file and modify the export line as follows:
Note how easy it is to send a message – Process ID ! <data>. Data can be an atom, a tuple, a list, etc. The processing of a receive statement is very much like a case statement. Depending upon what is received, the appropriate code is executed.
The ping_pong function spawns both processes. Pong is started first and its process id is passed to ping. Once ping starts, it sends a message to pong along with its process id.
Now, from Erlang shell,
In ping_pong, spawn twice as follows:
You should alter pong so that it does not die after receiving a quit message:
Now compile cp and run ping_pong again. You can notice that you have the structure of a client/server program.
However, if the ping processes need to be run at various occasions, it may not be possible to have the process id of the pong process. Erlang provides an option to register a process. Other processes can then use the symbolic reference. You will alter the ping_pong function as follows:
Notice that you no longer pass the process id of pong to ping. Ping will use the symbolic name pong to send a message. The changes to the export statement and the ping function will be as follows:
With this brief exploration, you should be convinced that Erlang is an excellent option for writing code to exploit multiple cores of the current processors and will try it out. Learning a new language is fun!
Exploring Software >