|
Joined: Dec 2000
Posts: 595
Member
|
Member
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? tks alot.
|
|
|
|
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.
|
|
|
|
Joined: Feb 2000
Posts: 4,625
Member
|
Member
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);
|
|
|
|
Joined: Sep 2000
Posts: 755
P.I.T.A. / Programmer
|
P.I.T.A. / Programmer
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
|
|
|
|
Joined: Feb 2000
Posts: 4,625
Member
|
Member
Joined: Feb 2000
Posts: 4,625 |
ok, don't unlock.
|
|
|
|
Joined: Dec 2000
Posts: 595
Member
|
Member
Joined: Dec 2000
Posts: 595 |
|
|
|
|
Joined: Dec 2000
Posts: 595
Member
|
Member
Joined: Dec 2000
Posts: 595 |
just a detail, is the locking done before or after the file is open?
|
|
|
|
Joined: Aug 2000
Posts: 335
Member
|
Member
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.
|
|
|
|
Joined: Dec 2000
Posts: 595
Member
|
Member
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?
|
|
|
|
Joined: Aug 2000
Posts: 335
Member
|
Member
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.
|
|
|
|
Joined: Dec 2000
Posts: 595
Member
|
Member
Joined: Dec 2000
Posts: 595 |
im making a script thatll open forums.cgi, read it, make changes and then save it..
|
|
|
|
Joined: Aug 2000
Posts: 335
Member
|
Member
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.
|
|
|
|
Joined: Dec 2000
Posts: 595
Member
|
Member
Joined: Dec 2000
Posts: 595 |
|
|
|
|
Joined: Dec 2000
Posts: 595
Member
|
Member
Joined: Dec 2000
Posts: 595 |
how the sub is getting the file name to lock?
local ($lockname) = shift;
? what is shift?
|
|
|
|
Joined: Feb 2000
Posts: 4,625
Member
|
Member
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.
|
|
|
|
Joined: Dec 2000
Posts: 595
Member
|
Member
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)?
|
|
|
|
Joined: Feb 2000
Posts: 4,625
Member
|
Member
Joined: Feb 2000
Posts: 4,625 |
it shortens the array by one, therefor moving everything down.
|
|
|
|
Joined: Dec 2000
Posts: 595
Member
|
Member
Joined: Dec 2000
Posts: 595 |
*studying* ok, tks! *studying o'reillys bible....*
|
|
|
|
Joined: Jul 2000
Posts: 1,349
Member
|
Member
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?
|
|
|
|
Joined: Dec 2000
Posts: 595
Member
|
Member
Joined: Dec 2000
Posts: 595 |
last one then. tks. (first one from the stack, understood..)
[ February 24, 2001: Message edited by: CopyCat ]
|
|
|
|
Joined: Aug 2000
Posts: 335
Member
|
Member
Joined: Aug 2000
Posts: 335 |
&Unlock("unlock.file");
should be
&Unlock("lock.file");
|
|
|
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: 1,157
Joined: July 2001
|
|
Forums63
Topics37,573
Posts293,925
Members13,849
|
Most Online5,166 Sep 15th, 2019
|
|
Currently Online
Topics Created
Posts Made
Users Online
Birthdays
|
|
|
|