Is there a limitation for System.IO.FileShare?

I want to create my own flat file database. This is how I access the flat file database

Dim fs As New System.IO.FileStream("C:\MyDb.txt", IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read) Dim sr As New System.IO.StreamReader(fs) 

Is there a restriction on using .Net to use System.IO.FileShare.Read , System.IO.FileShare.Write and System.IO.FileShare.ReadWrite when working with a file?

I mean, is .Net capable of supporting thousands of users using a file stream and an object read stream using System.IO.FileShare.Read to access a single file at the same time?

+6
source share
6 answers

I do not know the exact limit imposed by .NET / windows, so I created a real test for you. I checked the following test code for several minutes, and I found that before 635908 counting the use of system.io.fileshare it still works, i.e. You can still read the contents of a flat database file.

Here is the code (this is a winform application, .Net 4):

 Public Class Form1 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim filepath As String = "c:\database.txt" Dim filestream As System.IO.FileStream Dim count As Int32 For count = 0 To System.Int32.MaxValue filestream = New System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read) AppendLog(count, filestream.ReadByte) Next End Sub Private LogFilepath As String = "C:\LogInfo.txt" Private Enter As String = Chr(13) & Chr(10) Private Space As String = " " Private Sub AppendLog(ByVal Sequence As Int32, ByVal info As Byte) System.IO.File.AppendAllText(LogFilepath, Enter & Sequence & Space & CStr(info)) End Sub End Class 
+2
source

If you try to open a file with conflicting access rights and access rights, this will not work. But if it is a custom database, why do you need more than one file descriptor? Your user database software should manage open descriptors (from 1 per file). As for your specific question, there is no limit, but subsequent opening of the file must follow the rules of access and sharing.

http://msdn.microsoft.com/en-us/library/aa363874%28v=vs.85%29.aspx

+4
source

The FileShare element means that other files can also open the file. This means that it does not guarantee that the data will be synchronized in any way - it simply means that several programs can now read (since you installed - FileShare.Read ) from this file while you open it.

If you use ReadWrite , then several programs can read and write to a file. Again, you will not receive notification of any changes. If several programs are written to the same file at the same time as the stream, the data will be mixed together and you will get a damaged file. (A corrupt value means that neither you nor the other program can decompile it, because your data is intertwined with the application of your friends).

There are no unreasonable restrictions on the number of simultaneous programs reading a file.

+3
source

Several objects can access the same file, however, the disk on which the files are stored supports a read cache buffer for each object / process, and the cache will be multiplied by the number of objects accessing the file. Performance depends on the number of bytes needed to store the cache for each object and the total cache capacity.

If the file was modified during a read operation, use asynchronous reads. however, if the process terminates with the part of the file that is locked or closing the file with wonderful locks, the behavior is undefined.

I would recommend explicitly destroying stream objects if this is not required.

+1
source

You should have only one FileStream to write to the file and restrict its use to one stream at a time, using the usual locking mechanisms. A common model for DBMS software is to have a parallel queue of write operations and have a write stream that flushes them. In cases where you need the source of the write operation to complete it, you can use the asynchronous model (BeginWrite / EndWrite).

A Semaphore may be exactly what you need to read, as it allows the maximum number of threads to access it at any given time. You can use it to limit disk performance that would occur for an unlimited number of simultaneous random reads.

However, you should ALWAYS store the β€œhottest” data cache in memory to reduce the load. Without it, your drive will not be fast enough to keep up.

0
source

Opening a file usually means that it is opened exclusively by you, and no other process can access it unless you tell Windows that you want to share it.

Setting FileShare.Read/Write means that you give other processes the right to read or write the file while you open it.

I repeat: you give other processes the right to read and / or write to your file. Nothing more, nothing less.

Imagine a bit for file sharing as a door:

  • Nobody means that the door is closed and locked.
  • Reading means that you can only go one way.
  • Write means that you can only go differently.

So what is the door limit?

0
source

Source: https://habr.com/ru/post/894376/


All Articles