File sharing, locking, etc... between multiple users...

David Vaughan dvk at dvkconsult.com.au
Tue Feb 21 21:17:47 EST 2006


On 22/02/2006, at 5:00, "Jonathan Lynch" <jonathandlynch at gmail.com>  
wrote:
>
> 1) User A reads the file, and determines it is not owned
> 2) In the few milliseconds between when User A reads the file, and  
> resaves
> it, user B reads the file and also reads that it is not owned.
> 3) User A makes changes and saves the data
> 4) User B, who should have been locked out but was not, makes  
> changes and
> saves his data, overwriting the changes made by User A.
>
> My plan is to minimize the possibility of this type of collision by  
> having
> the minimum possible time between reading a file and resaving it with
> ownership. Secondly, I will include some collision detection code

That sort of thing can work quite well in scripted apps. I had to do  
a similar thing where the rate of access was very low but it was  
nonetheless important that there be no conflict. It worked like this:
1. User A reads directory and if the file is already owned, sleeps a  
randomly short time and tries again.
2. On finding the path clear, User A writes a flag asserting  
ownership and sleeps a randomly short(er) time.
3a. On waking again, User A reads the flags and if it is still the  
sole owner, sets about its business.
3b. If, on this second awakening, User A finds another owner  
contesting (i.e. a race condition slipped through the first check  
above) then User A removes their own ownership claim and goes back to  
the beginning.

Like I said, this is for low volume stuff but it is "perfectly"  
effective in preventing conflicts arising from race conditions while  
the random components avoid continuous synchronised failure. Setting  
a flag file in the directory should be faster than reading, changing  
and writing the content of the main file.

regards
David



More information about the use-livecode mailing list