From: Tom Danielsen Date: 2007-08-20T08:49:41+09:00 Subject: Re: [QUIZ] Process Rings (#135) Ok.... this is my first attempt at using callcc in ruby. The code is not what I would call beautiful, but should be readable...(??). Suggestions are most welcome! #!/usr/bin/env ruby # vim:et:ts=4:sw=4 $n = 0 if $DEBUG class RLWP def initalize @nxt = nil end attr_accessor :nxt def makecont passesleft, message = callcc { |cont| return cont } if passesleft <= 0 puts $n if $DEBUG exit 0 end $n += 1 if $DEBUG @nxt.call(passesleft - 1, message) end end def run(n, cycles, msg) process = Array.new(n) { RLWP.new } cont = process.collect { |p| p.makecont } process.each_with_index { |p,i| p.nxt = cont[(i+1) % n] } cont[0].call(n * cycles, msg) end run ARGV[0].to_i, ARGV[1].to_i, "xyzzy" tom@molly:~/src/quiz-135% time ruby -d quiz-135-callcc.rb 1000 1000 1000000 ruby -d quiz-135-callcc.rb 1000 1000 10.781 user 0.037 system 99% cpu 10.868 total tom@molly:~/src/quiz-135% At somewhere around 12000 processes ruby started crashing on my system, maybe a memory issue? tom@molly:~/src/quiz-135% time ruby quiz-135-callcc.rb 12000 3 [2] 30579 illegal hardware instruction (core dumped) ruby quiz-135-callcc.rb 12000 3 ruby quiz-135-callcc.rb 12000 3 1.695 user 0.713 system 32% cpu 7.418 total tom@molly:~/src/quiz-135% regards, -tom