Posts Tagged ‘series’
Introduction to Erlang : Shared Memory Example
Shared Memory
This post will be about building step by step a shared memory abstraction in Erlang. As you should all know, variables in Erlang are immutable; once a variable is bound to a value, this value cannot change. Thus in order to implement a mutable variable/object we need to represent it with a recursive process responsible for keeping the current value and providing the interface for using the object (read and write operations for example).
So, in order to implement a memory abstraction we have to use the aforementioned approach. We can either create a single process to be responsible for keeping the whole address space as a simple list, or create one process for each allocation operation. We will follow the second approach because it is more interesting and protects the memory process from becoming the bottleneck.
Let’s start!
The Messaging Interface
The interface of the memory system is quite simple. We just need the following operations:
start
: for starting the memory systemstop
: for stopping the memory systemalloc
: for allocating memoryfree
: for freeing memoryread
: for reading the value of a memory addresswrite
: for writing to a memory address
From the above, the four first operations will be handled by the memory system, while the two last by each memory address (process) that they refer to. We will create two modules:
mem
: the memory systemmemif
: the memory interface
and one file called “common” with the parameters of the system.
-define(MEMREGNAME, mem). -define(MAXSIZE, 100). |
This file will be include in both other modules (-include("common")
directive).
Read the rest of this entry »
Introduction to Erlang : Concurrency (Processes)
Concurrency
Concurrency is defined as “the temporal property of two things happening at the same time” (according to WordNet). In Computer Science, the definition is “concurrency is a property of systems in which several computations are executing simultaneously, and potentially interacting with each other. The computations may be executing on multiple cores in the same chip, preemptively time-shared threads on the same processor, or executed on physically separated processors.” (according to Wikipedia).
In practise, if two, or more, tasks are concurrent, then they can run in parallel. This implies that there are no data dependencies among them. For example, the following tasks
- unlock the car (the car was locked)
- start the engine of the car (the engine was off)
- drive the car
are non-concurrent since they have dependencies. One cannot start the engine of the car without first entering the car and she cannot drive it without first starting the engine. These tasks have to be executed sequentially (with a predefined order) no matter if more than one person (processing unit) help. On the other hand
- doing the laundry
- washing the dishes
- go for shopping
can be potentially run in parallel. For example, three different family members can handle one task each. Moreover, if only one person has to do all the tasks, she can decide to perform them in any order she decides since there are no dependencies among them.
Read the rest of this entry »
Introduction to Erlang : BIFs & Predefined Modules
Built-in Functions (BIFs)
Erlang’s Built-in Functions (shorthand BIFs) are commonly used functions that are intergrated into the Erlang’s VM for performance reasons. Most of them belong to the erlang
module, but there are some in other modules, such as lists
.
The BIFs can be separated to standard and non-standard. The standard ones are auto-imported; they can be called without the use of the module name prefix (remember the effect of the -import(...)
directive). On the other hand, the non-standard ones have to be called following the normal module:function(...)
convension. In the erlang
module’s man pages (here) the distinction between standard and non-standard is visible by the lack or existence of the erlang
(module’s name) prefix.
elrang
abs/1
Arithmetic absolut value of an integer or float.
erlang:append_element/2
Appends an element to a tuple.
apply/2|3
Calls the function passed as a parameter.
atom_to_list/1
Returns a string which corresponds to the text representation of Atom.
Read the rest of this entry »
Introduciton to Erlang : Recursion (2/2)
Accumulators
In several cases, as with the mlists:length/1
example, the non-tail recursive function can be easily turned to a tail recursive one by using the notion of accumulator. An accumulator is an extra argument introduced to a function in order to aggregate the partial results of the function. It turns the “bottom-up” collection of the final result to “top-down”.
In order to add and initialize the accumulator argument one has to introduce an extra function definition.
tlr(...) -> tlr(..., Accumulator_initial_value). % the clause that "breaks" the recursion and % returns the result tlr(..., Accumulator) -> Accumulator; tlr(..., Accumulator) -> ..., Accumulator_new_value = ..., ..., trl(..., Accumulator_new_value). |
Notice that typically you would only export the tlr/1
function and the tlr/2
would remain for inner-module use and not visible to the module’s users.
Read the rest of this entry »
Introduction to Erlang : Recursion (1/2)
Recursion
The definition of the word recursion is “(mathematics) an expression such that each term is generated by repeating a particular mathematical operation”, according to the WordNet. Recursion is one of the most powerful “tools” in a functional programming language and so it is for Erlang. Recursion can be used to apply divide and conquer techniques to problem solving, where a problem is broken to smaller subproblems, the subproblems are solved, and the results are “merged” to generate the final result.
Recursion happens when a function’s body definition includes a call to the function itself.
functionA(...) -> Body_before_recursion, % optional functionA(...), Body_after_recursion. % optional |
Recursion is used instead of the conventional loop statements of other programming languages, such as while
and for
in C.
Read the rest of this entry »