Controlling Simultaneous read/writes to a file
Posted on 2006-06-02
I want to carry a transaction number in a file shared by a number of processes.
Each process will periodically update this transaction number however I want to be able to control contention on this file.
In C or C++ you can use the Lock directive to perform an exclusive lock of a portion of the file such that if any other process attempts a simultaneous lock, the second process will be placed in CPU wait until the lock is removed by the first process.
A process will lock the file, read and increment the transaction number before writing the new transaction number and finally unlocking the file.
What directives are used in C#.NET to accomplish an exclusive lock on a file that will place any other bidding processes on hold until the first lock is removed.