UBB.Dev
Posted By: BassTeQ File Locking - 09/17/2001 12:45 AM
How can I check if a file has been locked by another process in my script? Because its a cgi script and can be used my multiple users, hence causing a problem with the file it writed to. Im not very familiar with file locking so any help would be appreciated.

Just after I open the file in question I use
flock(FILE, LOCK_EX);
and after I close it I use
flock(FILE, LOCK_UN);

But using the above still causes problems with the data file.

Thanks
Posted By: LK Re: File Locking - 09/17/2001 12:52 AM
Quote
quote:
You should flock(FILE, LOCK_UN); before you close it wink
Posted By: BassTeQ Re: File Locking - 09/17/2001 2:24 AM
Ok ill give that a go.
Posted By: BassTeQ Re: File Locking - 09/18/2001 12:56 AM
Didnt really help, my main data file is still becomming corrupt. For example the program outputs a list of links, each time someone clicks on a link it increments a number in the data file. Now if I click on 3 links quickly, all 3 processess are reading & writting to the file at the same time, hence cauing it to become corrupt/incorrect, so how can I go about fixing this problem so that only one process can access the file at any given time, and the other processes have to wait until the one before it has finished.
Any help greatly appreciated.

Thanks
Posted By: Mark Badolato Re: File Locking - 09/18/2001 3:23 AM
First, don't unlock the file. when you close the file it will automatically be unlocked.

Second, are you doing

use Fcntl ':flock';

at the beginning of your program?
Posted By: BassTeQ Re: File Locking - 09/18/2001 7:38 AM
Hi Mark, at the beginning of my program I am using
Code
code:

is that different to
use Fcntl ':flock';

Sorry but this file locking thing is all new to me, never used it before.

Thanks

[ 09-18-2001: Message edited by: BassTeQ ]
Posted By: qasic Re: File Locking - 09/18/2001 5:13 PM
I have never seen that EFAULT type flock. Mark's way is the standard way that flock is specified. If you don't want to do that you can always use flock(DATA, 1) for exclusive lock and flock(DATA, 4) for a shared lock. An unlock is flock(DATA, 8) but you don't need to do that when you close a file because it's done automatically by Perl.

qasic
Posted By: Greg Hard Re: File Locking - 09/19/2001 1:30 AM
Actually, he is calling use Fcntl qw(:DEFAULT :flock); which calls the DEFAULT part of the Fcntl module as well.
Posted By: Greg Hard Re: File Locking - 09/19/2001 1:34 AM
you should be doing:

Code
code:

or, to debug:

Code
code:
Posted By: Burak Re: File Locking - 09/19/2001 2:13 PM
Quote
quote:
read the standard perl documentations wink
Posted By: BassTeQ Re: File Locking - 09/21/2001 11:20 AM
Thats for your help everyone, I got it working nicely.

How does a shared lock operate differently from an exclusive lock?
Posted By: qasic Re: File Locking - 09/21/2001 5:11 PM
joelogic, I had read the manual .. In fact I own Camel 3 smile but nowhere did it mention EFAULT .. it did mention DEFAULT and that's prolly what he refered to smile

Anyways, exclusive lock - the process who sets the exclusive lock can both read and write to the file only ... nobody else can. Shared lock - process to who locked the file has read/write access to it but others only have read access to the file.

qasic
Posted By: BassTeQ Re: File Locking - 09/24/2001 1:14 PM
Ok, thats for clearing that up for me!

jeologic
And that was the DEFAULT module, its just that the UBB turned it into a smilie because I had a semi-colan in front of it.

Cheers
© UBB.Developers