FileLock for two exclusive processed

81 Views Asked by At

Windows supports LockFile function for applying exclusive locks on a file, and the LockFileEx functions for applying an exclusive locks and shared read for all processes.

Is it possible to have ProcessA apply an exclusive lock, and then allow Only ProcessA And ProcessB to read the file, while at the time, no other process - outside those two processes - would have read access on the locked file?

1

There are 1 best solutions below

0
Jeaninez - MSFT On

According to the Doc:LockFileEx function

Exclusive locks cannot overlap an existing locked region of a file. Shared locks can overlap a locked region provided locks held on that region are shared locks. A shared lock can overlap an exclusive lock if both locks were created using the same file handle. When a shared lock overlaps an exclusive lock, the only possible access is a read by the owner of the locks.

In my opinion, Exclusive lock doesn't allow read and write operations.Shared lock allows only read operation.

If the file is set Exclusive lock with LockFileEx function, all other processes will be denied both read and write access. If the file is set Shared lockwith LockFileEx function, all processes can read the locked region. If the file is set shared lock and exclusive lock at the same time, the only possible access is a read by the owner of the locks.