An Erlang primer. Johan Montelius

Similar documents
Starting the System & Basic Erlang Exercises

A Tutorial on using Code::Blocks with Catalina 3.0.3

Linux File System and Basic Commands

Erlang functional programming in a concurrent world

Erlang functional programming in a concurrent world Johan Montelius and Vladimir Vlassov

An Introduction to Erlang

Introduction to Erlang. Franck Petit / Sebastien Tixeuil

CS390 Principles of Concurrency and Parallelism. Lecture Notes for Lecture #5 2/2/2012. Author: Jared Hall

Namy: a distributed name server. Johan Montelius. October 2, 2016

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

Goldy: a distributed game. Johan Montelius. October 2, 2016

Processes. Johan Montelius KTH

CS59-F16 Make-up Midterm

A process. the stack

Distributed Erlang and Map-Reduce

Erlang - functional programming in a concurrent world. Johan Montelius KTH

Rudy: a small web server. Johan Montelius. October 2, 2016

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

CSC111 Computer Science II

COS 318: Operating Systems. File Systems. Topics. Evolved Data Center Storage Hierarchy. Traditional Data Center Storage Hierarchy

Intermediate Programming, Spring Misha Kazhdan

Some Linux (Unix) Commands that might help you in ENSC351

Erlang: concurrent programming. Johan Montelius. October 2, 2016

B a s h s c r i p t i n g

The Command Shell. Fundamentals of Computer Science

Storage and File System

Processes in linux. What s s a process? process? A dynamically executing instance of a program. David Morgan. David Morgan

15-122: Principles of Imperative Computation

My malloc: mylloc and mhysa. Johan Montelius HT2016

Web Mechanisms. Draft: 2/23/13 6:54 PM 2013 Christopher Vickery

TNM093 Practical Data Visualization and Virtual Reality Laboratory Platform

Unix Tutorial Haverford Astronomy 2014/2015

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

A heap, a stack, a bottle and a rack. Johan Montelius HT2017

Using echo command in shell script

Erlang: An Overview. Part 2 Concurrency and Distribution. Thanks to Richard Carlsson for most of the slides in this part

CS 3 Introduction to Software Engineering. 3: Exceptions

Documentation for LISP in BASIC

Instructions. First, download the file

CS 360 Programming Languages Interpreters

Tcl/Tk for XSPECT a Michael Flynn

Intro. Scheme Basics. scm> 5 5. scm>

Exploring the file system. Johan Montelius HT2016

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

Once you have installed MobaXterm, open MobaXterm. Go to Sessions -> New Session, and click on the SSH icon.

Creating a Shell or Command Interperter Program CSCI411 Lab

Unit 10. Linux Operating System

Saint Louis University. Intro to Linux and C. CSCI 2400/ ECE 3217: Computer Architecture. Instructors: David Ferry

An Introduction to Erlang. Richard Carlsson

Chris Simpkins (Georgia Tech) CS 2316 Data Manipulation for Engineers Python Overview 1 / 9

CSCI 201 Lab 1 Environment Setup

Instructions PLEASE READ (notice bold and underlined phrases)

377 Student Guide to C++

Using the Zoo Workstations

Taking the Derivative

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 18: Naming, Directories, and File Caching

Getting your department account

Nano-Lisp The Tutorial Handbook

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 18: Naming, Directories, and File Caching

A LISP Interpreter in ML

Preparing and Running C Programs for CS 136 (W08)

CSE341: Programming Languages Using SML and Emacs Winter 2018

There are two tools with which you should be familiar: the program su, and more importantly, sudo. Here we see how to use them.

1 Getting Started with Linux.

let s implement a memory Asynchronous vs Synchronous the functional way a first try

Eclipse Environment Setup

Unit A451: Computer systems and programming. Section 3: Software 1 Intro to software

If you don t have the JDK, you will need to install it. 1. Go to

Review of Fundamentals

Tiny Instruction Manual for the Undergraduate Mathematics Unix Laboratory

(MCQZ-CS604 Operating Systems)

CPSC 150 Laboratory Manual. Lab 1 Introduction to Program Creation

Message-passing concurrency in Erlang

Overview of the UNIX File System. Navigating and Viewing Directories

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Table Of Contents. 1. Zoo Information a. Logging in b. Transferring files 2. Unix Basics 3. Homework Commands

