• Print

Author Topic: Locking file ?  (Read 457 times)

Fifi

  • Sr. Member
  • ****
  • Posts: 303
  • Over 40 years in the damned software industry!
    • My personnal G-WAN server for tests.
Locking file ?
« on: October 28, 2016, 12:23:49 pm »
Hi all,

Does QB64 provide a locking (or an exclusive access) method when opening a file?

If yes, please tell me how to do so (and how to release the lock).

Thanx in advance.

Cheers.
Fifi
Prior to send the arrow of truth, plunge the head in the honey pot. (a Cheyenne idiom).

Fifi

  • Sr. Member
  • ****
  • Posts: 303
  • Over 40 years in the damned software industry!
    • My personnal G-WAN server for tests.
Re: Locking file ?
« Reply #1 on: October 31, 2016, 05:08:23 am »
Hi all,

Quote from: Fifi on October 28, 2016, 12:23:49 pm
Does QB64 provide a locking (or an exclusive access) method when opening a file?

If yes, please tell me how to do so (and how to release the lock).

Here is an explanation for this need:

I have a small and very simple program that opens a text file with every single line displays its number.

So this program reads the file up to the end, and just adds a new line with the next number, then close the file and exit.

Easy.

However, this program may be used simultaneously by hundred requests!

So the problem is that if the file isn't locked by a request, then 2 or 3 (or even more) different requests can read the file at the same time, so get the same last line number, then write the same value at the end of the file.

The result will then looks like this:

Code: [Select]
1
2
3
4
5
5
5
8
9
9
9
9
12
13
etc.
etc.
etc.
1000
1001
1001
1001
1004
etc.
etc.
etc.

Oops! That's wrong.

The expected result may look like this:

Code: [Select]
1
2
3
4
5
6
7
8
9
10
11
12
13
etc.
etc.
etc.
1000
1001
1002
1003
1004
etc.
etc.
etc.

With this exemple, 3 requests were reading the file at the same time when they arrived at the line 4, so you get 3 times the value 5 for the lines 5, 6 and 7 and the problem was the same when reading the line 8 where 4 requests wrote 4 times the value 9 for the lines 9, 10, 11 and 12, etc. etc. etc.

The only way to avoid this is to lock the file when a request opens it.

Is there any way to do so with QB64, and if yes, how?

Then how to unlock the file before the exit?

Note: this is possible in C but only when using atomic functions.

Thank you in advance.

Cheers.
Fifi
Prior to send the arrow of truth, plunge the head in the honey pot. (a Cheyenne idiom).

RhoSigma

  • Sr. Member
  • ****
  • Posts: 264
  • Out Of Time
Re: Locking file ?
« Reply #2 on: October 31, 2016, 05:43:23 am »
Is OPEN file FOR APPEND LOCK WRITE AS #1 not working for you ?

try this (Note: Windows API calls involved)
http://www.qb64.net/forum/index.php?topic=11588.msg99371#msg99371

Fifi

  • Sr. Member
  • ****
  • Posts: 303
  • Over 40 years in the damned software industry!
    • My personnal G-WAN server for tests.
Re: Locking file ?
« Reply #3 on: October 31, 2016, 12:27:22 pm »
Hi RhoSigma,

Quote from: RhoSigma on October 31, 2016, 05:43:23 am
Is OPEN file FOR APPEND LOCK WRITE AS #1 not working for you ?

try this (Note: Windows API calls involved)
http://www.qb64.net/forum/index.php?topic=11588.msg99371#msg99371

Thank you for the first tip.

I'll give a try ASAP and let you know.

However, could your "mutex" stuff work under Linux?

TIA for your input.

Cheers.
Fifi.
« Last Edit: October 31, 2016, 02:23:49 pm by Fifi »
Prior to send the arrow of truth, plunge the head in the honey pot. (a Cheyenne idiom).

RhoSigma

  • Sr. Member
  • ****
  • Posts: 264
  • Out Of Time
Re: Locking file ?
« Reply #4 on: November 01, 2016, 02:31:36 am »
Quote from: Fifi on October 31, 2016, 12:27:22 pm
However, could your "mutex" stuff work under Linux?

probably not AS IS, but i'm pretty sure the Linux kernel will have similar functions,

named "CreateMutex", "CloseHandle" and "WaitForSingleObject", unfortunately i'am absolutely dumb on Linux, so some of our other Linux buddies should have a look on this.
« Last Edit: November 04, 2016, 06:38:37 am by RhoSigma »

  • Print