CONCURRENT PROGRAMMING USING ERLANG

Download Report

Transcript CONCURRENT PROGRAMMING USING ERLANG

CONCURRENT PROGRAMMING
USING ERLANG
SHASHANK KADAVERU
The History

1995 PC Week Study of software projects:
◦ 16% successful
◦ 53% operational (but less than successful)
◦ 31% cancelled

Butler Group 1997 on large software projects
◦ 5 out of 6 large projects fail
◦ In >$300M companies, 9 out of 10 large projects fail

How to approach this?
◦ Use high-level modeling tools & generate code?
◦ Raise the level of programming language?
◦ We should Fight all causes for project failure!
ERLANG
Erlang is a programming language which
was designed for programming
concurrent, real-time, distributed faulttolerant systems.
 Developed By Ericsson and hence the
name.
 ‘ER’icsson ‘LANG’uage.

Applications of ERLANG
Telecom industry
 Location based mobile services
 Electronic payment systems
 Messaging and Interactive Voice Response
services
 Data acquisition and real-time monitoring

The Telecom Industry
The Telecom industry consists of:
 Switches, routers,
 base-stations
 Networks
 Mobile telephones
What they Need..
Massive concurrency
Soft real-time
Distribution
Interaction with hardware
Very large software systems
Complex functionality
Continuous operation for many years
Software maintenance without stopping the
system
 Stringent quality and reliability requirements
 Fault tolerance errors








How ERLANG Prog started
Concurrent systems
programming languages
like Ada, Modula or Chill
Functional programming
languages like ML or
Miranda
Concurrent functional
programming language
Erlang
A Simple Example of ERLANG
-module(math2). -export([double/1]).
double(X) ->
times(X, 2).
times(X, N) ->
X * N.
The function double/12 can be evaluated from outside
the module, whereas
times/2 is purely local, for example:
> math2:double(10).
20
> math2:times(5, 2).
** undefined function: math2:times(5,2) **
Another Example
Pattern Matching
-module(math3).
-export([area/1]).
area({square, Side}) ->
Side * Side;
area({rectangle, X,Y}) ->
X * Y;
area({circle, Radius}) ->
3.14159 * Radius * Radius;
area({triangle, A, B, C}) ->
S = (A + B + C)/2,
math:sqrt(S*(S-A)*(S-B)*(S-C)).
Creating a new process using spawn
Syntax:
Pid = spawn(Module, FunctionName, ArgumentList)
Example Program:
-module(ex3).
-export([activity/3]).
activity(Name,Pos,Size) ->
…………
Pid = spawn(ex3,activity,[Joe,75,1024])
activity(Joe,75,1024)
Concurrency in ERLANG
ringing_a(A, B) ->
receive
{A, on_hook} ->
back_to_idle(A, B);
{B, answered} ->
A ! {stop_tone, ring},
switch ! {connect, A, B},
conversation_a(A, B)
after 30000 ->
back_to_idle(A, B)
end.
back_to_idle(A, B) ->
A ! {stop_tone, ring},
B ! terminate,
idle(A).
Selective receive
Asynchronous send
Optional timeout
Cooperating processes may be
linked together
using
spawn_link(…,…,…)
or
link(Pid)
Layering in ERLANG
Robust systems can be built by layering
“Supervisors”
“Workers”
Connectivity
Ports can be used to link to External processes.
These ports will behave like Erlang processes.
Port
Port ! {PidC, {command, Data}}
Port ! {PidC, {connect, Pid1}}
Port ! {PidC, close}
External
process
ERLANG v/s OTHER PROG LANG’S
Applications written
in Erlang
Applications
written in C,
C++ or Java
OTP Components
Standard Libraries
Erlang Run-Time System
Hardware and Operating System
Using Erlang in Complex Systems
◦ Fits very well with the incremental design
method
◦ High programmer satisfaction
◦ Outstanding support for robustness and
concurrency
◦ Easier to add/change single components
◦ Small directed teams can achieve impressive
results
Productivity Estimates
◦ Similar line/hour programmer productivity
◦ 4-10 fewer lines of source code (compared to
C/C++, Java)
 4-10x higher programmer productivity
◦ Similar number of faults per 1000 lines of
source code
 4-10x higher quality
Efficient Program Development
Requirements
Ideas
Prototyping
Productification
• Interaction with the real
environment
• Powerful and
appropriate abstraction
mechanisms
• Efficient implementation
• Useful tools
A Simple Erlang-XML Document
XML
Erlang
<?xml version=“1.0”?>
{‘home.page’, [{title, “My Home Page”}],
<home.page title=“My Home Page”>
[{title, “Welcome to My Home Page”},
<title>
{text,
Welcome to My Home Page
[{para,
</title>
“Sorry, this home page is still under ”
<text>
“construction. Please come back soon!”}
<para>
]}
Sorry, this home page is still under
]}.
construction. Please come back soon!
</para>
</text>
</home.page>
EARLANG HIGHLIGHTS

Concurrency
◦ Either transparent or explicit concurrency
◦ Light-weight processes
◦ Highly scalable

Declarative
◦ Functional programming language
◦ High abstraction level
◦ Pattern matching
EARLANG HIGHLIGHTS

Soft Real-time
◦ Response times in the order of milliseconds
◦ Per-process garbage collection

Robustness
◦ Simple and consistent error recovery
◦ Supervision hierarchies
◦ "Program for the correct case"
EARLANG HIGHLIGHTS

Distribution
◦ Explicit or transparent distribution
◦ Network-aware runtime system

Hot Code loading
◦ Easily change code in a running system
◦ Enables non-stop operation
◦ Simplifies testing

External Interfacing
◦ "Ports" to the outside world behave as Erlang processes
EARLANG HIGHLIGHTS

Portability
◦ Erlang runs on any UNIX, Windows, ...
◦ Supports heterogeneous networks
THE RESEARCH CONTINUES
 HiPE - High Performance Erlang (Uppsala, Astec)
 ETOS - Erlang to Scheme (Montréal)
 Erlang Verification (SICS, Astec)
 Type System (Glasgow)
 “Safe” Erlang (Canberra)
 Specification Techniques (Aachen)
 Erlang Processor (Ericsson CADLab)
 ...
References
http://www.ibm.com/developerworks/ope
nsource/library/os-erlang1/
 http://www.erlang.org
 http://en.wikipedia.org/wiki/Erlang_(progr
amming_language)

Any Questions?
Thank You