<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 11 (filtered)">
<style>
<!--
/* Font Definitions */
@font-face
{font-family:Consolas;
panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:10.0pt;
font-family:Arial;}
a:link, span.MsoHyperlink
{color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{color:purple;
text-decoration:underline;}
p.MsoPlainText, li.MsoPlainText, div.MsoPlainText
{margin:0in;
margin-bottom:.0001pt;
font-size:10.0pt;
font-family:"Courier New";}
span.Typed
{font-family:"Courier New";}
span.tty
{font-family:"Courier New";}
span.Name
{font-style:italic;}
span.Variable
{font-family:"Times New Roman";
font-style:italic;}
@page Section1
{size:8.5in 11.0in;
margin:1.0in 77.95pt 1.0in 77.95pt;}
div.Section1
{page:Section1;}
-->
</style>
</head>
<body lang=EN-US link=blue vlink=purple>
<div class=Section1>
<p class=MsoPlainText><span style='font-family:Arial'>Jay Nelson wrote:</span></p>
<p class=MsoPlainText><span style='font-family:Arial'>> So here is the
concrete challenge:</span></p>
<p class=MsoPlainText><span style='font-family:Arial'>> </span></p>
<p class=MsoPlainText><span style='font-family:Arial'>> 1) Send a message to
a linked process:</span></p>
<p class=MsoPlainText><span style='font-family:Arial'>> a)
{request, Msg}</span></p>
<p class=MsoPlainText><span style='font-family:Arial'>> 2) The other process
should generate 20 or so messages randomly</span></p>
<p class=MsoPlainText><span style='font-family:Arial'>> and
then quit</span></p>
<p class=MsoPlainText><span style='font-family:Arial'>> 3) Always handle
messages in the following order:</span></p>
<p class=MsoPlainText><span style='font-family:Arial'>> a)
5 messages of {reply, high, Msg}</span></p>
<p class=MsoPlainText><span style='font-family:Arial'>> b)
3 messages of {reply, normal, Msg}</span></p>
<p class=MsoPlainText><span style='font-family:Arial'>> c)
1 message of {reply, low, Msg}</span></p>
<p class=MsoPlainText><span style='font-family:Arial'>> d)
'EXIT' message</span></p>
<p class=MsoPlainText><span style='font-family:Arial'>> </span></p>
<p class=MsoPlainText><span style='font-family:Arial'>> Of course, there may
not be 5 remaining high messages or 3 remaining</span></p>
<p class=MsoPlainText><span style='font-family:Arial'>> normal messages so
you need to deal with these end cases. In doing so,</span></p>
<p class=MsoPlainText><span style='font-family:Arial'>> make sure you don't
just end up polling every 100 milliseconds. I've</span></p>
<p class=MsoPlainText><span style='font-family:Arial'>> simplified the
problem by only having one process that can generate</span></p>
<p class=MsoPlainText><span style='font-family:Arial'>> replies, so that the
selective receive doesn't become even more</span></p>
<p class=MsoPlainText><span style='font-family:Arial'>> complicated.</span></p>
<p class=MsoPlainText><span style='font-family:Arial'>> </span></p>
<p class=MsoPlainText><span style='font-family:Arial'>> I challenge any
beginner or intermediate erlang programmer to write</span></p>
<p class=MsoPlainText><span style='font-family:Arial'>> and post the code
for both processes and a description of your results.</span></p>
<p class=MsoPlainText><span style='font-family:Arial'>> All the advanced
erlang people can help point out the issues that will</span></p>
<p class=MsoPlainText><span style='font-family:Arial'>> have to be addressed
when you change the initial code approach.</span></p>
<p class=MsoPlainText><span style='font-family:Arial'>> </span></p>
<p class=MsoPlainText><span style='font-family:Arial'>> At least, that is my
bet. Valentin may be right. Let's see some code!</span></p>
<p class=MsoPlainText><span style='font-family:Arial;color:black'> </span></p>
<p class=MsoPlainText><span style='font-family:Arial;color:black'>OK, let me
begin with the output from my program so you can see if I got the answer
functionally correct:</span></p>
<p class=MsoPlainText><span style='font-family:Arial;color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>2> nelson:start("Message text").</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Sending message 20 with high priority.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Sending message 19 with high priority.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Sending message 18 with normal priority.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Sending message 17 with normal priority.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Sending message 16 with high priority.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Sending message 15 with high priority.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Sending message 14 with high priority.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Sending message 13 with high priority.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Sending message 12 with low priority.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Sending message 11 with normal priority.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Sending message 10 with normal priority.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Sending message 9 with low priority.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Sending message 8 with high priority.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Sending message 7 with low priority.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Sending message 6 with normal priority.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Sending message 5 with low priority.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Sending message 4 with low priority.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Sending message 3 with normal priority.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Sending message 2 with normal priority.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Sending message 1 with high priority.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Exit (normal) signal received from sender; begin prioritized
receive sequence.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Received {"Message text",high,20}.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Received {"Message text",high,19}.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Received {"Message text",high,16}.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Received {"Message text",high,15}.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Received {"Message text",high,14}.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Received {"Message text",normal,18}.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Received {"Message text",normal,17}.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Received {"Message text",normal,11}.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Received {"Message text",low,12}.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Received {"Message text",high,13}.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Received {"Message text",high,8}.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Received {"Message text",high,1}.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Received {"Message text",normal,10}.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Received {"Message text",normal,6}.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Received {"Message text",normal,3}.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Received {"Message text",low,9}.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Received {"Message text",normal,2}.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Received {"Message text",low,7}.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Received {"Message text",low,5}.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>Received {"Message text",low,4}.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>complete</span></p>
<p class=MsoPlainText><span style='font-family:Arial;color:black'> </span></p>
<p class=MsoPlainText><span style='font-family:Arial;color:black'>It appears to
me to do as requested. It receives 5 high priority messages before
receiving 3 normal priority messages and then a single low priority message.
When there are no more of any particular type, it skips it and moves to the
next. Here’s my code:</span></p>
<p class=MsoPlainText><span style='font-family:Arial;color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% @author David Mercer <dmercer@alum.mit.edu></span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% @doc Programming challenge by Jay Nelson, 6/1/08.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%%</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% Jay Nelson wrote:</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% <pre></span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% > So here is the concrete challenge:</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% > </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% > 1) Send a message to a linked process:</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% > a) {request, Msg}</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% > 2) The other process should generate 20 or so messages
randomly</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% > and then quit</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% > 3) Always handle messages in the following order:</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% > a) 5 messages of {reply,
high, Msg}</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% > b) 3 messages of {reply,
normal, Msg}</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% > c) 1 message of {reply,
low, Msg}</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% > d) 'EXIT' message</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% > </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% > Of course, there may not be 5 remaining high messages or
3 remaining</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% > normal messages so you need to deal with these end
cases. In doing so,</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% > make sure you don't just end up polling every 100
milliseconds. I've</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% > simplified the problem by only having one process that
can generate</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% > replies, so that the selective receive doesn't become
even more</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% > complicated.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% > </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% > I challenge any beginner or intermediate erlang
programmer to write</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% > and post the code for both processes and a
description of your results.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% > All the advanced erlang people can help point out the
issues that will</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% > have to be addressed when you change the initial code
approach.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%% </pre></span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>-module(nelson).</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%% API</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>-export([start/1, start/3]).</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%% @spec start(Msg::any()) -> complete</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%% @doc Execute the Nelson challenge.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%% Spawns a linked process, waits for the process to exit, then
receives the</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%% messages in accordance with the priority rules laid out by
Nelson.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%% The Msg argument is simply passed through to the spawned
process, which</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%% will return it in the messages it sends back.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%% @equiv start(Msg, 20, {5, 3, 1})</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>start(Msg) -> start(Msg, 20, {5, 3, 1}).</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%% @spec start(Msg::any(), N::integer(),
PriorityRules::PriorityRules) -> complete</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%% PriorityRules = {High::integer(),
Med::integer(), Low::integer()}</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%% @doc Execute the Nelson challenge.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%% Spawns a linked process, waits for the process to exit, then
receives the</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%% messages in accordance with the priority rules laid out by
Nelson.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%% The Msg argument is simply passed through to the spawned
process, which</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%% will return it in the messages it sends back.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%% N is the number of messages for the spawned process to send.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%%</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%% The PriorityRules specify the numbers of messages of each
priority which</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%% are received in each cycle. Nelson specified that these
numbers be 5, 3,</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%% and 1 respectively.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>start(Msg, N, {High, Med, Low}) -></span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> % Create linked process for
sending prioritized messages</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> Self = self(),</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> process_flag(trap_exit,
true),</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> Pid = spawn_link(fun() ->
start_sending(Self, Msg, N) end),</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> % Send the initial message</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> Pid ! {request, Msg},</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> % Wait for the linked process
to exit</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> receive</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> {'EXIT',
Pid, Reason} -></span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> %
Generally we expect Reason = normal, but we don't care if it's something else</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> io:format("Exit
(~p) signal received from sender; begin prioritized receive sequence.\n",
[Reason]),</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> %
Once we receive the exit message, begin receiving the messages</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> loop({0,
High}, {0, Med}, {0, Low})</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> end.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>start_sending(Pid, Msg, N) -></span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> % Seed the random number
generator</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> {A1, A2, A3} = now(),</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> random:seed(A1, A2, A3),</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> % Send</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> send(Pid, Msg, N).</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>send(_Pid, _Msg, 0) -> complete;</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>send(Pid, Msg, N) when N > 0 -></span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> Priority = case
random:uniform(3) of</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> 1
-> high;</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> 2
-> normal;</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> 3
-> low</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> end,</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> io:format("Sending
message ~p with ~p priority.\n", [N, Priority]),</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> Pid ! {reply, Priority, {Msg,
Priority, N}},</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> send(Pid, Msg, N - 1).</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%% loop/3 is the prioritized receive loop. It works by
keeping track of</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%% counters for the three priorities, counting up each one in
priority</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%% order until it reaches the max. If no message of that
priority is</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>%% received, then the max is set to 0. When all counters are
zero, all messages have been received.</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>loop({0, 0}, {0, 0}, {0, 0}) -> complete;</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>loop({H, H}, {M, M}, {L, L}) -> loop({0, H}, {0, M}, {0, L});</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>loop({H1, H2}, {0, M}, {0, L}) when H2 > H1 -></span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> receive</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> {reply,
high, Msg} -></span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> io:format("Received
~p.\n", [Msg]),</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> loop({H1
+ 1, H2}, {0, M}, {0, L})</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> after
0 -></span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> loop({0,
0}, {0, M}, {0, L})</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> end;</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>loop({H, H}, {M1, M2}, {0, L}) when M2 > M1 -></span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> receive</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> {reply,
normal, Msg} -></span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> io:format("Received
~p.\n", [Msg]),</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> loop({H,
H}, {M1 + 1, M2}, {0, L})</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> after
0 -></span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> loop({H,
H}, {0, 0}, {0, L})</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> end;</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> </span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'>loop({H, H}, {M, M}, {L1, L2}) when L2 > L1 -></span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> receive</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> {reply,
low, Msg} -></span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> io:format("Received
~p.\n", [Msg]),</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> loop({H,
H}, {M, M}, {L1 + 1, L2})</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> after
0 -></span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> loop({H,
H}, {M, M}, {0, 0})</span></p>
<p class=MsoPlainText style='margin-left:.5in'><span style='font-family:Consolas;
color:black'> end.</span></p>
<p class=MsoPlainText><span style='font-family:Arial;color:black'> </span></p>
<p class=MsoPlainText><span style='font-family:Arial;color:black'>Please
advise. Thank-you.</span></p>
</div>
</body>
</html>