What's all this fuss about Erlang

Source: Internet
Author: User

Reprinted: http://blog.sina.com.cn/s/blog_49faf32901008pap.html

 

By Joe Armstrong
Original article: What's all this fuss about Erlang
Translator: Joshua Zhu Xu Shiwei)


What's all this fuss about Erlang?

No one can predict the future, but I plan to make some rational speculation.

Let's assume that intel is correct and the Keifer project is successful. If so, the 32-core processor will appear in the market in 2009/2010 years. It is not surprising that sun has produced the Niagara, which has 8 cores and each core runs 4 hyper-threading (which is equivalent to 32 cores ). This is an exciting development for Erlang programmers. For this reason, they have been waiting for 20 years. Now, it is time to get a return.

For Erlang programmers, the good news is:Your Erlang program runs n times faster on a n-core processor.

Is this true?

Almost. Although it is too early, we are still very optimistic (rather optimistic, I have never seen such optimism in the past 20 years !).

Sometimes we need to make minor adjustments to our program-when I generate an Erlang document on a sun Niagara machine (with 32 cores, I changed my program to a line (I changed a map to pmap-sorry, I will give you some technical details here. pmap is just a "parallel map" (parallel map) ). This program (which generates 63 documents based on the Wiki tag) is running seven times faster. Not 32 times. I admit this, but it has been significantly accelerated. (Subsequent work made us realize that we encountered an I/O bottleneck when writing a disk, so unless we can make the disk I/O Parallel, otherwise we will stay on this 7 times

At Ericsson, where I worked and Erlang was developed, we were Porting some applications to a 4-core processor.-Guess what? After some minor adjustments, they run almost 4 times faster. We can't wait for Intel's 80-core processor in the lab ......

Why is our program running faster? This is all related to mutable state and concurrency.

Variable status and concurrency

Looking back (more than 20 years ago), there are two concurrency models:

  • Shared state concurrency)
  • Message Passing concurrency)

Now, the whole world has taken a route (towards the sharing state), and we have chosen another one. Almost no other language can follow the "path to message transmission concurrency", with the exception of Oz and OCCAM. In the message transmission concurrency model, we claim that there is no sharing status. All calculations are completed in the process, and the only way to exchange data is through asynchronous message transmission.

So why is this beneficial?

