SlideShare a Scribd company logo
A quick introduction to
OTP Applications
using a
Gen_server example
on Erlang/OTP R15B
Baed on the official online manuals and a gen_server example from http://www.rustyrazorblade.
com/2009/04/erlang-understanding-gen_server/
files and source code
0 directories, 5 files
├── demo.script
├── demo_application.app
├── demo_application.erl
├── demo_supervisor.erl
└── demo_gen_server.erl
%% -*- erlang -*-
main(_Strings)->
% compile all the .erl files to .beam files
make:all(),
% start demo_application
application:start(demo_application),
% run demo_gen_server:add/1
Output = demo_gen_server:add(10),
io:format(
"nnInitial demo_gen_server state was 0;
adding 10; returned: ~pnn",
[Output]
),
halt().
demo.script
{ application,
demo_application,
[ { mod,
{ demo_application,
[]
}
},
{ applications,
[ kernel,
stdlib
]
}
]
}.
demo_application.app
-module(demo_application).
-behaviour(application).
-export([start/2, stop/1]).
-compile(native).
start(_Type,_Args) ->
demo_supervisor:start_link().
stop(State) ->
{ok,State}.
demo_application.erl
-module(demo_supervisor).
-behaviour(supervisor).
-export([start_link/0, init/1]).
-compile(native).
start_link() ->
supervisor:start_link(
{ local,
demo_supervisor_instance1
},
demo_supervisor,
[]
).
init(_Args) ->
{ ok,
{ { one_for_one,
1,
60
},
[ { demo_gen_server_child_id,
{ demo_gen_server,
start_link,
[]
},
permanent,
brutal_kill,
worker,
[ demo_gen_server ]
}
]
}
}.
demo_supervisor.erl
-module(demo_gen_server).
-export( [ start_link/0,
add/1,
subtract/1,
init/1,
handle_call/3
]
).
-compile(native).
start_link()->
gen_server:start_link(
{ local,
demo_gen_server_instance2
},
demo_gen_server,
[],
[]
).
init(_Args) -> {ok, 0}.
add(Num) ->
gen_server:call(
demo_gen_server_instance1,
{add, Num}
).
subtract(Num) ->
gen_server:call(
demo_gen_server_instance1,
{subtract, Num}
).
handle_call({add, Num}, _From, State) ->
{reply, State + Num, State + Num};
handle_call({subtract, Num}, _From, State) ->
{reply, State - Num, State - Num}.
demo_gen_server.erl
a quick demo
0 directories, 5 files
├── demo.script
├── demo_application.app
├── demo_application.erl
├── demo_supervisor.erl
└── demo_gen_server.erl
PROMPT: escript demo.script
Recompile: demo_application
Recompile: demo_gen_server
Recompile: demo_supervisor
Initial demo_gen_server state was 0;
adding 10; returned: 10
PROMPT:
PROMPT: erl
Erlang R15B (erts-5.9) [source] [64-bit]
[smp:2:2] [async-threads:0] [hipe]
[kernel-poll:false]
Eshell V5.9 (abort with ^G)
1> make:all().
Recompile: demo_application
Recompile: demo_gen_server
Recompile: demo_supervisor
up_to_date
2> application:start(demo_application).
ok
3> demo_gen_server:add(10).
10
4> q().
ok
5>
PROMPT:
Method 2:
start the Erlang shell,
then manually run the commands from demo.
script.
Method 1:
just run
demo.script.
Current
Working
Directory
tear down
0 directories, 5 files
├── demo.script
├── demo_application.app
├── demo_application.erl
├── demo_supervisor.erl
└── demo_gen_server.erl
make:all().
This searches the current working directory, compiles any .erl files to
.beam files if the latter do not already exist, and recompiles any .erl
files which have changed since their existing .beam files were
compiled.
application:start(demo_application).
This reads demo_application.app, which is an application configuration
file, then attempts to compile, load, and start the application.
The internal events are expanded on the next slide.
demo_gen_server:add(10).
The "callback" module demo_gen_server implements the stock OTP
"behaviour" module gen_server, and is now instantiated as a process in
a supervision tree.
The internal events are expanded on the next slide.
ancestor system
processes
(try running
observer:start()
to view)
demo_supervisor
demo_
gen_server
overview
0 directories, 5 files
├── demo.script
├── demo_application.app
├── demo_application.erl
├── demo_supervisor.erl
└── demo_gen_server.erl
loading & starting
(1) calling: (behaviour module:function) application:start(demo_application)
(2) (1) calls: behaviour module:function) application:load(demo_application)
(3) (2) reads: (configuration file) demo_application.app
(4) (3) points to: (callback module) demo_application
(5) (1) calls: (callback module:function) demo_application:start/2
(6) (5) calls: (callback module:function) demo_supervisor:start_link/0
(7) (6) calls: (behaviour module:function) supervisor:start_link/2,3
(8) (7) calls: (callback module:function) demo_supervisor:init/1
(9) (8) points to: (callback module:function) demo_gen_server:start_link/0
(10) (7) calls: (callback module:function) demo_gen_server:start_link/0
(11) (10) calls: (behaviour module:function) gen_server:start_link/3,4
(12) (11) calls: (callback module:function) demo_gen_server:init/1
... and the application is started!
The process is probably even more complicated, further under the hood, but this should suffice for
introductions.
0 directories, 5 files
├── demo.script
├── demo_application.app
├── demo_application.erl
├── demo_supervisor.erl
└── demo_gen_server.erl
using/calling/doing
(1) calling: (callback module:function) demo_gen_server:add(10)
(2) (1) calls: (behaviour module:function) gen_server:call/2,3
(3) (2) calls: (callback module:function) demo_gen_server:handle_call/3
... then work is done, and a result is returned!