Reviewing gcc, make, gdb, and Linux Editors 1

ECS 153 Discussion Section. April 6, 2015

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall 2008.

Basic Survival UNIX.

Refactoring Erlang with Wrangler. Huiqing Li Simon Thompson School of Computing University of Kent

15 Unification and Embedded Languages in Lisp

Unix tutorial. Thanks to Michael Wood-Vasey (UPitt) and Beth Willman (Haverford) for providing Unix tutorials on which this is based.

Getting Started with Papyrus for RealTime v0.9

Systems Programming/ C and UNIX

Makefile Tutorial. Eric S. Missimer. December 6, 2013

ag.el Documentation Release 0.45 Wilfred Hughes

Overview of the UNIX File System

CPS221 Lecture: Operating System Protection

Chordy: a distributed hash table. Johan Montelius and Vladimir Vlassov. August 29, 2016

QUIZ. What is wrong with this code that uses default arguments?

1 Installation (briefly)

CMSC 331 Final Exam Fall 2013

Incident Response Tools

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

C Shell Tutorial. Section 1

Lec 1 add-on: Linux Intro

What is concurrency? Concurrency. What is parallelism? concurrency vs parallelism. Concurrency: (the illusion of) happening at the same time.

CS140 Operating Systems Final December 12, 2007 OPEN BOOK, OPEN NOTES

Transcription:

An Erlang primer Johan Montelius Introduction This is not a crash course in Erlang since there are plenty of tutorials available on the web. I will however describe the tools that you need so that you can get a programming environment up and running. I will take for granted that you know some programming languages, have heard of functional programing and that recursion is not a mystery to you. 1 Getting started The first thing you need to do is download the Erlang SDK. This is found at www.erlang.org and is available both as a Windows binary and as source that you can make with the usual tools available on a Unix or even MacOs platform. 1.1 Emacs You also need an text editor and what is better than Emacs. Download it from www.gnu.org/software/emacs/, available both for Unix, Mac and Windows. You need to add the code below to your.emacs file (provided that you have Erlang installed under C:/Program Files/). This will make sure that the Erlang mode is loaded as soon as you open a.erl file and that you can start an Erlang shell under Emacs etc. Change the <Ver> and <ToolsVer> to what is right in your system. On a Linux box it will look similar but the install directory is something like /usr/local/lib/erlang/. Note! If you cut and past this text the -character will not be a - character, if you see what I mean. When you load the file in emacs you will have errors. If you do cut and paste then write in by hand erlang-start. (setq load-path (cons "C:/Program Files/erl<Ver>/lib/tools-<ToolsVer>/emacs" load-path)) (setq erlang-root-dir "C:/Program Files/erl<Ver>") (setq exec-path (cons "C:/Program Files/erl<Ver>/bin" exec-path)) 1

(require erlang-start) You will have to find your emacs home directory where the file should be placed. If you re on the KTH student computers then the home area is h: but you then need to set the HOME environment variable so that emacs finds its way. If everything works you should be able to start an Erlang shell inside Emacs by M-x run-erlang (M-x is < escape > followed by x) 1.2 Eclipse If you prefer to use Eclipse you can install a Erlang plugin and do your development inside Eclipse. Fell free to use what ever environment you want but if you consider using Wordpad you should start thinking about why you re here at all. 2 Hello World Open a file hello.erl and write the following: -module(hello). -export([world/0]). world()-> "Hello world!". Now open a Erlang shell, compile and load the file with the command c(hello). and, call the function hello:world().. Remember to end commands in th shell with a dot. If things work out you have successfully written, compiled and executed your first Erlang program. Find the Erlang documentation and read the Getting started section. 3 Concurrent Programming Erlang was designed for concurrent programming. You will quickly learn how to divide your program into communicating processes and thereby give it far better structure. Try the following: -module(wait). -export([hello/0]). hello() -> receive 2

