Tutorials Point, posted on 27th Nov, 2017

Erlang Anonymous Functions

-module(helloworld). 
-export([start/0]). 

start() -> 
   Fn = fun() -> 
      io:fwrite("Anonymous Function") end, 
   Fn().

Tutorials Point, posted on 27th Nov, 2017

Erlang Maximum Number of Processes Concurrency

-module(helloworld). 
-export([max/1,start/0]). 

max(N) -> 
   Max = erlang:system_info(process_limit), 
   io:format("Maximum allowed processes:~p~n" ,[Max]), 
   
   statistics(runtime), 
   statistics(wall_clock), 
   
   L = for(1, N, fun() -> spawn(fun() -> wait() end) end), 
   {_, Time1} = statistics(runtime), 
   {_, Time2} = statistics(wall_clock), lists:foreach(fun(Pid) -> Pid ! die end, L), 
   
   U1 = Time1 * 1000 / N, 
   U2 = Time2 * 1000 / N, 
   io:format("Process spawn time=~p (~p) microseconds~n" , [U1, U2]).
   wait() -> 
   
   receive 
      die -> void 
   end. 
 
for(N, N, F) -> [F()]; 
for(I, N, F) -> [F()|for(I+1, N, F)]. 

start()->
   max(1000), 
   max(100000).

Tutorials Point, posted on 27th Nov, 2017

Erlang Receive_end Concurrency

-module(helloworld). 
-export([loop/0,start/0]). 

loop() ->
   receive 
      {rectangle, Width, Ht} -> 
         io:fwrite("Area of rectangle is ~p~n" ,[Width * Ht]), 
         loop(); 
      {circle, R} ->
      io:fwrite("Area of circle is ~p~n" , [3.14159 * R * R]), 
      loop(); 
   Other ->
      io:fwrite("Unknown"), 
      loop() 
   end. 

start() ->
   Pid = spawn(fun() -> loop() end), 
   Pid ! {rectangle, 6, 10}.

Tutorials Point, posted on 27th Nov, 2017

Erlang Pid ! Message Concurrency

-module(helloworld). 
-export([start/0]). 
start() -> 
   Pid = spawn(fun() -> server("Hello") end), 
   Pid ! {hello}. 

server(Message) ->
   io:fwrite("~p",[Message]).

Tutorials Point, posted on 27th Nov, 2017

Erlang Concurrency

-module(helloworld). 
-export([start/0]). 

start() ->
   spawn(fun() -> server("Hello") end). 

server(Message) ->
   io:fwrite("~p",[Message]).

Tutorials Point, posted on 27th Nov, 2017

Erlang spawnlink Distributed Programming

-module(helloworld). 
-export([start/0]). 

start() -> 
   spawn_link(node(),fun() -> server("Hello") end). 

server(Message) ->
   io:fwrite("~p",[Message]).

Tutorials Point, posted on 27th Nov, 2017

Erlang is_alive Distributed Programming

-module(helloworld). 
-export([start/0]). 

start() ->
   io:fwrite("~p",[is_alive()]).

Tutorials Point, posted on 27th Nov, 2017

Erlang spawn on Node

-module(helloworld). 
-export([start/0]). 

start() ->
   spawn(node(),fun() -> server("Hello") end). 

server(Message) -> 
   io:fwrite("~p",[Message]).

Tutorials Point, posted on 27th Nov, 2017

Erlang Node Distributed Programming

-module(helloworld). 
-export([start/0]). 

start() ->
   io:fwrite("~p",[node()]).

Tutorials Point, posted on 27th Nov, 2017

Erlang spawn Distributed Programming

-module(helloworld). 
-export([start/0]). 

start() ->
   spawn(fun() -> server("Hello") end). 

server(Message) ->
   io:fwrite("~p",[Message]).

1 2 3 4 5 6 7 ... 17 Next
Advertisements
Loading...