The concurrency model of the shared state is dragged down by the idea of "variable State" (as the name suggests, it is the memory that can be modified. All languages such as C, Java, and C ++ think that there is a kind of thing called "Memory", and we can modify it. As long as you only have one process to modify the memory, it will be okay. But if you have multiple processes to share and modify the same memory, you will suffer endless problems-this is silly.

To prevent simultaneous modification to the shared memory, we need a lock mechanism. You can call it mutex, synchronised method, or whatever name you want. If the program crashes in the critical section (that is, when they hold the lock), the disaster will come. All other programs will be overwhelmed.

How can programmers fix these problems? They do not fix the issue. They only pray. On single-core processors, their programs may still work, but on multiple cores-disaster. There are a variety of solutions to this problem (transactional memory (transaction memories) may be the best ). However, in the best cases, they are just a hodgedge. In the worst case, they are nightmares.

Erlang does not have a variable data structure
(Although not accurate, it is also accurate enough)

No variable data structure = No lock
No variable data structure = easy parallelization

How can we implement parallel processing? It is easy for programmers to break down the problem solution into many parallel processes.

This programming style has its own terminology-it is called concurrency Oriented Programming concurrency programming. Erlang is not object-oriented-it has its own representation.

The target team stepped down and played concurrently.

The world is concurrent and parallel, and many things happen at the same time. If I don't intuitively understand the idea of concurrency, I can't drive on the road. We have been conducting pure message transmission concurrency. Imagine a group of people who are not in a shared state. I have my private memories (in my mind). You have yours, and they are not shared. We communicate through information transmission (sound waves and light waves. Based on this information, we update our private status.

In short, this is for concurrent programming.

In terms of hiding a mutable state in an object (Translator's note: This is one of the core concepts of object-oriented-encapsulation changes): This feature makes parallelism an almost impossible problem to solve.

Is it valid?

Yes. Erlang is widely used in high-tech projects that value reliability. Erlang's flagship project (founded by Ericsson, a Swedish telecommunications company) is axd301, which has over 2 million lines of Erlang code. Axd301 has achieved reliability of 9 to 9 (yes, you are reading correctly, 99.9999999% ). Let's put it in this context: the five 9 s have been regarded as excellent (downtime of 5.2 minutes/year), and the seven 9 s can hardly reach ......, But we have achieved 9.

Why? Because there is no shared state, and there is a subtle error recovery model. You can learn all the details in my doctoral thesis.

Who is using Erlang?

  • People who know the inside story
  • Start-ups
  • Ericsson
  • Wings, a 3D modeling program http://www.wings3d.com/
  • Ejabberd, An Instant Messaging Server (Jabber/XMPP)
  • Tsung, a multi-protocol Distributed Load Testing Tool
  • Yaws, a very high-performance Web Server
  • Thousands of fans ("I hope I can do this during work hours ")

Is Erlang hard?

No-but it is a little different. Erlang does not have a syntax similar to C to make it easy to learn. It is neither "Object-Oriented" nor "mutable ", it is a functional programming language )".

This is what makes people feel scared-and that makes new users discouraged. However, Erlang is actually a very small and simple language. Maybe you are confused about what Erlang code looks like. Erlang uses pattern matching syntax in large quantities. Here is a small example of Erlang code (from the new programming Erlang book ):

-Module (geometry ).
-Export ([area/1]).
Area ({rectangle, width, HT})-> width * HT;
Area ({square, x})-> X * X;
Area ({circle, r})-> 3.14159 * r.

Now let's compile and run it in Erlang shell:

1> C (geometry ).
{OK, geometry}
2> geometry: Area ({rectangle, 10, 5 }).
50
3> geometry: Area ({circle, 1.4 }).
6.15752

Quite simple ...... The following is the Java code for doing similar things:

Abstract class shape {
Abstract double area ();
}
Class circle extends shape {
Final double radius;
Circle (double radius) {This. radius = radius ;}
Double area () {return math. Pi * radius ;}
}
Class rectangle extends shape {
Final double HT;
Final double width;
Rectangle (double width, double height ){
This. ht = height;
This. width = width;
}
Double area () {return width * HT ;}
}
Class square extends shape {
Final double side;
Square (double side ){
This. Side = side;
}
Double area () {return side * side ;}
}

Where can I download Erlang?

You can download Erlang from erlang.org.

How can I further understand Erlang?

Oh, I just wrote the book programming Erlang: software for a concurrent world (Pragmatic bookshelf, US $36.95, 978-1-934356-00-5. This is an Erlang tutorial guide. It covers the entire language and contains many demo programs and their complete source code, including:

  • A system similar to IRC/chat
  • A Streaming Media SHOUTcast Server
  • A map-reduce implementation for building a full-text Retrieval System

Blog

The atmosphere is very active in the blog circle. You can visit the Google search engine to search for the Erlang blog. In addition, there are some articles you may like:

  • More Erlang
  • Concurrency is easy
  • Web 2.0 is shifting

Network Document
Now let's look at this directory. You will find more than 50 PDF files here. I have selected some of them for your reference:

  • Getting_started-5.5.3.pdf
  • Tutorial-5.5.3.pdf
  • Programming_examples-5.5.3.pdf
  • Design_principles-5.5.3.pdf
  • Efficiency_guide-5.5.3.pdf
  • Asn1-1.4.4.12.pdf
  • Dialyzer-1.5.1.pdf
  • Inets-4.7.8.pdf
  • Mnesia-4.3.3.pdf
  • Parsetools-1.4.1.pdf
  • Reference_manual-5.5.3.pdf
  • Sasl-2.1.4.pdf
  • Snmp-4.8.4.pdf

Each of them represents a book to be compiled.

Have a good time reading!

Copyright (c) 2007 Joe Armstrong

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.