Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2008-05

FpFlock with Text as argument

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

amir

Hi,

I don't know if it is a bug or I am using fpflock incorrectly. I wanted to use fpflock to lock a file (and be sure that only one process at a time can have access to my file).

At first, I tried AssignFile and passed the TextFile(Text) variable to fpflock with LOCK_EX. It didn't work and both processes can access the file. Both print "After Flock".

Then I modify the code, and used fpopen instead of AssignFile and passed the cInt variable to fpflock . This worked.

I attached the codes of both experiments.

Amir

Reply by email

Luca Olivetti
to amir

En/na amir ha escrit:

At first, I tried AssignFile and passed the TextFile(Text) variable to fpflock with LOCK_EX. It didn't work and both processes can access the file.

Maybe fpLock just needs an open file, and AssignFile just establishes which filename corresponds to a pascal file, it doesn't open it. Try a "reset(InputFile)", though I think reset won't wait, it will just fail if the file is locked.

Bye

Luca

Reply by email

Michael Van Canneyt
to amir: I don't know if it is a bug or I am using fpflock incorrectly. I wanted to use fpflock to…
amir wrote 9 lines
Hi, I don't know if it is a bug or I am using fpflock incorrectly. I wanted to use fpflock to lock a file (and be sure that only one process at a time can have access to my file). At first, I tried AssignFile and passed the TextFile(Text) variable to fpflock with LOCK_EX. It didn't work and both processes can access the file. Both print "After Flock".

After AssignFile(), there is no filehandle yet, so you cannot lock the file. You can only lock the file after Reset() or ReWrite() was called.

Note that Reset and Rewrite will always work (unless the file doesn't exist, obviously) even if another process has locked the file. The subsequent fplock() operation should determine whether another process has locked the file.

Michael.

Reply by email