Friday, August 24, 2007

Erlang: Concurrency and Distribution (Part 2)

One of the main strengths for Erlang is support for concurrency and distribution. It has a small set of primitives to create processes and communicate between them. Erlang processes are not operating system processes or threads, but they are lightweight threads similar to Java's "green threads".

Erlang is designed to be run in a distributed multi-node environment. Every computation in Erlang is performed within a process. Processes have no shared memory and communicate by asynchronous message passing. This is the philosophy of Erlang, the Concurrency Oriented Programming (COP). You have to think of your design from this prospective. Your application consists of a set of processes may be distributed on different machines.

Here is a simple application to illustrate the feautres I was talking about. A process sends a message to another process using the send construct: Pid ! Message. Even if the Pid doesn’t exist, the send will succeed. A process can block to receive a message by using the receive construct. The receive construct may also take an optional timeout value in millseconds, after which the “after” clause of the receive construct is executed.


-module(IPCExample).

-export([main/0, thread/0]).

main() ->

Pid = spawn(receive1, thread, []),

io:fwrite("Spawned new process _w_n", [Pid]),

Pid ! hello.

thread() ->

io:fwrite("This is a thread._n", []),

process_messages().

process_messages() ->

receive

hello ->

io:fwrite("Received hello_n"),

process_messages()

after 2000 ->

io:fwrite("Timeout._n")

end.


Process IDs do not have to refer processes running on the same machine as the processes can be distributed an remote machines. So you can spawn a process using the node name and use the returned process ID to send messages just as if it were a process on the local machine.


Pid = spawn(remote_node,process1, thread, []) Pid ! a_message
Pid ! a_message


Erlang handles the creation of the processes very efficiently. Here is a simple graph comparing Java, C#, Erlang comparing the time taken to create processes and asynchronous message passing.


This figure compares the time of creation of processes in Erlang versus Java and C#. We observe that the time taken to create an Erlang process is a constant 1 micro second up to 2,500 processes; and then it increases to about 3 micro second for up to 30,000 processes. As for Java and C# a small number of processes takes about 300 micro second to create a process. Creating more than two thousand processes is impossible.
This figure compares the time of message passing between different processes in Erlang versus Java and C#. The time to send a simple message between two concurrent processes running on the same machine as a function of the total number of processes. We see that for up to 30,000 processes the time to send a message between two Erlang processes is about 0.8 micro second. For C# it takes about 50 micro second. per message, up to the maximum number of processes (which was about 1800 processes). Java was even worse, for up to 100 process it took about 50 micro second per message thereafter it increased rapidly to 10ms per message when there were about 1000 Java processes.

It is quite clear that Java and C# can't compete with Erlang in this features. We have to realize that the world is going towards parallel processing even we don't accept Erlang as a language I think it will take place in the next decade.

To be continued ...

Ruby vs Java, PHP, dot NET ... Hilarious :D

I have never imagined that comparing web development frameworks would be that funny ... Ruby Rulez :D.
Ruby vs Java
Comparing Jars of Java to Ruby.



Ruby vs dot NET
Or we can say dot NOT :D.




Ruby vs PHP
I didn't know that php is this sticky.
Round 1

Round 2

Round 3


Round 4




Sunday, August 19, 2007

Erlang: The Introduction (Part 1)

Is Erlang really going to be the next Java according to Ralph's Blog? Erlang is really a powerful programming language, but thinking of it as a replacement for Java is not realistic. Developers are using Java over a decade now, they already have their applications, frameworks, libraries, ... etc. all written in Java. I don't see a clear reason to leave all this and start again from a scratch using another programming language. I am not completely against Erlang, although I am not completely with Java but I will try to predict the answer for the question I asked at the beginning. I will talk a bit about Erlang before I start comparing it to Java.

Introduction

Erlang is a general-purpose concurrent programming language and runtime system. It was designed by Ericsson to support distributed, real time, fault tolerant and non-stop application.

It was developed in the late 1980's by Ericsson to program the next generation of telecom applications. First it was used to program ATM Switches. It was released as open source in 1998 with the Open Telecom Platform (Erlang language, libraries and a real-time distributed database (Mnesia)) which I am going talk about it in details later.

Erlang is named after A. K. Erlang (Agner Krarup Erlang was a Danish mathematician, statistician and engineer, who invented the fields of traffic engineering and queuing theory). It is sometimes thought that its name is an abbreviation of Ericsson Language.

Erlang as a framework has a lot features:

Concurrent: has a process-based model of concurrency with asynchronous message passing

Real-time: intended for programming real-time systems where response times in the order of milliseconds are required

Continuous operation: has primitives which allow code to be replaced in a running system and allow old and new versions of code to execute at the same time

Robust: Safety is a crucial requirement in systems such as the above. There are three constructs in the language for detecting run-time errors.

Memory management: a symbolic programming language with a real-time garbage collector

Distribution: has no shared memory. All interaction between processes is by asynchronous message passing

Erlang is a functional programming language. It is different from C++, C#, and Java, which are Procedural Programming Languages.

Procedural languages: stress on the organization of data, and sequences of instructions which operate on some global state

Functional languages: treat programs as evaluations of functions without a global state

To have a good idea about the language I added simple examples to check:
  • Example 1: Hello World

This is the usual hello world program that displays "Hello World!" to the screen.

-module(hello).

-export([hello/0]).

hello() ->io:fwrite("Hello, World!~n", []).

  • Example 2: Factorial

This a simple factorial example.

-module(fact).

-export([fac/1]).

fac(0) -> 1;

fac(N) when N > 0 -> N * fac(N-1).


To be continued ...