What will happen if two different programs try to write to the same file simulatneously?

StackOverflow https://stackoverflow.com/questions/5374567

  •  28-10-2019
  •  | 
  •  

Question

  1. What will happen if two different programs try to write to the same file simultaneously?

  2. Will one of the programs experience a file lock error?

  3. How should programs be designed to handle this scenario?

Was it helpful?

Solution

When the second app (or thread) try to open the file for writing it would throw IO exception.. simple..

OTHER TIPS

say you have user A and user B, what you can do is, let both of them modify the content, there will be a small difference in time however "simultaneous" you want it to be, so check which user has "submitted" the changes first, save those changes and prompt a smart message to the next user saying "file has been updated, check changes before.. blah blah".

Use FileLock to avoid IO Exception when file is being accessed by multiple threads.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top