More Related Content

PDF
Using OTP and gen_server Effectively
PDF
Asynchronous programming in .net 4.5 with c#
PPSX
Async-await best practices in 10 minutes
PPTX
Closures
PPT
Asynchronous in dot net4
PPTX
Oop object oriented programing topics
ODP
New Stuff In Php 5.3
PPTX
How Functions Work
Using OTP and gen_server Effectively
Asynchronous programming in .net 4.5 with c#
Async-await best practices in 10 minutes
Closures
Asynchronous in dot net4
Oop object oriented programing topics
New Stuff In Php 5.3
How Functions Work

What's hot (20)

PDF
Introducing Elixir and OTP at the Erlang BASH
PDF
Test your code like a pro - PHPUnit in practice
PPTX
Python Programming Essentials - M27 - Logging module
PPT
Unit Testing using PHPUnit
ODP
Software Testing
PDF
Static Analysis of PHP Code – IPC Berlin 2016
PPT
Beyond Unit Testing
PPT
Cell processor lab
PDF
What is the Joomla Framework and why do we need it?
PPTX
PHPUnit: from zero to hero
PPT
Test Driven Development with PHPUnit
PPT
Eff Plsql
PDF
2021.laravelconf.tw.slides2
PDF
BEAMing With Joy
PDF
Elixir and OTP
PDF
Python-nose: A unittest-based testing framework for Python that makes writing...
PPT
Exploiting stack overflow 101
ODP
Pyunit
Introducing Elixir and OTP at the Erlang BASH
Test your code like a pro - PHPUnit in practice
Python Programming Essentials - M27 - Logging module
Unit Testing using PHPUnit
Software Testing
Static Analysis of PHP Code – IPC Berlin 2016
Beyond Unit Testing
Cell processor lab
What is the Joomla Framework and why do we need it?
PHPUnit: from zero to hero
Test Driven Development with PHPUnit
Eff Plsql
2021.laravelconf.tw.slides2
BEAMing With Joy
Elixir and OTP
Python-nose: A unittest-based testing framework for Python that makes writing...
Exploiting stack overflow 101
Pyunit
Ad

