[QUIZ][SOLUTION] Process Rings (#135)


James Koppel

Here's my solution. Its "processes" are just plain threads. It works pretty=
well, but it hangs on rings of size 1.=0A=0A$processes =3D []=0A=0Adef kil=
l_processes=0A $processes.each do |thr|=0A thr.exit!=0A end=0Aend=0A=
=0Athread_proc =3D proc do=0A Thread.stop=0A if Thread.current[:count] =
=3D=3D 0=0A Thread.current[:next] =3D $processes.first=0A else=0A Th=
read.current[:next] =3D Thread.new(&thread_proc)=0A Thread.current[:next=
][:count] =3D Thread.current[:count] - 1=0A $processes.push(Thread.curre=
nt[:next])=0A true until Thread.current[:next].stop?=0A Thread.curren=
t[:next].run=0A end=0A while true=0A Thread.stop=0A msg =3D Thread.=
current[:message]=0A cnt =3D Thread.current[:message_count]=0A=0A Thr=
ead.current[:message] =3D nil=0A Thread.current[:message_count] =3D nil=
=0A =0A if cnt =3D=3D 0=0A kill_processes=0A else=0A Thr=
ead.current[:next][:message_count] =3D cnt - 1=0A Thread.current[:next=
][:message] =3D msg=0A #On small rings, the message can circle around =
before the first thread has stopped=0A true until Thread.current[:next=
].stop?=0A Thread.current[:next].run=0A end=0A end=0Aend=0A=0Aproc=
esses, cycles =3D ARGV.map{|n| n.chomp.to_i}=0A=0A$processes.push(Thread.ne=
w(&thread_proc))=0Atrue until $processes.first.stop?=0A$processes.first[:co=
unt] =3D processes - 1=0A$processes.first.run=0A=0Aputs "Creating #{process=
es} processes..."=0Asleep(0.1) until $processes.length =3D=3D processes=0A=
=0Aputs "Timer started."=0Astart_time =3D Time.new=0A=0Aputs "Sending a mes=
sage around the ring #{cycles} times..."=0A$processes.first[:message_count]=
=3D processes * cycles=0A$processes.first[:message] =3D "Good day!"=0A$pro=
cesses.first.run=0A=0Asleep(0.1) while $processes.first.alive?=0A=0Aputs "D=
one."=0Aputs "Time in seconds: " + (Time.new.to_i - start_time.to_i).to_s=
=0A=0A----- Original Message ----=0AFrom: Ruby Quiz <[email protected]=
net>=0ATo: ruby-talk ML <[email protected]>=0ASent: Friday, August 17=
, 2007 8:15:37 AM=0ASubject: [QUIZ] Process Rings (#135)=0A=0AThe three rul=
es of Ruby Quiz:=0A=0A1. Please do not post any solutions or spoiler discu=
ssion for this quiz until=0A48 hours have passed from the time on this mess=
age.=0A=0A2. Support Ruby Quiz by submitting ideas as often as you can:=0A=
=0Ahttp://www.rubyquiz.com/=0A=0A3. Enjoy!=0A=0ASuggestion: A [QUIZ] in t=
he subject of emails about the problem helps everyone=0Aon Ruby Talk follow=
the discussion. Please reply to the original quiz message,=0Aif you can.=
-=3D-=3D-=3D-=3D=0A=0AI recently wrote about a challenge in the Programming=
Erlang book on my blog:=0A=0A http://blog.grayproductions.net/articles/=
2007/08/13/erlang-message-passing=0A=0ALanguage comparison issues aside, ju=
st figuring out how to build a ring of=0A"processes" was quite the brain be=
nder for me. That always makes for good Ruby=0AQuiz material, in my opinio=
n.=0A=0AThe task is straight forward:=0A=0A 1. Your program should take =
two command-line arguments: a number of=0A processes and a number of=
cycles.=0A 2. Begin by creating the requested number of processes, in a=
ring.=0A For example, when three processes are requested, process on=
e=0A creates and sends messages to process two, which creates and sen=
ds=0A messages to process three. The third process then sends its=0A=
messages back to process one.=0A 3. Pass a message around your ri=
ng of processes a number of times=0A equal to the requested cycles. =
Print timing results for how=0A long this takes.=0A=0AThe message you=
pass doesn't much matter. A simple String is fine. You may=0Aalso wish t=
o pass a counter with it, to verify the correct number of sends.=0A=0AI'll =
leave the definition of "processes" intentionally vague. Ruby doesn't have=
=0Aan equivalent to Erlang processes so we will just say that each process =
should=0Arepresent a node where we could run some instructions concurrently=
Be=0Acreative.=0A=0A=0A=0A=0A=0A=0A =0A___________________________=
_________________________________________________________=0ASick sense of h=
umor? Visit Yahoo! TV's =0AComedy with an Edge to see what's on, when. =0Ah=


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question