|
Joined: Oct 2000
Posts: 743
Moderator / Code Fixer
|
Moderator / Code Fixer
Joined: Oct 2000
Posts: 743 |
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
I can't afford a good signature editor
|
|
|
|
Joined: Mar 2001
Posts: 7,394
Admin / Code Breaker
|
Admin / Code Breaker
Joined: Mar 2001
Posts: 7,394 |
You should flock(FILE, LOCK_UN); before you close it
|
|
|
|
Joined: Oct 2000
Posts: 743
Moderator / Code Fixer
|
Moderator / Code Fixer
Joined: Oct 2000
Posts: 743 |
I can't afford a good signature editor
|
|
|
|
Joined: Oct 2000
Posts: 743
Moderator / Code Fixer
|
Moderator / Code Fixer
Joined: Oct 2000
Posts: 743 |
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
I can't afford a good signature editor
|
|
|
|
Joined: Sep 2000
Posts: 755
P.I.T.A. / Programmer
|
P.I.T.A. / Programmer
Joined: Sep 2000
Posts: 755 |
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?
"Annnnnnnndd now, opening for Iron Maiden...... WYLD STALLYNS!!!" --Bill S. Preston, Esquire and Ted "Theodore " Logan
|
|
|
|
Joined: Oct 2000
Posts: 743
Moderator / Code Fixer
|
Moderator / Code Fixer
Joined: Oct 2000
Posts: 743 |
Hi Mark, at the beginning of my program I am using 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 ]
I can't afford a good signature editor
|
|
|
|
Joined: Jan 2001
Posts: 1,940
Developer
|
Developer
Joined: Jan 2001
Posts: 1,940 |
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
|
|
|
|
Joined: Feb 2000
Posts: 4,625
Member
|
Member
Joined: Feb 2000
Posts: 4,625 |
Actually, he is calling use Fcntl qw(:DEFAULT :flock); which calls the DEFAULT part of the Fcntl module as well.
|
|
|
|
Joined: Feb 2000
Posts: 4,625
Member
|
Member
Joined: Feb 2000
Posts: 4,625 |
you should be doing: or, to debug:
|
|
|
|
Joined: May 2000
Posts: 1,356
Addict
|
Addict
Joined: May 2000
Posts: 1,356 |
read the standard perl documentations
|
|
|
|
Joined: Oct 2000
Posts: 743
Moderator / Code Fixer
|
Moderator / Code Fixer
Joined: Oct 2000
Posts: 743 |
Thats for your help everyone, I got it working nicely.
How does a shared lock operate differently from an exclusive lock?
I can't afford a good signature editor
|
|
|
|
Joined: Jan 2001
Posts: 1,940
Developer
|
Developer
Joined: Jan 2001
Posts: 1,940 |
joelogic, I had read the manual .. In fact I own Camel 3 but nowhere did it mention EFAULT .. it did mention DEFAULT and that's prolly what he refered to 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
|
|
|
|
Joined: Oct 2000
Posts: 743
Moderator / Code Fixer
|
Moderator / Code Fixer
Joined: Oct 2000
Posts: 743 |
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
I can't afford a good signature editor
|
|
|
Donate to UBBDev today to help aid in Operational, Server and Script Maintenance, and Development costs.
Please also see our parent organization VNC Web Services if you're in the need of a new UBB.threads Install or Upgrade, Site/Server Migrations, or Security and Coding Services.
|
|
Posts: 5,833
Joined: January 2000
|
|
Forums63
Topics37,573
Posts293,925
Members13,849
|
Most Online5,166 Sep 15th, 2019
|
|
Currently Online
Topics Created
Posts Made
Users Online
Birthdays
|
|
|
|