Viewers also liked (20)

PPTX
Finite State Machines - Why the fear?
DOCX
References
DOCX
2016 práctica calameo william
PDF
Computer science
DOCX
Unit 2: Booklet (First Draft)
PPTX
תמונות מתוך ההרצאות
DOC
Research Paper Zaahl 2014 corrections
PDF
EL FÚTBOL ARGENTINO.Gustavo Albano Abreu.ISBN:9789871775323
PPTX
PERUBAHAN FISIOLOGI MASA NIFAS
PPTX
Nuevas Tecnologías, debes de realizar:
PPTX
งานนำเสนอ
PPT
Resultados del cuestionario proyecto afrocolombianidad dirigido a estudiantes
PPTX
Actividad 7
PPT
Anh co hien bai boc
PDF
15 16
PPT
Ed6620 edmodo presentation
PDF
Unitat 05 geografia_2a_part
DOCX
production diary
PPT
Презентація:Матеріали до уроків
PPTX
Food and health
Finite State Machines - Why the fear?
References
2016 práctica calameo william
Computer science
Unit 2: Booklet (First Draft)
תמונות מתוך ההרצאות
Research Paper Zaahl 2014 corrections
EL FÚTBOL ARGENTINO.Gustavo Albano Abreu.ISBN:9789871775323
PERUBAHAN FISIOLOGI MASA NIFAS
Nuevas Tecnologías, debes de realizar:
งานนำเสนอ
Resultados del cuestionario proyecto afrocolombianidad dirigido a estudiantes
Actividad 7
Anh co hien bai boc
15 16
Ed6620 edmodo presentation
Unitat 05 geografia_2a_part
production diary
Презентація:Матеріали до уроків
Food and health
Ad

Similar to OTP application (with gen server child) - simple example (20)

PDF
Rupicon 2014 Action pack
PDF
Experimentos lab
PDF
OpenERP Technical Memento V0.7.3
PPT
Test Drive Development in Ruby On Rails
PDF
Session10-PHP Misconfiguration
PDF
Re-Design with Elixir/OTP
ODP
(2) c sharp introduction_basics_part_i
PPTX
Introduction to node.js
PDF
jBPM5 Community Training Module 4: jBPM5 APIs Overview + Hands On
PDF
Testing Legacy Rails Apps
PPT
Red5 - PHUG Workshops
PDF
Open Source RAD with OpenERP 7.0
PPT
PPTX
Symfony2 Introduction Presentation
PDF
Configuration Management and Transforming Legacy Applications in the Enterpri...
PDF
Dive into Play Framework
KEY
LvivPy - Flask in details
PPTX
Unit tests in_symfony
PDF
Android Patching & Client-Side CyberSecurity
PDF
Swift 2.0: Apple’s Advanced Programming Platform for Developers
Rupicon 2014 Action pack
Experimentos lab
OpenERP Technical Memento V0.7.3
Test Drive Development in Ruby On Rails
Session10-PHP Misconfiguration
Re-Design with Elixir/OTP
(2) c sharp introduction_basics_part_i
Introduction to node.js
jBPM5 Community Training Module 4: jBPM5 APIs Overview + Hands On
Testing Legacy Rails Apps
Red5 - PHUG Workshops
Open Source RAD with OpenERP 7.0
Symfony2 Introduction Presentation
Configuration Management and Transforming Legacy Applications in the Enterpri...
Dive into Play Framework
LvivPy - Flask in details
Unit tests in_symfony
Android Patching & Client-Side CyberSecurity
Swift 2.0: Apple’s Advanced Programming Platform for Developers

More from YangJerng Hwa (19)

