1

I want my .NET app to read a file that was written to by a Julia app. While reading it, I want my app to somehow lock the file so the Julia process cannot write it while .NET is reading it.

Only the Julia process ever writes this file.
Only my .NET app ever reads this file.

How is this done?

I would like to use:
System.IO.File.Copy( source_filename, destin_filename )

Doug Null
  • 7,989
  • 15
  • 69
  • 148
  • Are you sure that this "Julia" process can handle the fact that the file is locked for writing? – dymanoid Jan 11 '19 at 15:48
  • Not sure if this helps, but worth checking this [link](https://learn.microsoft.com/en-us/dotnet/api/system.io.filestream.lock?view=netframework-4.7.2) – Nilesh Jan 11 '19 at 15:55
  • Who is Julia? Can you talk to her to convince to have some IPC or API for what you need? You can try to open file exclusively (assuming Julia will release it immediately after writing), but then your reading may cause Julia to crash. – Sinatr Jan 11 '19 at 15:56
  • @Sinatr https://julialang.org/ – stuartd Jan 11 '19 at 15:57
  • @stuartd, thanks, completely miss the tag and thought it's a name of another programmer. – Sinatr Jan 11 '19 at 15:59
  • You would use FileShare.Read when you open the file. Which permits other processes to also read the file but not to write. Since that is the default choice for FileAccess.Read, including for File.Copy(), you don't have to anything at all. The language does not matter, this is implemented by the OS. https://stackoverflow.com/a/25098500/17034 – Hans Passant Jan 11 '19 at 16:25

2 Answers2

1

All modern Filesystems work like Databases. As such they have Locks and Transactions. NTFS is a shining example of this.

You want to aquire a Write Lock on the file in addition to your read lock (or just a ReadWrite Lock). And then just only use the read one. There can be multiple read locks, but only one write lock on any given file. In some cases anyone having a write lock can even exclude anyone else getting a read lock (and vice versa) - it can be nesseary to avoid file inconsistencies during reading. But that goes into deep implementation details.

Of course now the question becomes how well the other programm will deal with someone else locking the file. Technically you are supposed to handle that as any other Exogenous exception, but I am the first to admit that I might have skipped that code (and the retry one if nessesary) in some cases. Or the Multitasking for gracefull handling of a long lock aquisition time.

In either case, it might be best to release your lock ASAP. For such a purpose, it can be adviseable to just read the whole file into memory quickly and then work on the memory copy. How to go about that in .NET depends a lot on the filesize. The x32 Memory Limit and Object Limit are notorious for getting in the way with files in the GiB range.

Christopher
  • 9,634
  • 2
  • 17
  • 31
0

I think the way to go in this case is to have a .net thread that detects existence of new file from Julia, and then immediately moves it to a different, exclusive path, where it is read and deleted. The Julia app is only writing the file every 5 seconds.

Doug Null
  • 7,989
  • 15
  • 69
  • 148