Hi,

I forgot to include some description how it should work. The guarded
file is not %lockdir/lock, but any other file - because read-only
accesses to the file don't need to be guarded, and would fail if the
guarded file is currently renamed. You should make a directory
%lockdir/ with an empty file %lock

then you should use:

obtain-semaphore
something: read %my-guarded-file.txt
write %my-guarded-file.txt "something new"
release-semaphore

If you want to only read from your file, you can use just
something: read %my-guarded-file.txt
print something

If I remember correctly, you must only guard one situation,
"read-modify-write", because two simultaneous reads do not interfere,
and two simultaneous writes are a logic error of your program.

obtain-semaphore function uses loop and random wait interval to have a
better chance of succeeding if several processes run it simultaneously.
release-semaphore function should be simpler, loop is not necessary:

release-semaphore: func [] [
    if not error? try [ rename %lockdir/lock.locked %lock ] [
        return
    ]
    print "Could not release semaphore"
    quit
]

-- 
Michal Kracik

[EMAIL PROTECTED] wrote:
> 
> Thank you Mr. Kracik!
> 
> If only I undersatood completely whats going on in the script. Please
> correct me if I´m wrong:
> 1.Files in use are flagged with "semaphore"
> 2. The files in use are renamed as "%lock.locked"
> 
> The script loops, continues to "Try" to rename, if it encounters an error
> then it "waits". Wonßt I get an error anyway if the file is directly
> renamed when I CGI script user requests a file?
> 
> Excuse my newbie question
> 
> Best regards
> 
> Sharriff Aina
> med.iq information & quality in healthcare AG
> Gutenbergstr. 42
> 
> 
>                     [EMAIL PROTECTED]
>                     km.cz                An:     [EMAIL PROTECTED]
>                                          Kopie:
>                     11.10.00             Thema:  [REBOL] How does one persist data? 
>Re:
>                     21:56
>                     Bitte
>                     antworten an
>                     list
> 
> 
> 
> Hi Sharriff,
> 
> be careful when saving to a file and reading it later, as many users
> may run your CGI programs simultaneously. You should use some form of
> inter-process synchronization to serialize access to your file. AFAIK
> from REBOL you can reliably synchronize by opening TCP sockets or
> renaming a file. I use these two functions:
> 
> obtain-semaphore: func [] [
>     loop 100 [
>         if not error? try [ rename %lockdir/lock %lock.locked ] [
>             return
>         ]
>         wait ( random 5 ) / 10
>     ]
>     print "Could not obtain semaphore"
>     quit
> ]
> 
> release-semaphore: func [] [
>     loop 100 [
>         if not error? try [ rename %lockdir/lock.locked %lock ] [
>             return
>         ]
>         wait ( random 5 ) / 10
>     ]
>     print "Could not release semaphore"
>     quit
> ]
> 
> --
> Michal Kracik
> 
> [EMAIL PROTECTED] wrote:
> >
> > Does anyone have a soultion of persisting data across HTML pages? I want
> > data submitted through a CGI from a HTML form to be usable in other
> pages.
> >
> > Many thanks for the anticipated help guys
> >
> > Sharriff Aina
> > med.iq information & quality in healthcare AG

Reply via email to