mt2k has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Flocking
by Ovid (Cardinal) on Jun 22, 2000 at 21:42 UTC
    maleteen2000, yes, people gave you grief yesterday for your excessive questions in the Chatterbox. While, on one hand, it gets annoying that you keep asking basic questions that are already answered all over this site and others (in fact, the DBI questions you were asking yesterday were dealt with in a post ... yesterday), on the other hand, I do have to commend you on your perserverence in the face of adversity. I'll answer your question, but I'll also list a few resources that you can use to look things up on your own. First, I assume that the above code is pseudocode, because it's terrible. I'll give you the benefit of the doubt. Second, what you really want to do (at least, as far as I can tell from your example), is to modify a file in place with update mode ("+<").
    # always test for failure open (FILEHANDLE, "+< $filename") || die "Can't open $filename in upda +te mode: $!\n"; # read entire file into array of lines @array = <FILEHANDLE>; # do stuff to the file here # go back to beginning of file handle seek(FILEHANDLE, 0, 0) or die ("Seek failed on $filename: $!\n"); print FILEHANDLE @array or die ("Print failed on $filename: $!\n"); # truncate the file so we don't have excess garbage at the end truncate(FILEHANDLE, tell(FILEHANDLE)) or die ("Truncate failed on $fi +lename: $!\n"); close (FILEHANDLE) or die ("Close failed on $filename: $!\n");
    This code allows you to open the file, read its contents, modify them, and write them back out without having to close and reopen. I haven't tested it with flocking, so you'll need to play with it to see what type of flock works.

    Please note that flocking a file doesn't stop others from playing with it. They can still modify the file if they don't check to see if it's flocked.

    Further resources:

    • I don't know if you're aware of this, but perlmonks has a lot of useful information that can be read.
    • Perl.com
    • CPAN
    Please don't take this as a flame, but people will be much more willing to help you if it looks like you've done your homework first.

    Cheers!

    PS: I took the above code straight from the Perl Cookbook. Beg your parents for a copy.

Re: Flocking
by chromatic (Archbishop) on Jun 22, 2000 at 21:48 UTC
    The polite approach has you request a shared lock for reading and an exclusive lock for writing. (That's LOCK_SH and LOCK_EX, respectively.)

    On a non-braindead operating system, many processes can simultaneously acquire a shared lock on a file, but an exclusive lock can only be held by one, and it usually blocks until all other locks (including shared locks) are released.

    You have a couple of options. You could open the file for read/write access with an exclusive lock, at the beginning of your program. That's conceptually simple, but you'll have to "rewind" it after reading, if you want to overwrite it. (See seek.) It also has the drawback that if another process has any lock on the file, your program will block (unless you work around it). This is the technique I'd choose.

    You could also continue with the code above. If you don't close the filehandle explicitly, Perl will do that for you implicitly. Closing a filehandle has the side effects of flushing buffers and unlocking any locks on it.

    I doubt your code to open the filehandle to a different file and acquire an exclusive lock is an atomic operation. (It probably takes more than one processor cycle to accomplish, so there is a possibility another process could jump in there and grab a lock. Of course, any process that doesn't check for locks could do that anyway.)

    The best advice I can think of is to make sure that any other process that might access the file also use the same flock technique you decide on.

    Disclaimer: I'm not as smart as I seem, sometimes.

RE: Flocking
by flyfishin (Monk) on Jun 22, 2000 at 21:44 UTC
    Why not just open the file for read/write with +<, +>, or +>>? So you would have
    open FILE, "+>>file.txt" or open FILE, "+<file.txt" or open FILE, "+>file.txt"
Re: Flocking
by mt2k (Hermit) on Jun 22, 2000 at 21:54 UTC
    I must admit that from Ovid's post, I did not know that a script had to check to see if the file is locked
    I thought it was automatic that a file was actually locked and that the interpreter checked to see if the file had been locked.
    How, then, can I check to see if a file is locked?
      flock blocks until the lock is acquired. That is, LOCK_EX doesn't return until all current shared locks are released, and LOCK_SH doesn't return until any exclusive lock is released. Normally, you don't check to see if a file has been locked, you simply ask for the lock and go about your business.

      This is all in the perlfunc manpage, though the 5.6 docs are clearer...

      Two potentially non-obvious but traditional flock
      semantics are that it waits indefinitely until the
      lock is granted, and that its locks merely
      advisory.  Such discretionary locks are more
      flexible, but offer fewer guarantees.  This means
      that files locked with flock may be modified by
      programs that do not also use flock.