Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Textfile sharing

Status
Not open for further replies.

lorenzodv

Programmer
Feb 16, 2001
95
IT
I need to have multiple instances of the same Perls script writing/reading to the same text file.
How can I awoid problems sharing the file?
Thanks.

--
Lorenzo Dalla Vecchia
 
I haven't done that myself, but if I did I'd assume you'd have to do some kind of "locking". I did "perldoc -q lock", and here is *some* of what I got back:

--------------------------------------

=head1 Found in /usr/lib/perl5/5.00503/pod/perlfaq5.pod

=head2 How can I lock a file?

Perl's builtin flock() function (see L<perlfunc> for details) will call
flock(2) if that exists, fcntl(2) if it doesn't (on perl version 5.004 and
later), and lockf(3) if neither of the two previous system calls exists.
On some systems, it may even use a different form of native locking.
Here are some gotchas with Perl's flock():

=over 4

=item 1

Produces a fatal error if none of the three system calls (or their
close equivalent) exists.

=item 2

lockf(3) does not provide shared locking, and requires that the
filehandle be open for writing (or appending, or read/writing).

=item 3

Some versions of flock() can't lock files over a network (e.g. on NFS
file systems), so you'd need to force the use of fcntl(2) when you
build Perl. See the flock entry of L<perlfunc>, and the F<INSTALL>
file in the source distribution for information on building Perl to do
this.

For more information on file locking, see also L<perlopentut/&quot;File
Locking&quot;> if you have it (new for 5.006).

---------------------------------------

Do &quot;perldoc -q lock&quot; yourself and read through all the perldoc info on locking.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
There is a Perl function, 'flock' that will helps manage what you are trying to do. You can get the perldoc on it with 'perldoc -f flock'.

Flock will try to establish a lock on the file. If another process already has it locked, flock will wait. If the individual instances of a program locking the file will be relatively brief, you can let flock wait until the file is unlocked, at which point flock in the current program will lock the file. Note that this 'locking' works only for programs that check to see if the file is locked. Any other programs that don't check will go ahead and run over you.

The following is general flow. In each program that will be accessing the file,

flock HANDLE, 2; # exclusive lock - no other 'flocking' program can get to it
# do what you want to the file.
flock HANDLE, 8; # release the file for other processes to use

If the work each process does on the file takes a little while, you might not want flock to sit and wait. In that case, do something like..

unless (flock HANDLE, 2) { do something else }

'hope this helps










keep the rudder amid ship and beware the odd typo
 
Thanks goBoating, you cleared my doubts.

--
Lorenzo Dalla Vecchia
 
To continue on this subject is there a way to keep other programs from accesing the file till you close the file? I am using a discusware board and am writing to the userlist but if the board accesses the user database it gets stopped in the middle. I was using flock but until reading the previous entry I had no idea flock only worked if you were using flock troughout your program.

Thanks
Shelby
 
FLOCK works on a &quot;per use&quot; basis as well! Remember, each time a user accesses your script, a new instance of the script is created. Therefore, if you use FLOCK when you open the file and then release flock when you close the file then you're &quot;locking&quot; the file for a very short period of time.

Note: FLOCK only works if you call it like this: use Fcntl &quot;:flock&quot;; before trying to open the file. There's always a better way...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top