X -> io:format("aaa! surprise, a message: ~s~n", [X]) The io:format procedure will output the string to the stdout and replace the control characters (characters preceded by a tilde) with the elements in the list. The s means that the next element in the list should be a string, n simply outputs a newline. Load the above module and execute the command: P = spawn(wait, hello, []). The variable P is now bound to the process identifier of spawned process. The process was created and called the procedure hello/0. It is now suspended waiting for incoming messages. In the same Erlang shell execute the command: P! "hello". This will send a message, in this case a string hello, to the process that now wakes up and continues the execution. To make life easier one often register the process identifiers under names that can be access by all processes. If two processes should communicate they must know the process identifier. Either a process is given the identifier to the other process when it is created, in a message or, through the registered name of the process. Try this in a shell (first type f() to make the shell forget the previous binding to P): P = spawn(wait, hello, []). Now register the process identifier under the name foo. register(foo, P). And then send the process a message. foo! "hello". In this example the only thing we sent was a string but we can send arbitrary complex data structures. The receive statement can have several clauses that try to match incoming messages. Only if a match is found will a clause be used. Try this: -module(tic). -export([first/0]). first() -> 3

receive {tic, X} -> io:format("tic: ~w~n", [X]), second() second() -> receive {tac, X} -> io:format("tac: ~w~n", [X]), last(); {toe, X} -> io:format("toe: ~w~n", [X]), last() last() -> receive X -> io:format("end: ~w~n", [X]) Then in a shell execute the following commands: P = spawn(tic, first, []). P! {toe, bar}. P! {tac, gurka}. P! {tic, foo}. In what order where they received by the process. Note how messages are queued and how the process selects in what order to process them. 4 Distributed Programming Distributed programming is extremely easy in Erlang, the only problem we will have is finding the name of our node. The Erlang distributed systems normally work using domain names rather than explicit IP addresses. This could be a problem since we re working with a set of laptops that are not 4

regularly given names in the DNS. There is a work around for this that we will use. Connect to the WLAN, login and make sure that you have a access to the Internet. Run ipconfig or ifconfig to find out what IP address you have been allocated. You will use this IP address explicitly when starting an Erlang node. 4.1 node name When you start Erlang you can make it network aware by providing a name. You also would like to give it a secret cookie. Any node that can prove to have knowledge of the cookie will be trusted to do just about anything. This is of course quite dangerous and it s very easy for a malicious node to close a whole network down. Start a new Erlang shell with the following command, replacing the IP address to whatever you have. erl -name foo@130.237.250.69 -setcookie secret In the Erlang shell you can now find the name of your node with the bif node(). It should look something like foo@130.237.250.69. Doing the same if you re running Erlang under Emacs is slightly more tricky. You have to set a lisp variable that is used when Emacs starts Erlang. Type M-x eval-expression and evaluate the function. (set inferior-erlang-machine-options ("-name" "foo@130.237.250.69" "-setcookie" "secret")) You can also set the environment variable ERL FLAGS to the same string or include it in your.emacs file. This will however prevent you from running multiple Erlang shells on the same node. You would also have to change this every time you get a new IP address. Start a second Erlang shells now using the name bar on the same or another machine. Load and start the suspending hello process that we defined before on the foo-node and register it under the name wait. Now on the bar-node try the following: {wait, foo@130.237.250.69 }! "a message from bar". Note how interprocess communication in Erlang is handled in the same way regardless if the process is executing in the same shell, another shell or on another host. The only thing that has to be changes is how the Erlang shell is started and how to access external registered processes. 5

4.2 ping-pong If we could only send messages to registered processes it would not be a transparent system. We can send messages to any process but the problem is of course to get hold of the process identifier. There is no way to write this down and you can not use the cryptic 0.70.0 that the Erlang shell uses when it prints the value of a process identifier. The only processes that know the process identifier of a process is the creator of the process and the process itself. The process it self can find out by the built-in procedure self(). Now we are of course allowed to pass the process identifiers around and even send them in a message so we can let other processes know. Once a process knows it can use the identifier and does not have to know if it is a local or remote process. Try to define a process on one machine ping, that sends a message to a registered process on another machine with its own process identifier in the message. The process should the wait for a reply. The receiver on the other machine should look at the message and use the process identifier to send a reply. A world of warning, the send primitive! will accept both registered names, remote names and process identifiers. This can sometime cause a problem. If you implement a concurrent system that is not distributed and have a registered processes under the name foo, you could pass the atom foo around and anyone could treat it as a process identifier. Now if we distribute this application a remote process will not be able to use it as a process identifier since the registration process is local to a node. So keep track of what you pass around, is it a process identifier (that can be used remotely) or is it a name under which a process is registered. 6