Previous Thread
Next Thread
Print Thread
Rate Thread
#102258 02/18/2001 9:06 PM
Joined: Dec 2000
Posts: 595
Member
Member
Offline
Joined: Dec 2000
Posts: 595
hi

id love to know a basic command to lock a file while the script is writing to it... nothing fancy.... and some explanation on that perhaps O_o? laugh tks alot.

Sponsored Links
#102259 02/18/2001 10:30 PM
Joined: Jan 2000
Posts: 5,073
Admin Emeritus
Admin Emeritus
Joined: Jan 2000
Posts: 5,073
Are you going to be operating on Windows or on Unix?


UBB.classic: Love it or hate it, it was mine.
#102260 02/18/2001 11:49 PM
Joined: Feb 2000
Posts: 4,625
Member
Member
Offline
Joined: Feb 2000
Posts: 4,625
If your on unix or windows nt/2000 use flock.

use Fcntl ':flock';
#lock
flock(FILE,LOCK_EX);

#unlock
flock(FILE,LOCK_UN);

#102261 02/19/2001 12:12 AM
Joined: Sep 2000
Posts: 755
P.I.T.A. / Programmer
P.I.T.A. / Programmer
Offline
Joined: Sep 2000
Posts: 755
No need to unlock. Perl will automatically release the lock on the file when the close command is encountered (or when the program ends)

--mark


"Annnnnnnndd now, opening for Iron Maiden...... WYLD STALLYNS!!!" --Bill S. Preston, Esquire and Ted "Theodore " Logan
#102262 02/19/2001 12:26 AM
Joined: Feb 2000
Posts: 4,625
Member
Member
Offline
Joined: Feb 2000
Posts: 4,625
ok, don't unlock. wink

Sponsored Links
#102263 02/19/2001 9:09 AM
Joined: Dec 2000
Posts: 595
Member
Member
Offline
Joined: Dec 2000
Posts: 595
thanks alot.

#102264 02/19/2001 9:55 AM
Joined: Dec 2000
Posts: 595
Member
Member
Offline
Joined: Dec 2000
Posts: 595
just a detail, is the locking done before or after the file is open?

#102265 02/19/2001 10:11 AM
Joined: Aug 2000
Posts: 335
Member
Member
Offline
Joined: Aug 2000
Posts: 335
After.

And one limitation of flock: If you open a file with mode '>' or '+>', the file gets clobbered immediately (before the lock is acquired), which can be a problem if someone else is reading from the file.

#102266 02/19/2001 3:49 PM
Joined: Dec 2000
Posts: 595
Member
Member
Offline
Joined: Dec 2000
Posts: 595
what if i create a separete file and then rename/ovewrite it over the old one? will it wait untill the last user stops reading it?

#102267 02/20/2001 10:11 AM
Joined: Aug 2000
Posts: 335
Member
Member
Offline
Joined: Aug 2000
Posts: 335
I don't understand your question.

File locking is a complex issue. If you can explain exactly how this file is being used, I'll try to suggest a locking strategy.

Sponsored Links
#102268 02/20/2001 10:22 AM
Joined: Dec 2000
Posts: 595
Member
Member
Offline
Joined: Dec 2000
Posts: 595
im making a script thatll open forums.cgi, read it, make changes and then save it..

#102269 02/20/2001 10:50 AM
Joined: Aug 2000
Posts: 335
Member
Member
Offline
Joined: Aug 2000
Posts: 335
In that case, you should use the same locking mechanism that the UBB uses.

If you really mean 'forums.cgi', rather than 'var_forums.cgi', that means you using UBB version 5x, which doesn't use flock, so you wouldn't want to use flock either. (flock is an 'advisory' lock - it only works if everybody who accesses the same file uses it.)

In UBB 5x, locking is done using the subs Lock and Unlock in ubb_library.pl. You can search for &Lock("lock.file") to see how it's used.

#102270 02/20/2001 10:59 AM
Joined: Dec 2000
Posts: 595
Member
Member
Offline
Joined: Dec 2000
Posts: 595
ok dave, thanks.

#102271 02/24/2001 7:47 PM
Joined: Dec 2000
Posts: 595
Member
Member
Offline
Joined: Dec 2000
Posts: 595
how the sub is getting the file name to lock?

local ($lockname) = shift;

?
what is shift?

#102272 02/24/2001 8:08 PM
Joined: Feb 2000
Posts: 4,625
Member
Member
Offline
Joined: Feb 2000
Posts: 4,625
shift shifts the first value of the array off and returns it, shortening the array by 1 and moving everything down.

just call

&Lock("lock.file");

and then

&Unlock("unlock.file");

when your done.

#102273 02/24/2001 8:14 PM
Joined: Dec 2000
Posts: 595
Member
Member
Offline
Joined: Dec 2000
Posts: 595
thanks mastermind. thats a rather scaring code hehe

sorry about my bad english, will the shift delete the first 'variable' from the array (0)?

#102274 02/24/2001 8:18 PM
Joined: Feb 2000
Posts: 4,625
Member
Member
Offline
Joined: Feb 2000
Posts: 4,625
it shortens the array by one, therefor moving everything down.

#102275 02/24/2001 8:25 PM
Joined: Dec 2000
Posts: 595
Member
Member
Offline
Joined: Dec 2000
Posts: 595
*studying* ok, tks! *studying o'reillys bible....*

#102276 02/24/2001 8:48 PM
Joined: Jul 2000
Posts: 1,349
Ell Offline
Member
Member
Offline
Joined: Jul 2000
Posts: 1,349
@array = "hello","there";
&mysub(@array);

.
.
.
.

sub mysub {
$this = shift;
$that = shift;
}


in the sub:
$this = "there";
$that = "hello";


Experts Q: Aren't arrays passed by copy, rather than by reference? you have to callsub(*array); and access @_ to alter the original array, right?

#102277 02/24/2001 8:52 PM
Joined: Dec 2000
Posts: 595
Member
Member
Offline
Joined: Dec 2000
Posts: 595
last one then. tks.
(first one from the stack, understood..)

[ February 24, 2001: Message edited by: CopyCat ]

#102278 02/24/2001 10:17 PM
Joined: Aug 2000
Posts: 335
Member
Member
Offline
Joined: Aug 2000
Posts: 335
&Unlock("unlock.file");

should be

&Unlock("lock.file");


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
isaac
isaac
California
Posts: 1,157
Joined: July 2001
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 20240430)