Is it needed to lock open file for write?

Discussion in 'Perl Misc' started by news.t-com.hr, Dec 1, 2005.

  1. Is it needed to lock open file for write?

    use Fcntl qw:)flock);
    open(FH, ">codes.txt") or die "can't open codes.txt $!";
    flock(FH, LOCK_EX) or die "can't flock codes.txt $!";

    Or is also good only to use command open

    open(FH, ">codes.txt") or die "can't open codes.txt $!";


    TNX
    news.t-com.hr, Dec 1, 2005
    #1
    1. Advertising

  2. news.t-com.hr wrote:
    > Is it needed to lock open file for write?
    >
    > use Fcntl qw:)flock);
    > open(FH, ">codes.txt") or die "can't open codes.txt $!";
    > flock(FH, LOCK_EX) or die "can't flock codes.txt $!";
    >
    > Or is also good only to use command open
    >
    > open(FH, ">codes.txt") or die "can't open codes.txt $!";
    >


    AFAIK, locking is really only necessary if there is a possibility of
    two processes operating on the same file at the same time--in your case
    the operation would be a file write.
    it_says_BALLS_on_your forehead, Dec 1, 2005
    #2
    1. Advertising

  3. news.t-com.hr

    Rhugga Guest

    The only problem with file locking is that not all utilities/commands
    honor file locking.

    For example, let's say your perl program is running and has an
    exlcusive lock on /etc/password.

    Then let's say, for example, that yppasswd doesn't honor exclusive
    locks, and just does it's thing regardless. The user jsmith decides to
    change his password while your program is doing it's thing. You now run
    into the risk of 1) corrupting the file completely or 2) losing your
    changes to the file, or 3) the user's password change being lost.

    You'd be surprised at the number of standard commands that don't honor
    exclusive locks. I think these commands are become more rare, but all
    it takes is 1 to make things go pear shaped. For Solaris, somewhere on
    bigadmin or sunsolve there is a list of commands and/or libraries that
    fall into this category. I think the list has been decreasing over the
    years but I'm sure there are still some.

    This is one reason why its recommended when tweaking /etc/passwd or
    /etc/shadow from a script that you make a copy of the file, modify the
    copy, the move the modifed copy into place. (even this has its risks
    but they are very minimized)

    In your case, however, if your code will be the only one ever modifying
    the file, and your locking just to prevent 2+ running copies from
    stepping on each other, than it would work for you.
    Rhugga, Dec 1, 2005
    #3
    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. Fuzzyman
    Replies:
    3
    Views:
    486
    Andrew MacIntyre
    Dec 5, 2003
  2. Robert Brewer
    Replies:
    0
    Views:
    485
    Robert Brewer
    Dec 5, 2003
  3. k3xji
    Replies:
    7
    Views:
    804
    Gabriel Genellina
    Dec 30, 2008
  4. nano2k

    Application.Lock()/UnLock() or lock(Application)

    nano2k, Jul 23, 2007, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    275
    nano2k
    Aug 9, 2007
  5. Iulian Ilea
    Replies:
    1
    Views:
    306
    pcx99
    Dec 21, 2006
Loading...

Share This Page