[Gambas-user] How to lock a file

nando nando_f at ...951...
Tue Oct 7 16:19:23 CEST 2008


There is a problem with your method.
Between the execution of the KILL and the CREATION of the file,
multitasking happens and another task could create the file just before
the original thread executed the CREATE. 

In different words, if the first thread lost the CPU just after the KILL command
due to multitasking and a second thread began the WHILE condition,
the WHILE would not execute because the WHILE condition fails and 
then the file is CREATED with the user of the second thread.
Then, multitasking happens and the second thread loses the CPU and the first
thread continues and it will then create the file destroying the second
threads information.  Anything could happen in a multitasking environment.

I use directories because folder creation is a one-step process there isn't time between
the two steps for
another task to steal the lock in betweem code statements.
An example is something like...

'create lock, this can be in a SUB with tje folder name passed in

counter=100     '100 loops of 0.1 sec = 10 seconds max to try lock
flag = FALSE    
WHILE flag = FALSE
  TRY MKDIR "lock-folder-name"
  IF ERROR THEN  'it failed
    flag=TRUE
    error.clear
    WAIT 0.1
    dec counter
    if counter<=0 then
      'you could say the the lock took too long
      'you could allow a user choice to retry or blindly continue.
      break   'we will assume something is locked too long
    endif
  else
    flag=FALSE
  endif
wend


'unlock, this can be a SUB too
TRY RMDKR "lock-folder-name"
error.clear

You don't need to know who has the lock because only a successful lock
leads to code writing some important information for the thread that successfully
locked.  The unlock must follow very shortly thereafter to release the lock
and cannot hog the lock.

-Fernando


---------- Original Message -----------
From: Rolf-Werner Eilert <eilert-sprachen at ...221...>
To: nando_f at ...951..., mailing list for gambas users
<gambas-user at lists.sourceforge.net>
Sent: Tue, 07 Oct 2008 10:47:02 +0200
Subject: Re: [Gambas-user] How to lock a file

> For my apps in Gambas, I use lockfiles, not folders, to lock a certain 
> file. Experience told me that "sometimes" when two users/processes want 
> to access the same file, these may overlap. So I added an identification 
> method and a time delay in case of locks. This is what it looks like 
> (kinda prototyped):
> 
> WHILE Exist(theLockFile) THEN
> 
>    OPEN theLockFile FOR READ ...
>      LINE INPUT firstLine
>    CLOSE ...
> 
>    IF firstLine = myOwnName THEN 'old lock file from myself:
>      KILL theLockFile              'may be deleted, then proceed
> 
>    ELSE                          'lock file from someone else:
>      WAIT one second
> 
> WEND
> 
> 'now first of all, write my own lockfile
> 'then begin reading the file I want
> 'finally delete lockfile
> 
> This is how this looks in practice:
> 
>    WHILE Exist(pfad &/ "klassenbuch.block")
>      OPEN pfad &/ "klassenbuch.block" FOR READ AS #dtnr
>        LINE INPUT #dtnr, t$
>      CLOSE #dtnr
>      IF t$ = system.User THEN
>        KILL pfad &/ "klassenbuch.block"
>      ELSE
>        WAIT 1
>      END IF
>    WEND
> 
>    OPEN pfad &/ "klassenbuch.block" FOR WRITE CREATE DIRECT AS #dtnr
>      PRINT #dtnr, system.User
>    CLOSE #dtnr
> 
>    OPEN karteiVerz &/ jg &/ "klassenbuch" FOR READ AS #dtnr
> 
> and so on. While I am writing this email, I find that this has one big 
> drawback: If the lock file was made by someone else and that app 
> crashed, my program will wait forever for the lockfile to disappear. So 
> one should add a counter, e. g. 5 seconds, to stop waiting.
> 
> Regards
> 
> Rolf
> 
> nando schrieb:
> > In some applications, I create a folder as a lock.
> > The existance of the folder indicates 'locked'
> > It works very well and is used through Samba to share
> > the lock on both the linux and windows side.
> > -Fernando
> > 
> > 
> > ---------- Original Message -----------
> > From: Benoit Minisini <gambas at ...1...>
> > To: mailing list for gambas users <gambas-user at lists.sourceforge.net>
> > Sent: Fri, 26 Sep 2008 23:51:57 +0200
> > Subject: Re: [Gambas-user] How to lock a file
> > 
> >> On vendredi 26 septembre 2008, Almanova Sistemi wrote:
> >>> I need to lock the file to avoid writing by another user in multiuser
> >>> environment
> >>>
> >> Sorry for that, the documentation is completely false. But who wrote it? :-)
> >>
> >> The LOCK instruction does not lock a specific stream not a stream, but instead 
> >> use a specific path to create a global system lock.
> >>
> >> You use it this way:
> >>
> >> DIM hFile AS File
> >>
> >> TRY hFile = LOCK "~/lock"
> >> IF ERROR THEN
> >>   PRINT "Already locked by something else!"
> >> ELSE
> >>   PRINT "Got locked!"
> >>   ...
> >>   UNLOCK hFile
> >> ENDIF
> >>
> >> Do not rely on the lock file contents. It is truncated to zero byte when the 
> >> lock is acquired.
> >>
> >> Regards,
> >>
> >> -- 
> >> Benoit Minisini
> >>
> 
> -------------------------------------------------------------------------
> This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
> Build the coolest Linux based applications with Moblin SDK & win great prizes
> Grand prize is a trip for two to an Open Source event anywhere in the world
> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> _______________________________________________
> 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