From: Ned Konz Date: 2002-07-26T07:16:57+09:00 Subject: Re: Singleton Classes & thread safety On Thursday 25 July 2002 02:44 pm, David King Landrith wrote: > This is the same program being run twice concurrently, but from > what you say it sounds like this may be equivalent to two separate > programs. From the OS point of view (and for that matter from the Ruby interpreter's point of view) they *are* separate programs. That they share the same source code doesn't make them the same program. > I'd like to protect the resources that the program uses, > and I was hoping that using a singleton class to access the > resources would ensure this (rather than writing and debugging my > own mutex stuff). I wouldn't be much surprised if someone had already made a file-based mutex in Ruby. If you can't find one, it's easy to port Perl to Ruby. But actually, under Linux you might be able to get away with running 'ps' or examining /proc/[0-9]*/cmdline to see if you're already running. for instance, here's a program that I named "myname" and made executable. Try running it twice from different shell prompts. #!/usr/bin/ruby IO.popen("ps -C myname") { |file| file.each { |line| line.chomp! if line.match(/^\s*(\d+).*\s+(\S+)$/) if $2 == "myname" if $1 == Process.pid.to_s puts "found my PID #{$1}" else puts "found another PID #{$1}" end end end } } sleep 30 -- Ned Konz http://bike-nomad.com GPG key ID: BEEA7EFE