file locking : does the os do this ?

J

Jazeker

Hello,

This question may be a bit out of scope. Just let me know.

I have some Perl/CGI scripts that write to/read from a text file. 'till
now, I always assumed that when you open a file for writing, the OS will
not allow another instance of the script to do the same (via refusing or
making it wait, ...) and that the Perl programmer should not be
concerned with that. I have seen that Perl offers locking mechanisms,
but I usually see them in the context of random I/O file handling (and
IIRC in binary mode).

Am I wrong and do I have to include file locking in my script when using
the usual simple text file handling like :
open (TEST, "> test.txt") || die "Error opening test.txt";
print TEST "hello";
close TEST || die "Error opening test.txt";

Sorry for the somewhat off-topicness of this message... just a link to
a better newsgroup or a good read will suffice then :)

thx
 
C

Chris Mattern

Jazeker said:
Hello,

This question may be a bit out of scope. Just let me know.

I have some Perl/CGI scripts that write to/read from a text file. 'till
now, I always assumed that when you open a file for writing, the OS will
not allow another instance of the script to do the same (via refusing or
making it wait, ...)

Depends on the OS and file system, but generally, no, the OS will assume
you know what you're doing and allow the simultaneous access.
and that the Perl programmer should not be
concerned with that. I have seen that Perl offers locking mechanisms,
but I usually see them in the context of random I/O file handling (and
IIRC in binary mode).

Am I wrong and do I have to include file locking in my script when using
the usual simple text file handling like :
open (TEST, "> test.txt") || die "Error opening test.txt";
print TEST "hello";
close TEST || die "Error opening test.txt";

It depends on whether you expect other scripts or programs to be using
test.txt. If you *are* expecting this, you'll need to use flock.
Sorry for the somewhat off-topicness of this message... just a link to
a better newsgroup or a good read will suffice then :)

thx

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top