file locking : does the os do this ?

Discussion in 'Perl Misc' started by Jazeker, Mar 5, 2005.

  1. Jazeker

    Jazeker Guest

    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
    --
    print <<EOF;
    Just a noobish Perl hacker
    EOF
    Jazeker, Mar 5, 2005
    #1
    1. Advertising

  2. Jazeker wrote:

    > 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?"
    Chris Mattern, Mar 5, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Praveen
    Replies:
    1
    Views:
    746
    Kevin Yu [MSFT]
    Apr 16, 2005
  2. Timasmith
    Replies:
    4
    Views:
    452
    Bjorn Borud
    Nov 1, 2006
  3. Praveen
    Replies:
    5
    Views:
    171
    Kevin Yu [MSFT]
    Apr 20, 2005
  4. Ludwigi Beethoven
    Replies:
    5
    Views:
    320
    Mike Hall
    Jul 26, 2003
  5. sonet
    Replies:
    9
    Views:
    127
Loading...

Share This Page