Com S 541 Lecture -*- Outline -*- * The Erlang Language (5.7) ------------------------------------------ ERLANG (5.7) From Ericsson telecommunications - automatic memory management - hides internal representation of data - efficient threads - high-performance s/w fault tolerance - dynamically typed - first class functions - asynchronous message passing - used in Open Telecom Platform - updated while running ------------------------------------------ Q: What other language do we know that hides the representation of data? Oz ** computation model (5.7.1) ------------------------------------------ COMPUTATION MODEL program = process* processes = port + mailbox port = stream + recursive function pattern matching can - wait for messages - remove some from mailbox - leave others message passing asynchronous processes are independent no shared references between processes transparent distribution failure detection (linking), - sends message when another process fails replicated database ------------------------------------------ Q: What features are useful for reliability? independent processes, asynchronous messages, no shared refs DB compensates for the lack of mutable store ** Semantics of Erlang (5.7.2) We'll skip the syntax, but see the Erlang book ------------------------------------------ CONCURRENCY AND MESSAGE PASSING PRIMITIVES IN ERLANG PID = spawn(M,F,A) -- creates new process with id PID running module M's function F with argument list A Pid!Msg -- sends Msg to process with id Pid receive Pattern1 [when Guard1] -> Body1; ... PatternN [when GuardN] -> BodyN; [ after Expr -> BodyT; ] -- blocks until a message matches one of the Patterns (with true guard) removes it from mailbox, binds values to pattern variables, executes the corresponding body -- the after clause is optional, it gives a timeout, after which BodyT is executed ------------------------------------------ a timeout of 0 means after is executed if no messages match *** translation into Oz ------------------------------------------ TRANSLATION INTO OZ process ~~> thread + port send to process ~~> send to port mailbox ~~> receive ~~> function on 2 streams (I/O) ------------------------------------------ ... port's stream **** without timeout ------------------------------------------ TRANSLATION OF RECEIVE WITHOUT TIMEOUT T(B Sin Sout) ~~> B Sout=Sin where receive doesn't appear in B T(receive P1 -> B1; ... PN -> BN; Sin Sout) ~~> local fun {Loop S L#E Sout} case S of M|S1 then case M of T(P1) then E=S1 T(B1 L Sout) ... [] T(PN) then E=S1 T(BN L Sout) else E1 in E=M|E1 {Loop S1 T#E1 Sout} end end end L in {Loop Sin L#L Sout} end ------------------------------------------ Q: What is T? The translation function Q: What is L? Q: What is S1 above? not a mistake, it's the new tail of S (the input) Q: What is L#L doing? Q: How would you deal with the guards? **** with timeout Q: How would you handle a non-zero timeout? use an Alarm and WaitTwo to make a race between messaging and the alarm Q: How would you handle a zero timeout? use IsDet