Lock/Unlock

General FreeBASIC programming questions.
aRanger
Posts: 9
Joined: May 04, 2009 22:27

Lock/Unlock

Post by aRanger »

I don't understand what is going on here. The docs say the record parameter is optional but when I specify this:

UnLock #pDcp->hFile

I get a compiler error saying it expects a ",". The same thing happens with the Lock statement. So I am confused on this. It is apparently only accepting the second syntax specified in the help file or at least that is what it appears.

aRanger
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

Whoever wrote the doc page was misinformed. They may have been working off QB documentation rather than testing the FB version.

Several months ago ( http://freebasic.net/forum/viewtopic.ph ... 438#108438 ) someone brought this to light. The problem hasn't been fixed, but the wiki (from which the doc files are generated) has been updated to reflect the problem.
http://freebasic.net/wiki/KeyPgLock
http://freebasic.net/wiki/KeyPgUnlock

I'd say it's a deficiency in FreeBASIC, but it needs someone who knows what they're doing to fix it.
aRanger
Posts: 9
Joined: May 04, 2009 22:27

Post by aRanger »

OK thanks, I will just use FileAttr and get the Windows file handle and use the Windows API to lock and unlock the file - should not be a problem.
ceoceo
Posts: 40
Joined: Jul 01, 2007 1:52

lock

Post by ceoceo »

Program1:
Open "ARCH" For RANDOM As #1 Len=10
lock #1, 1
sleep

Program2:
Dim a As string *10
open "ARCH" For RANDOM As #1 Len=10
get #1, 1, a

don't work lock ?

Thanks
oursilverton
Posts: 2
Joined: Mar 13, 2012 15:33

Re: Lock/Unlock

Post by oursilverton »

Locking doesn't work quite like that...
you have to put lock/unlock around your file actions, the 'system' does not actually lock a file, it's kind of like getting a hall pass.

You ask the system to lock something (whether it is a file or a record or a port, or whatever) and the system gives you a pass key.
when you are done, you give back the pass key.
If someone else tries to get the pass key while you have it, they are told 'sorry Charlie', and have to wait until you've given it back.

Now, some systems will make you wait while the pass key is out, but that can cause deadlocks so they often (and this seems to be the case with freebasic) will return an error instead of just waiting... the problem with freebasic is the error returned when a file is already locked (the passkey is out for that file / record combination) is 3, which is just File IO error, so it doesn't really tell you what happened.

So, to put locking into your system you have to lock before and unlock after every write AND read (whatever method you write and read, be it put or get or print or input or whatever)

AND you have to check the status code from the lock (ERR) to know whether or not the file is already locked by another program.

NOTE: UNLOCK in freebasic ALWAYS returns error code 3 - at least in the version I'm using.

I wrote an fLock routine that tries to lock, then if it gets error code 3, it sleeps for a while, then tries again, with a maximum number of loops - I'll let you figure out how to do it yourself, that's half the fun of programming isn't it? challenges like this?

I put a variable in the parameters to tell the routine whether to wait or not, so that I can get an immediate response if I want.

Again, if you use locking on writes (puts/prints/whatever) only, it won't lock readers and you MIGHT get strange results (theoretically at least).

Hopefully this will help...
Andy
ceoceo
Posts: 40
Joined: Jul 01, 2007 1:52

Re: Lock/Unlock

Post by ceoceo »

Thanks Andy
Solve it with routines in "C"
freebasic-now
Posts: 7
Joined: Jun 13, 2013 8:54

$2,500 USD Reward to Fix the File Byte Lock and Unlock funct

Post by freebasic-now »

didacted.
Last edited by freebasic-now on Oct 19, 2015 6:28, edited 10 times in total.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Lock/Unlock

Post by counting_pine »

I think the best way to achieve this is to fix FB's runtime library directly. For someone comfortable with writing more low-level file locking code, working it into FB's runtime library - which is written in C - probably wouldn't be too difficult.

For the changes to be accepted into FB's source code, they must be made available under the LGPL with linking exception:
http://sf.net/p/fbc/code/ci/master/tree ... icense.txt

We'd also want some demonstration/testing code which could be easily adapted into a unit test.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Lock/Unlock

Post by counting_pine »

Does the technique in MichaelW's code (http://freebasic.net/forum/viewtopic.ph ... 97#p108497) work OK for you?
freebasic-now
Posts: 7
Joined: Jun 13, 2013 8:54

Re: Lock/Unlock

Post by freebasic-now »

didacted.

.
Last edited by freebasic-now on Oct 19, 2015 6:29, edited 4 times in total.
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: Lock/Unlock

Post by vdecampo »

Can't you just use the LockFile function to lock a file under Windows?
Zippy
Posts: 1295
Joined: Feb 10, 2006 18:05

Re: Lock/Unlock

Post by Zippy »

@freebasic-now
freebasic-now wrote: It could be argued, with the Nvidia CUDA, Intel Phi, pending Xeon, Arm, and SeaMicro type of solutions, the file byte locking and unlocking challenge is the MOST IMPORTANT IMPROVEMENT NEEDED for the entire Freebasic Ecosystem. With it, the future is possible, without it, Freebasic is relegated to yesterday's use... A single app running on a legacy PC or similar configuration, that is not optimal. The future is massive multi-core and massive parrallelism.
Hyperbole. At best.

[Disclaimer: I do not speak for the fb community, the fb devs, or anyone other than myself.]

Who are you? Who/what is "we"? Your anonymity belies credibility.

Present/prove your identity, your credentials, the exact criteria of the challenge (specfics that can be independently measured and ajudicated), your ability to pay (escrow) - then I might take you seriously (but note that I consider your challenge inappropriate to FreeBASIC). You otherwise come off as an erudite sockpuppet looking to cream free code.

Zzzzzzz
freebasic-now
Posts: 7
Joined: Jun 13, 2013 8:54

Re: Lock/Unlock

Post by freebasic-now »

didacted
Last edited by freebasic-now on Oct 19, 2015 6:30, edited 4 times in total.
freebasic-now
Posts: 7
Joined: Jun 13, 2013 8:54

Re: Lock/Unlock

Post by freebasic-now »

didacted
Last edited by freebasic-now on Oct 19, 2015 6:31, edited 5 times in total.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Lock/Unlock

Post by counting_pine »

Zippy wrote:Present/prove your identity, your credentials, the exact criteria of the challenge (specfics that can be independently measured and ajudicated), your ability to pay (escrow) - then I might take you seriously (but note that I consider your challenge inappropriate to FreeBASIC). You otherwise come off as an erudite sockpuppet looking to cream free code.
If anyone is interested in writing this, I think a sensible procedure might be:

- freebasic-now provides some explicit specifications for code they want to work.
Ideally, they include some simple test code, that declares, calls and tests the set of functions they want implemented. They will also explain how they will pay the money.
- the willing coder agrees to the above terms, implements those functions and provides a pre-compiled static library that contains them.
(The library could perhaps be "crippled" in some way, to ensure it only works with the test code or in a limited time frame.)
- freebasic-now links the library to their test code, tests the executable and confirms that it works as they expect, and agrees in principle to hand over the money.
- the coder e-mails a copy of the source code to a mutually trusted third party, who confirms that the code exists and works.
- freebasic-now transfers the agreed sum of money.
- the coder releases the source code to the library to the public. (The third party is also able to do this, to ensure the coder doesn't take the money and run.)

Risks:
- freebasic-now refuses to pay up, and the coder's time goes unrewarded.
- the third party conspires with freebasic-now, and hands over the code without the payment taking place.
- the third party conspires with the coder, and they both take the money and run without releasing the code.

There is not much risk of the third case, since the coder has already gone to the trouble of implementing the routines well enough to pass the test code, and has no clear incentive to keep it secret.
I would say there is little risk of the second case, if the mutually trusted person is someone known and trusted by the community here.
Post Reply