0

In my ruby script I have put below code to make sure the same script should not be executed again parallel when another process is already running.

if $0 == __FILE__
  if File.new(__FILE__).flock(File::LOCK_EX | File::LOCK_NB)
    if RbConfig::CONFIG['host_os'].include? 'linux'
      linux_main
    elsif RbConfig::CONFIG['host_os'].include? 'mingw'
      windows_main
    elsif RbConfig::CONFIG['host_os'].include? 'solaris'
      solaris_main
    end
  end
end

__END__

But its not working as expected on Windows. I have initiated one process and when I run initiate another process on another powershell session its still running instead of coming out.

Any suggestion pls.

1 Answer 1

1

Ruby's File class is mostly a thin layer on top of POSIX. As a result, several methods either don't work or behave differently on non-POSIX platforms. This is explicitly acknowledged in the documentation, e.g. the method you are using says:

Not available on all platforms.

It is also implementation-dependent. E.g. JRuby went through the trouble to develop a full POSIX compatibility layer for Java, and as a result, on Windows, JRuby actually is more conforming with Ruby than "Ruby" (i.e. YARV) is!

Long story short: File#flock is just a thin layer on top of POSIX's flock function, and that function simply doesn't exist on Windows. Note that Windows does support file locking using the LockFile Windows API function. However, it doesn't support POSIX file locking. (Except maybe in a POSIX emulation environment such as Mirosoft Services for Unix, MSYS, or Cygwin.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.