[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[QUIZ][SOLUTION] Process Rings (#135

James Koppel

8/20/2007 9:11:00 PM

Here's my solution. Its "processes" are just plain threads. It works pretty well, but it hangs on rings of size 1.$processes = []def kill_processes $processes.each do |thr| thr.exit! endendthread_proc = proc do Thread.stop if Thread.current[:count] == 0 Thread.current[:next] = $processes.first else Thread.current[:next] = Thread.new(&thread_proc) Thread.current[:next][:count] = Thread.current[:count] - 1 $processes.push(Thread.current[:next]) true until Thread.current[:next].stop? Thread.current[:next].run end while true Thread.stop msg = Thread.current[:message] cnt = Thread.current[:message_count] Thread.current[:message] = nil Thread.current[:message_count] = nil if cnt == 0 kill_processes else Thread.current[:next][:message_count] = cnt - 1 Thread.current[:next][:message] = msg #On small rings, the message can circle around before the first thread has stopped true until Thread.current[:next].stop? Thread.current[:next].run end endendprocesses, cycles = ARGV.map{|n| n.chomp.to_i}$processes.push(Thread.new(&thread_proc))true until $processes.first.stop?$processes.first[:count] = processes - 1$processes.first.runputs "Creating #{processes} processes..."sleep(0.1) until $processes.length == processesputs "Timer started."start_time = Time.newputs "Sending a message around the ring #{cycles} times..."$processes.first[:message_count] = processes * cycles$processes.first[:message] = "Good day!"$processes.first.runsleep(0.1) while $processes.first.alive?puts "Done."puts "Time in seconds: " + (Time.new.to_i - start_time.to_i).to_s----- Original Message ----From: Ruby Quiz <james@grayproductions.net>To: ruby-talk ML <ruby-talk@ruby-lang.org>Sent: Friday, August 17, 2007 8:15:37 AMSubject: [QUIZ] Process Rings (#135)The three rules of Ruby Quiz:1. Please do not post any solutions or spoiler discussion for this quiz until48 hours have passed from the time on this message.2. Support Ruby Quiz by submitting ideas as often as you can:http://www.ruby.... Enjoy!Suggestion: A [QUIZ] in the subject of emails about the problem helps everyoneon Ruby Talk follow the discussion. Please reply to the original quiz message,if you can.-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=I recently wrote about a challenge in the Programming Erlang book on my blog: http://blog.grayproductions.net/articles/2007/08/13/erlang-message-passi... comparison issues aside, just figuring out how to build a ring of"processes" was quite the brain bender for me. That always makes for good RubyQuiz material, in my opinion.The task is straight forward: 1. Your program should take two command-line arguments: a number of processes and a number of cycles. 2. Begin by creating the requested number of processes, in a ring. For example, when three processes are requested, process one creates and sends messages to process two, which creates and sends messages to process three. The third process then sends its messages back to process one. 3. Pass a message around your ring of processes a number of times equal to the requested cycles. Print timing results for how long this takes.The message you pass doesn't much matter. A simple String is fine. You mayalso wish to pass a counter with it, to verify the correct number of sends.I'll leave the definition of "processes" intentionally vague. Ruby doesn't havean equivalent to Erlang processes so we will just say that each process shouldrepresent a node where we could run some instructions concurrently Becreative. ____________________________________________________________________________________Sick sense of humor? Visit Yahoo! TV's Comedy with an Edge to see what's on, when. http://tv.yahoo.com/colle...