PDF
Structuring Marketing Teams
PDF
Architecturing the software stack at a small business
PDF
Reactive datastore demo (2020 03-21)
PDF
JavaScript - Promises study notes- 2019-11-30
PDF
2019 10-09 google ads analysis - eyeballing without proper math
PDF
2019 malaysia car accident - total loss - diy third-party claim - simplifie...
PDF
2019 09 tech publishers in malaysia
PDF
2019 09 web components
PDF
Appendix - arc of development
PDF
A Docker Diagram
PDF
A Software Problem (and a maybe-solution)
PDF
Deep learning job pitch - personal bits
PDF
Monolithic docker pattern
PPTX
What people think about Philosophers & Management Consultants
PPTX
Process for Terminating Employees in Malaysia
PPTX
Pour-over Coffee with the EK43
PDF
ERTS diagram
PPTX
Intro to Stock Trading for Programmers
PDF
A Haphazard Petcha Kutcha
Structuring Marketing Teams
Architecturing the software stack at a small business
Reactive datastore demo (2020 03-21)
JavaScript - Promises study notes- 2019-11-30
2019 10-09 google ads analysis - eyeballing without proper math
2019 malaysia car accident - total loss - diy third-party claim - simplifie...
2019 09 tech publishers in malaysia
2019 09 web components
Appendix - arc of development
A Docker Diagram
A Software Problem (and a maybe-solution)
Deep learning job pitch - personal bits
Monolithic docker pattern
What people think about Philosophers & Management Consultants
Process for Terminating Employees in Malaysia
Pour-over Coffee with the EK43
ERTS diagram
Intro to Stock Trading for Programmers
A Haphazard Petcha Kutcha

Recently uploaded (20)

PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Empathic Computing: Creating Shared Understanding
PDF
Machine learning based COVID-19 study performance prediction
PPTX
A Presentation on Artificial Intelligence
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Big Data Technologies - Introduction.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
cuic standard and advanced reporting.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Unlocking AI with Model Context Protocol (MCP)
Encapsulation_ Review paper, used for researhc scholars
Review of recent advances in non-invasive hemoglobin estimation
20250228 LYD VKU AI Blended-Learning.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Empathic Computing: Creating Shared Understanding
Machine learning based COVID-19 study performance prediction
A Presentation on Artificial Intelligence
Network Security Unit 5.pdf for BCA BBA.
Per capita expenditure prediction using model stacking based on satellite ima...
Big Data Technologies - Introduction.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
cuic standard and advanced reporting.pdf
Digital-Transformation-Roadmap-for-Companies.pptx

