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

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

  •  28-10-2019
  •  | 
  •  

سؤال

  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?

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top