Loading ...
Sorry, an error occurred while loading the content.

RE: [PBML] read/write/flock question

Expand Messages
  • Charles K. Clarkson
    ... Read perlopentut File Locking . ... Can you rephrase that? It doesn t make sense to me. HTH, Charles K. Clarkson -- Mobile Homes Specialist 254 968-8328
    Message 1 of 10 , Dec 4, 2004
    • 0 Attachment
      Luinrandir Hernsen <Luinrandir@...> wrote:

      : I had this thought... (ok.. duct tape your collective heads,
      : perl addicts!) i know this is not the correct way, but I was
      : wondering if there was a way a open file command could be told
      : to wait untill the flock was clear to allow the next person
      : into the file??? I thought is using an on error type of command
      : but i'm not sure how to loop it.. I would also need to put a 5
      : sec timeout too, so it doesn't become an infinite loop. Again I
      : know this is not perl code.. I am seeking the logic on how this
      : is done and suggestions on the code.

      Read perlopentut 'File Locking'.


      : I also need the write to file version of this.

      Can you rephrase that? It doesn't make sense to me.

      HTH,

      Charles K. Clarkson
      --
      Mobile Homes Specialist
      254 968-8328
    • Luinrandir Hernsen
      ... where? this makes no sence to me.... Lou
      Message 2 of 10 , Dec 4, 2004
      • 0 Attachment
        >
        > Read perlopentut 'File Locking'.
        >

        where? this makes no sence to me....

        Lou
      • Charles K. Clarkson
        ... The perl documentation comes with in a bunch of files. One of those files is named perlopentut . In that file is a section named File Locking . In that
        Message 3 of 10 , Dec 4, 2004
        • 0 Attachment
          Luinrandir Hernsen <Luinrandir@...> wrote:

          : : Read perlopentut 'File Locking'.
          : :
          :
          : where? this makes no sence to me....

          The perl documentation comes with in a bunch of files.
          One of those files is named "perlopentut". In that file is
          a section named 'File Locking'. In that section is a few
          examples of how to use flock() and why.

          I installed Activeperl on my system (windows XP). I
          installed it on my C: drive in a directory named Perl.
          In my browser, if I go to this address "perlopentut" is
          available there as an HTML file.

          file:///C:/Perl/html/index.html

          The same documentation is available online as HTML
          files at http://www.perldoc.com/.


          HTH,

          Charles K. Clarkson
          --
          Mobile Homes Specialist
          254 968-8328
        • Luinrandir Hernsen
          ok... i found this..... and now i am more confused...... nothing like knowing how much i don t know! File Locking In a multitasking environment, you may need
          Message 4 of 10 , Dec 4, 2004
          • 0 Attachment
            ok... i found this.....
            and now i am more confused......
            nothing like knowing how much i don't know!


            File Locking

            In a multitasking environment, you may need to be careful not to col-
            lide with other processes who want to do I/O on the same files as you
            are working on. You'll often need shared or exclusive locks on files
            for reading and writing respectively. You might just pretend that
            only
            exclusive locks exist.

            Never use the existence of a file "-e $file" as a locking indication,
            because there is a race condition between the test for the existence
            of
            the file and its creation. It's possible for another process to
            create
            a file in the slice of time between your existence check and your
            attempt to create the file. Atomicity is critical.

            Perl's most portable locking interface is via the "flock" function,
            whose simplicity is emulated on systems that don't directly support
            it
            such as SysV or Windows. The underlying semantics may affect how it
            all works, so you should learn how "flock" is implemented on your
            sys-
            tem's port of Perl.

            File locking does not lock out another process that would like to do
            I/O. A file lock only locks out others trying to get a lock, not
            pro-
            cesses trying to do I/O. Because locks are advisory, if one process
            uses locking and another doesn't, all bets are off.

            By default, the "flock" call will block until a lock is granted. A
            request for a shared lock will be granted as soon as there is no
            exclu-
            sive locker. A request for an exclusive lock will be granted as soon
            as there is no locker of any kind. Locks are on file descriptors,
            not
            file names. You can't lock a file until you open it, and you can't
            hold on to a lock once the file has been closed.

            Here's how to get a blocking shared lock on a file, typically used
            for
            reading:

            use 5.004;
            use Fcntl qw(:DEFAULT :flock);
            open(FH, "< filename") or die "can't open filename: $!";
            flock(FH, LOCK_SH) or die "can't lock filename: $!";
            # now read from FH

            You can get a non-blocking lock by using "LOCK_NB".

            flock(FH, LOCK_SH | LOCK_NB)
            or die "can't lock filename: $!";

            This can be useful for producing more user-friendly behaviour by
            warn-
            ing if you're going to be blocking:

            use 5.004;
            use Fcntl qw(:DEFAULT :flock);
            open(FH, "< filename") or die "can't open filename: $!";
            unless (flock(FH, LOCK_SH | LOCK_NB)) {
            $| = 1;
            print "Waiting for lock...";
            flock(FH, LOCK_SH) or die "can't lock filename: $!";
            print "got it.\n"
            }
            # now read from FH

            To get an exclusive lock, typically used for writing, you have to be
            careful. We "sysopen" the file so it can be locked before it gets
            emp-
            tied. You can get a nonblocking version using "LOCK_EX | LOCK_NB".

            use 5.004;
            use Fcntl qw(:DEFAULT :flock);
            sysopen(FH, "filename", O_WRONLY | O_CREAT)
            or die "can't open filename: $!";
            flock(FH, LOCK_EX)
            or die "can't lock filename: $!";
            truncate(FH, 0)
            or die "can't truncate filename: $!";
            # now write to FH

            Finally, due to the uncounted millions who cannot be dissuaded from
            wasting cycles on useless vanity devices called hit counters, here's
            how to increment a number in a file safely:

            use Fcntl qw(:DEFAULT :flock);

            sysopen(FH, "numfile", O_RDWR | O_CREAT)
            or die "can't open numfile: $!";
            # autoflush FH
            $ofh = select(FH); $| = 1; select ($ofh);
            flock(FH, LOCK_EX)
            or die "can't write-lock numfile: $!";

            $num = <FH> || 0;
            seek(FH, 0, 0)
            or die "can't rewind numfile : $!";
            print FH $num+1, "\n"
            or die "can't write numfile: $!";

            truncate(FH, tell(FH))
            or die "can't truncate numfile: $!";
            close(FH)
            or die "can't close numfile: $!";
          Your message has been successfully submitted and would be delivered to recipients shortly.