A simple inter-process lock

Linux程序员通常喜欢用文件锁来做进程间的同步,或简单地用文件锁指示程序进程是否还健在。在ruby里面可以很简单的实现文件锁:
=begin
 file lock for inter-process sync.
 usage:
  FSLock('mylock') do
    # protected by lock,
    # do your job here ...
  end
=end 
class FSLock
  def initialize(name=nil)
    name ||= 'global'
    @fname = name + '.lock'
    if block_given?
      lock()
      yield
      unlock()
    end
  end

  def critical
    lock()
    yield() if block_given?
    unlock()
  end

  def lock
    @f = File.new(@fname, "ab")
    @f.flock(File::LOCK_EX) if @f
  end

  def unlock
    @f.close if @f
  end
end


测试代码:
if $0 == __FILE__
  unless fork
    # child process
    3.times do |i|
      FSLock.new('/tmp/myapp') do
        sleep 2   # child process sleep while holding the lock
        puts "#{Time.now.to_s}: Ping !"
      end
    end
  else
    # parent process
    sleep 0.1
    6.times do
      FSLock.new('/tmp/myapp') do   # parent process will be blocked while child holding the lock
        puts "#{Time.now.to_s}: Pong !"
      end
      sleep 0.1
    end
    Process.wait
  end
end


父子进程通过文件锁来同步,子进程持有锁后休眠2秒导致父进程企图获取锁时休眠。最后子进程不在持有锁的时候,父进程不再block。

Win32用户可以在Cygwin下运行此代码。

你可能感兴趣的:(linux,F#,Ruby)