You've used the requisite search terms -- file and lock.
So why didn't you search for a howto?
And, not just BTW, most of your "simplicity sake" (sic) discussion simply adds verbosity and concepts that are irrelevant to "jsut (sic) what steps" are required to accomplish file locking during a specific set of operations.
OTOH, you don't tell us what OS (which is relevant, as JavaFan points out) nor do you show us any code or pseudocode illustrating what you've tried and where you're stuck. | [reply] |
I also need to make sure no other user can open the file and read and/or write to it while the process is taking place for another user running it. What are the exact steps needed to take in order to accomplish this?
That depends on your OS/filesystem. If you have mandatory locking, just require an exclusive lock. Otherwise, you may need do drastic measures like dropping the run level so the OS runs in single user mode. Or by hacking the kernel sources, recompiling and installing the changed kernel. Of course, if you own the file, removing write access for everyone else easily accomplishes the task.
| [reply] |
I appreciate the help but I didn't think I was entering into a spelling bee. All I want to know is what is the best (most efficient, cross OS, generic) way of locking a file (to read in and modify the contents and then write them back to the same file using, if possible, the same filehandle). I don't want to have to read in a file with a lock, modify the contents, close that handle, then open the file again with a lock to write back. This leaves a small window between locks where the file could get read in before another user has his changes saved (the latter user would then overwrite the first user's changes). The application should be able to accomplish this on as many platforms as possible (Windows would come first, Unix, Linux). I'd appreciate any HELPFUL input on this, not posts that simply are used to criticize or weak attempts at trying to impress with their vocab with no helpful insight. | [reply] |
| [reply] |
Thank you for the post. Most helpful. :-) I've been trying to use +> to open the file for read/write purposes but cannot read the contents in. I'm trying to read the contents into an array such as (simpler version) but it isn't working :
open (FH, ">+path");
my @lineArray = <FH>;
print join("<br>", @lineArray); # nothing prints although the file do
+es have content
Is there something I have to do with buffering? Reading from the handle in this way is no good when opening for read/write? Any help on this would be most appreciated as well. Thanks. | [reply] [d/l] |
best (most efficient, cross OS, generic) way of locking a file
There is no generic solution!! It will be OS and file system dependent. The best you could do is write (or find) a module that accounts for all of these variances. Though, as JavaFan indicated, even that could be challenge since on some systems you will have to isolate the file.
Elda Taluta; Sarks Sark; Ark Arks
| [reply] |