I frequently find myself writing scripts which access text files as a form of basic database. My understanding is that the file should be locked to grant a script instance exclusive file access and prevent problems if two script instances try to access the same file simultaneously:
open(TXT, "< text.txt");
flock(TXT,2);
# Read file etc.
flock(TXT,8);
close TXT;
I can understand the need for this when writing to a file but is it really necessary when reading? Surely the OS the webserver is running on can handle multiple simultaneous reads, especially in the case of UNIX.
This is presenting a problem with my current project where I would like to use perl to send a file to STDOUT to effectively force the user to download a file rather than just using an html link.
If I were to use a link, multiple users could downlaod the same file simultaneously. If I use the script method with flock then only one instance of the script (and thus one user) can download the file concurrently. Not a problem with a 30k image, but a 20MB software update will be.
On the same subject, just what does happen if flock is in use and a second script instance tries to access a locked file? Does the second script abort, carry on executing without opening the file (potentially resulting in errors unless a check is made to ensure the file was successfully accessed) or wait until the file becomes available and then resume? I have always assumed the latter but do not know if this is the case.
Many thanks,
Tyger.
open(TXT, "< text.txt");
flock(TXT,2);
# Read file etc.
flock(TXT,8);
close TXT;
I can understand the need for this when writing to a file but is it really necessary when reading? Surely the OS the webserver is running on can handle multiple simultaneous reads, especially in the case of UNIX.
This is presenting a problem with my current project where I would like to use perl to send a file to STDOUT to effectively force the user to download a file rather than just using an html link.
If I were to use a link, multiple users could downlaod the same file simultaneously. If I use the script method with flock then only one instance of the script (and thus one user) can download the file concurrently. Not a problem with a 30k image, but a 20MB software update will be.
On the same subject, just what does happen if flock is in use and a second script instance tries to access a locked file? Does the second script abort, carry on executing without opening the file (potentially resulting in errors unless a check is made to ensure the file was successfully accessed) or wait until the file becomes available and then resume? I have always assumed the latter but do not know if this is the case.
Many thanks,
Tyger.