Link to home
Start Free TrialLog in
Avatar of Rohit Bajaj
Rohit BajajFlag for India

asked on

What happens when two users make changes to a file simulataneouly

Hi,
Suppose there is file which has "rohit bajaj" written in it.
Now suppose two users login to a machine and cd to this file location both having read/ write access to this file opens the file and they modify it to --

1) First user
rohip pakka

2) Second User
pohit pakaj

And they save the file at the same time.
what will the file contain ? Or what are the possibilities for its content ?

Two clear possibilities are that the changes of user1 or user2 only are visible. Is there any other possibilty ? Kind of mixup of both ? and how ?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Mal Osborne
Mal Osborne
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is a message generated by Excel 2010, running on Windows 10, trying to open an already open file on a 2008R2 server. Not all applications on all platforms will do this:

User generated image
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rohit Bajaj

ASKER

Hmm Database is clear. But do all filesystem have mechanisms like that...
Like take the case of linux machine... and vi editor...
Or probably it depends on the editor being used...
What if its actually been done through a java code...
Well when you get right down to it, assuming nothing gets in your way, the last user that saves a piece of data (and someone would always be last), is what you'd end up seeing.

 The key word there is "piece".     If the editor / code has a file open in binary, then they can address any individual byte and if that's what is committed (saved) rather than the entire file as a complete unit, then yes, things could get pretty mashed up.

But if I write the first 10 bytes and then you write the same 10 bytes, I will always see in those 10 bytes what you wrote.

Jim.
There is no one simple answer to the question.  The topic you are asking about is referred to as "file locking".  There are different types of file locking implementations,and they vary from operating system to operating system (or operating system variant), from file system to file system, and, in databases, from database to database.

Some platforms support multiple file locking choices that can be selected at the OS level, the file system level, the file level, or the application level.  Some platforms, like Windows, support locking a selected range of data within a file, as opposed to the entire file.  And database locking options are even more complex.

Depending on the locking scheme in place, several things could happen (in vi, or a Java program, or whatever).  Here are some examples:

  • First process to open obtains an exclusive read/exclusive write lock on the file.  No other process can open it until the first process releases the lock.
  • First process to open obtains a shared read/exclusive write lock on the file.  Any process can open, but only the first process can write.
  • First process to open obtains a shared read/shared write lock (or no lock).  Any process can open, and any can write.  If multiple processes just append data (like a log file), this sort of scheme would work OK, as long as the OS serializes the writes.  If the entire file is rewritten at save time, last one to save generally wins.  If two processes save at exactly the same time "unpredictable results" may occur - this gets way down to implementation-specific details of the OS, file system, and application.  You'd just have to test it on the target platform to see what happens.
Anyway, this is a fairly complex topic.  Here are a few resources for your reading enjoyment that probably delve down deeper into the subject than you may care to go :-)

https://en.wikipedia.org/wiki/File_locking
https://gavv.github.io/blog/file-locks/
http://chris.improbable.org/2010/12/16/everything-you-never-wanted-to-know-about-file-locking/