OTP application (with gen server child) - simple example

  • 1. A quick introduction to OTP Applications using a Gen_server example on Erlang/OTP R15B Baed on the official online manuals and a gen_server example from http://www.rustyrazorblade. com/2009/04/erlang-understanding-gen_server/
  • 3. 0 directories, 5 files ├── demo.script ├── demo_application.app ├── demo_application.erl ├── demo_supervisor.erl └── demo_gen_server.erl %% -*- erlang -*- main(_Strings)-> % compile all the .erl files to .beam files make:all(), % start demo_application application:start(demo_application), % run demo_gen_server:add/1 Output = demo_gen_server:add(10), io:format( "nnInitial demo_gen_server state was 0; adding 10; returned: ~pnn", [Output] ), halt(). demo.script { application, demo_application, [ { mod, { demo_application, [] } }, { applications, [ kernel, stdlib ] } ] }. demo_application.app -module(demo_application). -behaviour(application). -export([start/2, stop/1]). -compile(native). start(_Type,_Args) -> demo_supervisor:start_link(). stop(State) -> {ok,State}. demo_application.erl -module(demo_supervisor). -behaviour(supervisor). -export([start_link/0, init/1]). -compile(native). start_link() -> supervisor:start_link( { local, demo_supervisor_instance1 }, demo_supervisor, [] ). init(_Args) -> { ok, { { one_for_one, 1, 60 }, [ { demo_gen_server_child_id, { demo_gen_server, start_link, [] }, permanent, brutal_kill, worker, [ demo_gen_server ] } ] } }. demo_supervisor.erl -module(demo_gen_server). -export( [ start_link/0, add/1, subtract/1, init/1, handle_call/3 ] ). -compile(native). start_link()-> gen_server:start_link( { local, demo_gen_server_instance2 }, demo_gen_server, [], [] ). init(_Args) -> {ok, 0}. add(Num) -> gen_server:call( demo_gen_server_instance1, {add, Num} ). subtract(Num) -> gen_server:call( demo_gen_server_instance1, {subtract, Num} ). handle_call({add, Num}, _From, State) -> {reply, State + Num, State + Num}; handle_call({subtract, Num}, _From, State) -> {reply, State - Num, State - Num}. demo_gen_server.erl
  • 5. 0 directories, 5 files ├── demo.script ├── demo_application.app ├── demo_application.erl ├── demo_supervisor.erl └── demo_gen_server.erl PROMPT: escript demo.script Recompile: demo_application Recompile: demo_gen_server Recompile: demo_supervisor Initial demo_gen_server state was 0; adding 10; returned: 10 PROMPT: PROMPT: erl Erlang R15B (erts-5.9) [source] [64-bit] [smp:2:2] [async-threads:0] [hipe] [kernel-poll:false] Eshell V5.9 (abort with ^G) 1> make:all(). Recompile: demo_application Recompile: demo_gen_server Recompile: demo_supervisor up_to_date 2> application:start(demo_application). ok 3> demo_gen_server:add(10). 10 4> q(). ok 5> PROMPT: Method 2: start the Erlang shell, then manually run the commands from demo. script. Method 1: just run demo.script. Current Working Directory
  • 7. 0 directories, 5 files ├── demo.script ├── demo_application.app ├── demo_application.erl ├── demo_supervisor.erl └── demo_gen_server.erl make:all(). This searches the current working directory, compiles any .erl files to .beam files if the latter do not already exist, and recompiles any .erl files which have changed since their existing .beam files were compiled. application:start(demo_application). This reads demo_application.app, which is an application configuration file, then attempts to compile, load, and start the application. The internal events are expanded on the next slide. demo_gen_server:add(10). The "callback" module demo_gen_server implements the stock OTP "behaviour" module gen_server, and is now instantiated as a process in a supervision tree. The internal events are expanded on the next slide. ancestor system processes (try running observer:start() to view) demo_supervisor demo_ gen_server overview
  • 8. 0 directories, 5 files ├── demo.script ├── demo_application.app ├── demo_application.erl ├── demo_supervisor.erl └── demo_gen_server.erl loading & starting (1) calling: (behaviour module:function) application:start(demo_application) (2) (1) calls: behaviour module:function) application:load(demo_application) (3) (2) reads: (configuration file) demo_application.app (4) (3) points to: (callback module) demo_application (5) (1) calls: (callback module:function) demo_application:start/2 (6) (5) calls: (callback module:function) demo_supervisor:start_link/0 (7) (6) calls: (behaviour module:function) supervisor:start_link/2,3 (8) (7) calls: (callback module:function) demo_supervisor:init/1 (9) (8) points to: (callback module:function) demo_gen_server:start_link/0 (10) (7) calls: (callback module:function) demo_gen_server:start_link/0 (11) (10) calls: (behaviour module:function) gen_server:start_link/3,4 (12) (11) calls: (callback module:function) demo_gen_server:init/1 ... and the application is started! The process is probably even more complicated, further under the hood, but this should suffice for introductions.
  • 9. 0 directories, 5 files ├── demo.script ├── demo_application.app ├── demo_application.erl ├── demo_supervisor.erl └── demo_gen_server.erl using/calling/doing (1) calling: (callback module:function) demo_gen_server:add(10) (2) (1) calls: (behaviour module:function) gen_server:call/2,3 (3) (2) calls: (callback module:function) demo_gen_server:handle_call/3 ... then work is done, and a result is returned!