|
|
Question : File locking / share access
|
|
I have a database of registered users of my site. Every user can update his information, and it goes back to the same place. How should I organise that database so that there won't be any problem with the simultaneous access of multiple users? I am running Apache on Linux, Perl. Can use mSQL, xBase and, of course, plain text or tied hash. Should I create a "lock" file and wait in a loop until that file is deleted? Note: the "Users" database is just a sample. Really, I am talking about a file (table) that is being read/updated many times per second, and that's why I expect problems with sharing. Thank you!
|
Answer : File locking / share access
|
|
================================================================================ Note that the following pseudo-code is not reliable:
while true if (not exists LockFile) then CreateLockFile exit-while end if sleep end while AccessDataFile
As in multi-processing, a different process may create the lock file between the if.. and the CreateLockFile..
The code below uses sysopen, which trys to create a file, and fails if it already exists.
use Fcntl;
if (sysopen(f1, $path, O_WRONLY + O_EXCL)) or die "Couldn't open $path for writing: $!\n";
This way you can loop until a LockFile is created succesfully. When you are done accessing the data-file, you delete the LockFile.
The problem with lock-files is that you have to do polling and waiting for them to be released (which seems unreasonable for accesses multiple times a second), and there is always a chance that a lock-file stays on disk because of a bug in your code or some other mishap. Then data would be inaccessible, until you manually remove the lockfile.
If flock(2) is implemented on your machine, you can try something like:
$LOCK_SH = 1; $LOCK_EX = 2; $LOCK_NB = 4; $LOCK_UN = 8; open MyFile, ">>PathToMyFile" or die "Can't open MyFile: $!"; flock MyFile, $LOCK_EX; print MyFile "here is a new line\n"; flock MyFile, $LOCK_UN;
You can try implementing the critical section with semaphores: http://theory.uwinnipeg.ca/CPAN/perl/pod/perlfunc/semop.html
Depending on the number of users in your system, you can consider having a dedicated data-file for each user.
If you use databases, they have internal locking capabilities, and usually give you control over types of locks (exclusive, shared on row, page, table). You can also control timeouts for waiting on locks.
Hope this helps, Cadabra
|
|
|
|
|