Recent

Author Topic: fpflock vs tcritical  (Read 5388 times)

toby

  • Sr. Member
  • ****
  • Posts: 252
fpflock vs tcritical
« on: July 13, 2019, 10:11:22 pm »
i am doing cgi whidh writes all web accesses to same file

i have fpflock code on writing to file and tcritical section around the writing to file both seem to do the job (but i have to do it in a thread for the tcritical even though it is just a single thread)

which is really better/more professional for less resource usage (i am thinking fpflock) but would like to hear from the fpc devs etc

note : using fileopen fmshareexclusive was very slow using filewrite vs fpflock using writeln)

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: fpflock vs tcritical
« Reply #1 on: July 13, 2019, 10:25:51 pm »
Nonsense. Remove.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Akira1364

  • Hero Member
  • *****
  • Posts: 561
Re: fpflock vs tcritical
« Reply #2 on: July 13, 2019, 10:35:08 pm »
Nonsense. Remove.

Perhaps... oddly phrased, to say the least, but not nonsense. They seem to be asking whether it is preferable to use the fpFlock function or a TCriticalSection to "lock" a file.
« Last Edit: July 13, 2019, 11:52:33 pm by Akira1364 »

toby

  • Sr. Member
  • ****
  • Posts: 252
Re: fpflock vs tcritical
« Reply #3 on: July 13, 2019, 10:41:30 pm »
what is wrong with you - get some civility into your existence - you should not be allowed to post this kind of uncivil bs in a public forum

toby

  • Sr. Member
  • ****
  • Posts: 252
Re: fpflock vs tcritical
« Reply #4 on: July 13, 2019, 10:50:27 pm »
you have never done any cgi coding yet have no problem putting in a rediculous reply?  database vs writeln?  are you kidding me?

toby

  • Sr. Member
  • ****
  • Posts: 252
Re: fpflock vs tcritical
« Reply #5 on: July 13, 2019, 10:56:05 pm »
They seem to be asking whether it is preferable to use the fpFlock function or a TCriticalSection to lock a file.

BrunoK

  • Sr. Member
  • ****
  • Posts: 452
  • Retired programmer
Re: fpflock vs tcritical
« Reply #6 on: July 13, 2019, 11:21:49 pm »
If you do write/writeln to the file, you could already try not bother about locks. It seems that FPC 3.0.4 does it for you.
Just try.

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: fpflock vs tcritical
« Reply #7 on: July 13, 2019, 11:32:33 pm »
TCriticalSection has nothing to do with protecting a file. It only makes sense if you use more than one thread. TCriticalSection only prevents several threads to access the same code. So in my eyes fpFlock and TCriticalSection isn't the same, because they do totally different things.  :)
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

toby

  • Sr. Member
  • ****
  • Posts: 252
Re: fpflock vs tcritical
« Reply #8 on: July 13, 2019, 11:42:24 pm »
Hi BrunoK

That isn't true you have to lock the file of use tcriticalsection around the file write code - you get a real mess if the file isn't locked or  the file writing code isn't in a tcriticalsection

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: fpflock vs tcritical
« Reply #9 on: July 13, 2019, 11:58:20 pm »
Hi BrunoK

That isn't true you have to lock the file of use tcriticalsection around the file write code - you get a real mess if the file isn't locked or  the file writing code isn't in a tcriticalsection

I think this statement  merits more information from your part.

Locking the file should prevent any other process from being able to write/read it until you unlock it, while enclosing the writing code in a critical section "simply" signals the system that you want to do it as close to an "atomic" operation (without synchronization worries) as possible. They are not really related operations, more than in a very general sense.

What "mess" are you seeing? What is you're trying to do: writing to a log or similar, an HTTP PUT? ...?
« Last Edit: July 14, 2019, 12:00:27 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

toby

  • Sr. Member
  • ****
  • Posts: 252
Re: fpflock vs tcritical
« Reply #10 on: July 13, 2019, 11:59:51 pm »
Hi RAW

> TCriticalSection has nothing to do with protecting a file. It only makes sense if you use more than one thread.

you have to use a thread to even use tcriticalsection
and it has everything to do with protecting  file if the code in the tcriticalsection is writing to a file

> TCriticalSection only prevents several threads to access the same code. So in my eyes fpFlock and TCriticalSection isn't the same, because they do totally different things. 

the end result is the same if done if threads are done in a single program

(i am adding this only to help someone else doing cgi and wanting to protect writing to a file)
this is now the key for me - tcriticalsection doesn't protect the file writing code if the program is run several times in a cgi - it only protests the code in threads running in the same file - unlike fpflock which protects the file writing even if several instances of the program _are_ run (not the plural)

thank you for this insight i missed    fpflock is the only way to do it (and i;ve already disqualified fileopen fmshareexclusive with filewrite)


toby

  • Sr. Member
  • ****
  • Posts: 252
Re: fpflock vs tcritical
« Reply #11 on: July 14, 2019, 12:07:47 am »
Hi lucamar

yes it is just what a form post sends to the server

'the mess' is in the file - interspersed data from the different forms posts  -  you have to use fpflock to lock the file so other program instances can't write to file (fpc 3.0.4 or fpc 3.1.1 doesn't do it automatically)

and tcriticalsection only protects code in a single program (no matter how many threads - though for a single thread it isn't needed - evidentally)
it does not protect the code writing section from other instances


lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: fpflock vs tcritical
« Reply #12 on: July 14, 2019, 12:17:05 am »
Ah, now I understand.

Yes, locking the file should suffice in those circumstances; the second and further instances won't be able to access it until the first unlocks it.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

toby

  • Sr. Member
  • ****
  • Posts: 252
Re: fpflock vs tcritical
« Reply #13 on: July 14, 2019, 12:32:48 am »
Hi one last thing - might be a bug or smething i missed

when i use :

writeln('fpflock EX ', fpflock(fw, LOCK_EX), ' fpgeterrno : ', fpgeterrno, ' ', syserrormessage(fpgeterrno));

i get output

fpflock EX 0 fpgeterrno : 25 Not a typewriter

is this correct?  the fpgeterrno and the syserrormessage  - i;ve googled it and nothing explains this to me

toby

  • Sr. Member
  • ****
  • Posts: 252
Re: fpflock vs tcritical
« Reply #14 on: July 14, 2019, 01:00:12 am »

i filled up the program with
writeln('fpgeterrno : ', fpgeterrno, ' ', syserrormessage(fpgeterrno)); //

and it occures because of this line which is before the fpflock line  (good that it isn't from the fpflock line but i still don't understand it lol)
if fileexists(fn) then append(fw) else rewrite(fw);

 

TinyPortal © 2005-2018