------ art_65614_24913224.1153220919747 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 7/18/06, Adam Shelly <adam.shelly / gmail.com> wrote: > > On 7/14/06, Ruby Quiz <james / grayproductions.net> wrote: > > 1) Devise an "interesting" definition for sleep which allows > negative > > durations. Alternately, use one of the definitions given > here. > > 2) Write some Ruby code demonstrating behavior which satisfies > that > > definition. As with the above example, you needn't provide a > drop-in > > replacement for Kernel.sleep. > > > > When I read this quiz, it reminded me of the time I was working on a > huge realtime 24/7 distributed unix system. Each machine had a time > daemon keeping it in synch with a central time server. During the > first live tests, my application would occasionally core-dump. When I > first saw the traces, It looked like either one of my queues was > getting corrupted, or the system time had jumped backward. The lead > of the services group assured me that it was absolutely impossible > that the daemon could set the clock back. So I spent 2 long days > inspecting core dumps and trace logs, going through my code line by > line, adding more logging, questioning my sanity. In the end, I > presented the lead with a stack of logs proving that the time daemon > was infact occasionally setting the clock backward by one second. > > That gave me this idea for how to implement negative sleep: > ----- > # time_demon.rb > # (only tested on windows) > > module Time_Demon > if RUBY_PLATFORM =~ /(win|w)32$/ > TimeStrFormat = "time %H:%M:%S" > else > TimeStrFormat = "date -s %H:%M:%S" > end Dangerous !!!! Just do not do this, you have to be root to do it anyway, but if you do it say good bye to your filesystems, databases etc. etc. Would be nice to do it in a virtual host though ;) Cheers Robert > def sleep sec > t = Time.now > t+=sec > system(t.strftime(TimeStrFormat)) > sec.to_i > end > end > > if __FILE__ == $0 > p "the time is #{Time.now}" > sleep 5 > p "the time is #{Time.now}" > begin > sleep -5 > rescue Exception => ex > p "caught >>#{ex}<<" > end > p "the time is #{Time.now}" > > include Time_Demon > sleep 100 > p "the time is #{Time.now}" > sleep -50 > p "the time is #{Time.now}" > sleep -50 > p "the time is #{Time.now}" > end > ----- > > -Adam > > -- Deux choses sont infinies : l'univers et la bóŐise humaine ; en ce qui concerne l'univers, je n'en ai pas acquis la certitude absolue. - Albert Einstein ------ art_65614_24913224.1153220919747--