[Gambas-user] Locking files - best method

nando nando_f at ...951...
Tue Feb 17 15:03:33 CET 2009


I think you're looking for the filesystem to prevent you from file operations
while 'locked'.  I used 'locking' with VB5 and relied on Window's internal locking
mechanism. After years of agony, I found the external subdirectory method
the best.  I don't have shared files on just one computer, there are many computers.
I found that VB5 sometimes got stupid and never unlocked - especially when
long time between reboots.  But now, with the subdir method, I can fully control it.
I do this with Gambas using Samba while I port away from VB5.

The main problem is this:
Program 1 locks a file (or a portion of a file) and that program freezes up, 
locks up, computer is shutdown, etc WHILE the lock is in force.
Then other programs try the same and....
The whole system is screwed!
Now that I use subdirs, I know that a lock 'should be' free (ie unlocked)
within 10 seconds because I know that there is no way my code should last
that long....and something is wrong.  In such a case, I can actually
proceed (fake re-locking it) with the second program, give a warning message
that it couldn't lock because it is taking too long but is proceeding
and will subsequently unlock (rmdir) too.
In very few cases, I have had to go in and RMDIR manually because things
got jammed up, but discovered those situations to be wierd-condition bugs
which are now fixed.
Personally, I like the full control it provides and works over multiple
programs and multiple computers.


---------- Original Message -----------
From: Rolf-Werner Eilert <eilert-sprachen at ...221...>
To: mailing list for gambas users <gambas-user at lists.sourceforge.net>
Sent: Mon, 16 Feb 2009 08:33:12 +0100
Subject: Re: [Gambas-user] Locking files - best method

> Benoît Minisini schrieb:
> >> Sharing a file exclusively locked very briefly and getting everything
> >> to work correctly amongst multiple programs can be brain-twisting.
> >> With increasing use of mulit-cores, the possibility for sharing files
> >> using any lock technique is more important than ever before
> >> because all associated programs can execute simultaneously.
> >>
> > 
> > You can use the LOCK/UNLOCK commands and a specific void file on the disk to 
> > create system exclusive sections in your code.
> > 
> > Let's suppose you want to modify a file named "/tmp/foo".
> > 
> > ...
> > DIM hLock AS Stream
> > DIM hFile AS Stream
> > 
> > ' Try to acquire the lock
> > TRY hLock = LOCK "/tmp/foo.lock"
> > IF ERROR THEN
> >   PRINT "'foo' is being modified. Try again later."
> >   RETURN
> > ENDIF
> > 
> > ' Lock was acquired, you can modify the file now!
> > hFile = OPEN "/tmp/foo" FOR OUTPUT
> > ...
> > CLOSE #hFile
> > 
> > ' Do not forget to release the lock
> > UNLOCK hLock
> > ...
> >
> 
> IMHO "the one" drawback remains: foo.lock isn't foo. You cannot lock the 
> file itself, relying on the operating system / the file system to really 
> make sure nobody else is able to corrupt your file during your access.
> 
> If there's some other app that doesn't care about .lock files, your 
> /tmp/foo will be lost. Of course, the same applies to my own methods 
> like lock directories or self-made lock files.
> 
> The only way to solve this would be a mechanism in the file system 
> itself, giving the opportunity to code something like
> 
> hFile = OPEN "/tmp/foo" FOR OUTPUT LOCK
> 
> which would unlock the file automagically when you CLOSE it (or the 
> instance of your application ends).
> 
> Regards
> 
> Rolf
> 
> ------------------------------------------------------------------------------
> Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
> -OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
> -Strategies to boost innovation and cut costs with open source participation
> -Receive a $600 discount off the registration fee with the source code: SFAD
> http://p.sf.net/sfu/XcvMzF8H
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
------- End of Original Message -------





More information about the User mailing list