During this activity, students should be able to:
This activity helps the student develop the following skills, values and attitudes: ability to analyze and synthesize, capacity for identifying and solving problems, and efficient use of computer systems.
Individually or in pairs, solve the following set of concurrent programming exercises using Erlang. Place all your functions in a module called procs
.
The function factorial
takes as its input an integer number N. This function does the following:
Process P, on the other hand, does the following:
For example:
> procs:factorial(5). 120
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
:
recent
: Report the value of the most recently computed Fibonacci number.
span
: Report how many Fibonacci numbers have been computed so far.
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 output may vary):
> 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
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> received message 1/5. <0.34.0> received message 1/5. <0.33.0> received message 2/5. <0.34.0> received message 2/5. <0.33.0> received message 3/5. <0.34.0> received message 3/5. <0.33.0> received message 4/5. <0.34.0> received message 4/5. <0.33.0> received message 5/5. <0.33.0> finished <0.34.0> received message 5/5. <0.34.0> finished
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> received 1/4 from <0.31.0> <0.34.0> received 1/4 from <0.33.0> <0.35.0> received 1/4 from <0.34.0> <0.33.0> received 2/4 from <0.35.0> <0.34.0> received 2/4 from <0.33.0> <0.35.0> received 2/4 from <0.34.0> <0.33.0> received 3/4 from <0.35.0> <0.34.0> received 3/4 from <0.33.0> <0.35.0> received 3/4 from <0.34.0> <0.33.0> received 4/4 from <0.35.0> <0.33.0> finished <0.34.0> received 4/4 from <0.33.0> <0.34.0> finished <0.35.0> received 4/4 from <0.34.0> <0.35.0> finished
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.33.0> received 0/4 from <0.31.0> <0.34.0> received 1/4 from <0.33.0> <0.33.0> received 1/4 from <0.34.0> <0.35.0> received 1/4 from <0.33.0> <0.33.0> received 1/4 from <0.35.0> <0.36.0> received 1/4 from <0.33.0> <0.33.0> received 1/4 from <0.36.0> <0.34.0> received 2/4 from <0.33.0> <0.33.0> received 2/4 from <0.34.0> <0.35.0> received 2/4 from <0.33.0> <0.33.0> received 2/4 from <0.35.0> <0.36.0> received 2/4 from <0.33.0> <0.33.0> received 2/4 from <0.36.0> <0.34.0> received 3/4 from <0.33.0> <0.33.0> received 3/4 from <0.34.0> <0.35.0> received 3/4 from <0.33.0> <0.33.0> received 3/4 from <0.35.0> <0.36.0> received 3/4 from <0.33.0> <0.33.0> received 3/4 from <0.36.0> <0.34.0> received 4/4 from <0.33.0> <0.34.0> finished <0.33.0> received 4/4 from <0.34.0> <0.35.0> received 4/4 from <0.33.0> <0.35.0> finished <0.33.0> received 4/4 from <0.35.0> <0.36.0> received 4/4 from <0.33.0> <0.36.0> finished <0.33.0> received 4/4 from <0.36.0> <0.33.0> finished
You don't need to hand in a report for this activity.
Deliver a single file called procs.erl
containing your solutions. Please provide the following information:
IMPORTANT: The program source file must include at the top the author's personal information (name and student id) within comments. For example:
% ITESM CEM, March 21, 2017. % Erlang Source File % Activity: Práctica #6: Erlang concurrente % Authors: % A01166611 Pepper Pots % A01160611 Anthony Stark . . (The rest of the program goes here) .
Due date is Tuesday, March 21.
This activity will be evaluated using the following criteria:
-10 | The program doesn't contain within comments the author's personal information. |
---|---|
10 | The program contains syntax errors. |
DA | The program was plagiarized. |
10-100 | Depending on the amount of exercises that were solved correctly. |