 # Práctica #6: Erlang concurrente

### Objectives

During this activity, students should be able to:

• Write concurrent programs using the Erlang programming language.

This activity helps students develop the following skills, values and attitudes: ability to analyze and synthesize, capacity for identifying and solving problems, and efficient use of computer systems.

## Activity Description

Individually or in pairs, solve the following set of concurrent programming exercises using Erlang. Place all your functions in a module called procs.

1. The function factorial takes as its input an integer number $$N$$. This function does the following:

1. It creates a new process $$P$$.
2. It sends $$P$$ a message requesting $$N!$$
3. It waits until it receives the result from $$P$$. Once this happens, it returns the result.

Process $$P$$, on the other hand, does the following:

1. It waits until it receives a message requesting $$N!$$
2. It computes $$N!$$
3. It sends back to the original sender a message with the result of the computation.

For example:

> procs:factorial(5).
120

2. The function fibo_proc starts a new process that will compute in the background a new Fibonacci number every second. The first two numbers of the Fibonacci sequence are 0 and 1, all other elements are calculated adding the two previous numbers in the sequence. The function returns a process ID that can be used to send the following messages via a function called fibo_send:

1. recent: Report the value of the most recently computed Fibonacci number.
2. span: Report how many Fibonacci numbers have been computed so far.
3. Anything else sent as a message terminates the process execution and returns the atom killed.

After the process has been terminated, any attempt to send a message will only return the atom killed.

NOTE: The is_process_alive(Pid) function can be used to determine if a process is still alive or not.

The following examples demonstrate how to use the fibo_proc and fibo_send functions (these are time sensitive operations, thus the output will most likely be different):

> P = procs:fibo_proc().
<0.47.0>
> procs:fibo_send(P, recent).
8
> procs:fibo_send(P, span).
10
> procs:fibo_send(P, recent).
144
> procs:fibo_send(P, span).
14
> procs:fibo_send(P, whatever).
killed
> procs:fibo_send(P, recent).
killed
> procs:fibo_send(P, span).
killed

3. The function called double starts two processes and sends a message $$M$$ times forwards and backwards between them. After all the messages have been sent the processes should terminate gracefully. The output of the function should look something similar to the following ($$M=5$$):

> procs:double(5).
Created <0.33.0>
Created <0.34.0>
<0.33.0> finished
<0.34.0> finished

4. The function called ring starts $$N$$ processes, and sends a message $$M$$ times around all the processes in the ring. After all the messages have been sent the processes should terminate gracefully. The output of the function should look something similar to the following ($$N=3$$, $$M=4$$):

> procs:ring(3, 4).
Current process is <0.31.0>
Created <0.33.0>
Created <0.34.0>
Created <0.35.0>
<0.33.0> finished
<0.34.0> finished
<0.35.0> finished

5. The function called star starts $$N+1$$ processes in a star, and sends a message $$M$$ times forwards and backwards between the center process and the other processes. After all the messages have been sent the processes should terminate gracefully. The output of the function should look something similar to the following ($$N=3$$, $$M=4$$):

> procs:star(3, 4).
Current process is <0.31.0>
Created <0.33.0> (center)
Created <0.34.0>
Created <0.35.0>
Created <0.36.0>
<0.34.0> finished
<0.35.0> finished
<0.36.0> finished
<0.33.0> finished


## Deliverables

No report needs to be handed in.

The program source file must include at the top the authors’ personal information (name and student ID) within comments. For example:

%----------------------------------------------------------
% Práctica #6: Erlang concurrente
% Date: March 31, 2019.
% Authors:
%          A01166611 Pepper Pots
%          A01160611 Anthony Stark
%----------------------------------------------------------


### Instrucciones para subir archivo

Para entregar el archivo procs.erl, ingresa los siguientes datos:

Only one team member needs to upload the file.

Due date is Sunday, March 31.

## Evaluation

This activity will be evaluated using the following criteria:

 -10 The program doesn't contain within comments the author's personal information. The program contains syntax errors. The program was plagiarized in whole or in part. Depending on the amount of exercises that were solved correctly.