in reply to Re: Need Advice on Folder/Drive locking
in thread Need Advice on Folder/Drive locking

As i don't want the drive to be shared, instead of semaphore can i use mutex? Also, how do we create a mutex on a remote machine?

  • Comment on Re^2: Need Advice on Folder/Drive locking

Replies are listed 'Best First'.
Re^3: Need Advice on Folder/Drive locking
by cdarke (Prior) on Feb 04, 2010 at 15:28 UTC
    A Semaphore with a maximum count of 1 is functionally the same as a mutex (sometimes called a binary semaphore). Of course you can use a mutex instead, is this on Windows?

    Nowhere in your post did you mention that a remote machine is involved. In that case a Mutex or Semaphore is not appropriate (I'm not so sure that file locking would be either). Other options include named pipes, but this then depends on the operating system.
      I am sorry that i didn't post the right information at the beginning itself. yes, it is on windows machine & the drives are located on a remote machine.
        OK. Windows mutuxes do not conform to DCE and cannot be applied across machines. I have done this in the past, but it involved named pipes (which, on Windows, are a communications protocol). It is a non-trival task.

        There are other sneaky ways of performing IPC if you have shared drives. One is to change the (Windows) attributes of a "well known" shared file (can be empty) in its own folder. You can use Win32::File with GetFileAttributes and SetFile Attributes.

        For example, set the READONLY attribute to indicate that the disc is not to be written to, then unset it when you are done. Check that attribute before reading. Unfortunately this means you have to poll the file, or preferably use notification events with Win32::ChangeNotify.

        Update: I should have pointed out that using file attributes as a flag is not atomic, and can lead to a race condition. It appeared to me that this is unlikely in your senario, however for a poduction system that could not be tolerated. In that case you would need a service on one of the machine to control access - this is where Named Pipes come in.