c# – c # file locking


In php, it is possible to simultaneously access a file with write locks LOCK_EX (exclusive write access) and read LOCK_SH (shared read access) The lock is called after opening the file via flock

When a lock is invoked, the behavior depends on the type of lock. In general, it works similarly to ReaderWriterLock and in c # you need to use it for this behavior. However, ReaderWriterLock in c # runs in a single process (and does not directly relate to files).

Moreover, windows will interfere with simultaneous access to the file from different processes.

There is a FileShare in c #, but it looks like it's just permission to another process to open a file for the same type of access (from msdn it's not very clear what, but in fact)

Give an example of how to make the same locking behavior with a shared file in c # as in php or its abstract counterpart ReaderWriterLock

upd : I see an explanation is required what kind of behavior is expected, let's say there are several processes that want to almost simultaneously

  1. read
  2. read
  3. read
  4. write
  5. read
  6. write

The first 3 will get concurrent read permission. 4th will wait and receive a block to write when the first 3 release the file. 5th will wait until 4th frees, and 6th until 5th frees

So the options FileShare.ReadWrite or FileShare.Write do not follow any queue (I checked 2 writers and my output is porridge). They just indicate what another process can do without throwing an exception. But they do not protect files from simultaneous recording in any way.

If we set an exclusive lock without FileShare, then other processes will receive exceptions, which we want to avoid

upd2: again no one understands what the problem is.

Problem : There are many processes that can read and write to the file.

Purpose : to protect the shared file from concurrent access so that only one writer can write at a time. Not "deny others access", but provide competing access , when many readers can read a lot at the same time, but write only one, that is, what ReaderWriterLock is for (I do not see a fundamental difference between it and the file locking system in php)

The option "catch exceptions and repeat after 100ms until victorious" does not stand up to criticism. A writer who is waiting for the file to be free may never wait, because in those 100ms a bunch of readers can open the file for themselves. The correct solution is that a read / write lock is queued and in pkhp (and not only in it) it is available out of the box, but in c # I did not find how to do it.

In short, the question sounds like this: how to make ReaderWriterLock for files that are shuffling between several processes?


Behavior similar to that described in PHP:

File.Open(path, mode, FileAccess.ReadWrite, FileShare.Read)

This command opens the file for read-write, while allowing other processes to open the file for reading (thereby holding the write lock).

File.Open(path, mode, FileAccess.Read, FileShare.ReadWrite)

This command, on the contrary, opens the file for reading – and at the same time allows other processes to read and write it.

Behavior similar to ReaderWriterLock:

File.Open(path, mode, FileAccess.ReadWrite, FileShare.None)

This is an exclusive lock on the file, opening it in read-write mode.

File.Open(path, mode, FileAccess.Read, FileShare.Read)

This is a non-exclusive lock on the file, opening it in read mode.

Well, 14 more combinations remained unconsidered 🙂

Scroll to Top