Previous Thread
Next Thread
Print Thread
Rate Thread
#103139 09/16/2001 5:45 PM
Joined: Oct 2000
Posts: 743
Moderator / Code Fixer
Moderator / Code Fixer
Offline
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 frown
Sponsored Links
#103140 09/16/2001 5:52 PM
Joined: Mar 2001
Posts: 7,394
LK Offline
Admin / Code Breaker
Admin / Code Breaker
Offline
Joined: Mar 2001
Posts: 7,394
Quote
quote:
You should flock(FILE, LOCK_UN); before you close it wink

#103141 09/16/2001 7:24 PM
Joined: Oct 2000
Posts: 743
Moderator / Code Fixer
Moderator / Code Fixer
Offline
Joined: Oct 2000
Posts: 743
Ok ill give that a go.


I can't afford a good signature editor frown
#103142 09/17/2001 5:56 PM
Joined: Oct 2000
Posts: 743
Moderator / Code Fixer
Moderator / Code Fixer
Offline
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 frown
#103143 09/17/2001 8:23 PM
Joined: Sep 2000
Posts: 755
P.I.T.A. / Programmer
P.I.T.A. / Programmer
Offline
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
Sponsored Links
#103144 09/18/2001 12:38 AM
Joined: Oct 2000
Posts: 743
Moderator / Code Fixer
Moderator / Code Fixer
Offline
Joined: Oct 2000
Posts: 743
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 ]


I can't afford a good signature editor frown
#103145 09/18/2001 10:13 AM
Joined: Jan 2001
Posts: 1,940
Developer
Developer
Offline
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

#103146 09/18/2001 6:30 PM
Joined: Feb 2000
Posts: 4,625
Member
Member
Offline
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.

#103147 09/18/2001 6:34 PM
Joined: Feb 2000
Posts: 4,625
Member
Member
Offline
Joined: Feb 2000
Posts: 4,625
you should be doing:

Code
code:

or, to debug:

Code
code:

#103148 09/19/2001 7:13 AM
Joined: May 2000
Posts: 1,356
Addict
Addict
Joined: May 2000
Posts: 1,356
Quote
quote:
read the standard perl documentations wink

Sponsored Links
#103149 09/21/2001 4:20 AM
Joined: Oct 2000
Posts: 743
Moderator / Code Fixer
Moderator / Code Fixer
Offline
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 frown
#103150 09/21/2001 10:11 AM
Joined: Jan 2001
Posts: 1,940
Developer
Developer
Offline
Joined: Jan 2001
Posts: 1,940
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

#103151 09/24/2001 6:14 AM
Joined: Oct 2000
Posts: 743
Moderator / Code Fixer
Moderator / Code Fixer
Offline
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 frown

Link Copied to Clipboard
Donate Today!
Donate via PayPal

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.
Recommended Hosts
We have personally worked with and recommend the following Web Hosts:
Stable Host
bluehost
InterServer
Visit us on Facebook
Member Spotlight
Gizmo
Gizmo
Portland, OR, USA
Posts: 5,833
Joined: January 2000
Forum Statistics
Forums63
Topics37,573
Posts293,925
Members13,849
Most Online5,166
Sep 15th, 2019
Today's Statistics
Currently Online
Topics Created
Posts Made
Users Online
Birthdays
Top Posters
AllenAyres 21,079
JoshPet 10,369
LK 7,394
Lord Dexter 6,708
Gizmo 5,833
Greg Hard 4,625
Top Posters(30 Days)
Top Likes Received
isaac 82
Gizmo 20
Brett 7
WebGuy 2
Morgan 2
Top Likes Received (30 Days)
None yet
The UBB.Developers Network (UBB.Dev/Threads.Dev) is ©2000-2024 VNC Web Services

 
Powered by UBB.threads™ PHP Forum Software 8.0.0
(Preview build 20221218)