December 13, 2012 Erlang, Languages No comments
December 13, 2012 Erlang, Languages No comments
The one I didn’t get. But don’t worry, I can “kill” myself and “spawn” another me to try once again.
Not sure if I have to repeat some of the definitions given to this language, because I doubt that my readers read only this blog and never heard about Erlang. But who would start searching for “Erlang” if you are already here, so to avoid this dilemma I just copied one of its definitions later on.
Erlang is great and hard language at the same time. Its syntax easily looks somewhat alien to many developers, not saying about normal people, who are capable of reading only few human languages. On the other hand ideas behind Erlang are just astonishing. “Let it crash” concept works, because there is no shared state between processes, and if something crashes you just restart stuff, not worrying about overhead as processes are lightweight. And how does this sound to you: dynamic language in which you can change code on the fly? I don’t think you can allow for downtime when you are in a rocket. Not sure if NASA uses Erlang, but many telecom companies use it for sure, which resulted in birth of Open Telecom Platform (OTP) – a library for telecom applications.
I’ve found following explanation very sensible:
Erlang was developed at Ericsson and was designed from the ground up for writing scalable, fault-tolerant, distributed, non-stop, soft-realtime applications. Everything in the language, runtime and libraries reflects that purpose, which makes Erlang the best platform for developing this kind of software.
Use Erlang if you want your application to:
- handle very large number of concurrent activities
- be easily distributable over a network of computers
- be fault-tolerant to both software & hardware errors
- scale with the number of machines on the network
- be upgradable & reconfigurable without having to stop & restart
- be responsive to users within certain strict timeframes
- stay in continuous operation for many years
Please visit http://veldstra.org/whyerlang/ for more “why Erlang?”.
This or some similar question often is very interesting when its about some exotic or small language. Hard to answer, but I’ve found this really nice site: http://roberto-aloi.com/languagesintheworld/#. Try it out to figure out who is coding Erlang/Haskell/Scala/… in your country.
In my Haskell post I wrote “I’m too new to Haskell to talk about some advanced features and to write programs which are over 10 lines of code.” I can ensure that now I can write Erlang programs which are more than 10 lines of code, but I won’t be able to confidently talk about most of aspects of this language either.
I can write larger programs, because I wrote one. Pitfall here is that I don’t completely understand how it works. I believe this is due to the fact, that I’ve chosen wrong problem to solve with this language. Erland is not really build for stuff like “run this for 10 seconds”, but rather for stuff like “that’s your message, deal with it”. Though, I could easily be wrong.
I decided that my first Erlang program will be “sleeping barber” problem, which I’ve already done in Clojure (BTW, there is small bug, spot it!). Once again:
Problem called "sleeping barber." was created by Edsger Dijkstra in 1965. It has these characteristics:
- A barber shop takes customers.
- Customers arrive at random intervals, from 10 to 30 milliseconds.
- The barber shop has three chairs in the waiting room.
- The barber shop has one barber and one barber chair.
- When the barber’s chair is empty, a customer sits in the chair, wakes up the barber, and gets a haircut.
- If the chairs are occupied, all new customers will turn away.
- Haircuts take 20 milliseconds.
- After a customer receives a haircut, he gets up and leaves.
Write a multithreaded program to determine how many haircuts a barber can give in 10 seconds.
Counter – does nothing smart except of counting number of customers with new haircut
Barber
-module(barber). -import(counter,[incr/1]). -export([loop/0, cuthair/3]). loop() -> receive {Pid, Counter}-> timer:sleep(20), Pid ! "haircut done!", counter:incr(Counter), loop() end. cuthair(To, Barber, Counter) -> % make barber work synchronously Barber ! { To, Counter }, receive BarberService -> BarberService end.
Barbershop
Crazy stuff. For example, take a look at how I implemented continuous generation of customers. It is tail recursion function, which takes 10000 as starting WorkTime and generates customer after 10-30ms of thread sleeping, then calls itself with WorkTime minus time needed to send customer to barbershop. It continues until function is out of WorkTime. Similar I do for barber_work_day.
-module(barbershop). -export([loop/3,customers/2,barber_work_day/4]). loop(FreeChairs, Barber, Counter) -> receive { customer } -> loop(FreeChairs-1, Barber, Counter); { barber } -> loop(FreeChairs+1, Barber, Counter); { free_chairs_count, To } -> To ! { chairs, FreeChairs }, loop(FreeChairs, Barber, Counter) end. barber_work_day(WorkTime, Barbershop, Barber, Counter) when WorkTime < 1 -> 1; barber_work_day(WorkTime, Barbershop, Barber, Counter) when WorkTime > 0 -> receive { start } -> self() ! { start }, X = 20, FreeChairs = get_free_chairs_count(Barbershop), case FreeChairs of _ when (FreeChairs < 3) -> timer:sleep(X), Barber ! { self(), Counter }, Barbershop ! {barber}, barber_work_day(WorkTime-20, Barbershop, Barber, Counter); _ -> barber_work_day(WorkTime, Barbershop, Barber, Counter) end end. get_free_chairs_count(Barbershop) -> Barbershop ! { free_chairs_count, self() }, receive { chairs, FreeChairs } -> FreeChairs end. customers(WorkTime, Barbershop) when WorkTime < 0 -> 1; customers(WorkTime, Barbershop) when WorkTime > 0 -> receive { start } -> self() ! { start }, X = random:uniform(21) + 9, timer:sleep(X), FreeChairs = get_free_chairs_count(Barbershop), case FreeChairs of _ when (FreeChairs > 0) -> Barbershop ! {customer}, customers(WorkTime-X, Barbershop); _ -> customers(WorkTime-X, Barbershop) end end.
Program
Here I just initialize random, create processes and kick-off generation of customers and barber’s work day.
{A1, A2, A3} = now(). random:seed(A1, A2, A3). Counter = spawn(fun() -> counter:loop(0) end). Barber = spawn(fun barber:loop/0). Barbershop = spawn(fun() -> barbershop:loop(3,Barber,Counter) end). Customers = spawn(fun() -> barbershop:customers(10000,Barbershop) end). BarberWorkDay = spawn(fun() -> barbershop:barber_work_day(10000,Barbershop,Barber,Counter) end). BarberWorkDay ! {start}. Customers ! {start}. counter:get_count(Counter).
Un-obviously it executes slightly longer than 10 seconds (12-15), so when I call counter:get_count(Counter) exactly after 10 seconds I’m getting slightly lower numbers than expected, so I just call get_count in console until number stops growing with some very realistic number, like 434.
Now you can blame me for my dumbness, because it is really a lot of code for quite simple problem and it took me good 6+ hours to implement.
Links:
I really hope you liked this post! If true, instead of blaming me, you are welcome to share your own experience with Erlang.