CONCURRENT PROGRAMMING USING ERLANG SHASHANK KADAVERU The History







![A Simple Example of ERLANG -module(math 2). -export([double/1]). double(X) -> times(X, 2). times(X, N) A Simple Example of ERLANG -module(math 2). -export([double/1]). double(X) -> times(X, 2). times(X, N)](https://slidetodoc.com/presentation_image_h2/9f95a9e46cdbb122d4c9d851ebfc4d3c/image-8.jpg)
![Another Example Pattern Matching -module(math 3). -export([area/1]). area({square, Side}) -> Side * Side; area({rectangle, Another Example Pattern Matching -module(math 3). -export([area/1]). area({square, Side}) -> Side * Side; area({rectangle,](https://slidetodoc.com/presentation_image_h2/9f95a9e46cdbb122d4c9d851ebfc4d3c/image-9.jpg)

















- Slides: 26

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 >$300 M 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 modulemath 2 exportdouble1 doubleX timesX 2 timesX N A Simple Example of ERLANG -module(math 2). -export([double/1]). double(X) -> times(X, 2). times(X, N)](https://slidetodoc.com/presentation_image_h2/9f95a9e46cdbb122d4c9d851ebfc4d3c/image-8.jpg)
A Simple Example of ERLANG -module(math 2). -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: > math 2: double(10). 20 > math 2: times(5, 2). ** undefined function: math 2: times(5, 2) **
![Another Example Pattern Matching modulemath 3 exportarea1 areasquare Side Side Side arearectangle Another Example Pattern Matching -module(math 3). -export([area/1]). area({square, Side}) -> Side * Side; area({rectangle,](https://slidetodoc.com/presentation_image_h2/9f95a9e46cdbb122d4c9d851ebfc4d3c/image-9.jpg)
Another Example Pattern Matching -module(math 3). -export([area/1]). area({square, Side}) -> Side * Side; area({rectangle, X, Y}) -> X * Y; area({circle, Radius}) -> 3. 14159 * 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, Function. Name, Argument. List) Example Program: -module(ex 3). -export([activity/3]). activity(Name, Pos, Size) -> ………… Pid = spawn(ex 3, 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 ! {Pid. C, {command, Data}} Port ! {Pid. C, {connect, Pid 1}} Port ! {Pid. C, 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 -10 x higher programmer productivity ◦ Similar number of faults per 1000 lines of source code � 4 -10 x higher quality

Efficient Program Development Requirements Ideas Prototyping • Interaction with the real environment • Powerful and appropriate abstraction mechanisms • Efficient implementation Productification • 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 <para> soon!”} 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 l Hi. PE - High Performance Erlang (Uppsala, Astec) l ETOS - Erlang to Scheme (Montréal) l Erlang Verification (SICS, Astec) l Type System (Glasgow) l “Safe” Erlang (Canberra) l Specification Techniques (Aachen) l Erlang Processor (Ericsson CADLab) l. . .

References �http: //www. ibm. com/developerworks/o pensource/library/os-erlang 1/ �http: //www. erlang. org �http: //en. wikipedia. org/wiki/Erlang_(pr ogramming_language)

Any Questions? Thank You