[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] file-locking algorithm



Reading or writing?  The test I attached does both.  It is a very simple
matter to test such a thing.


>Although I am designing and testing this Perl program on a Macintosh, I
>plan to eventually put it on an Unix server. As a result, the file-locking
>should also work on Unix, if possible.

flock() is unimplemented on MacPerl, I believe.  As stated, you cannot have
multiple writes to one file, so flock() is largely unnecessary.  If you
must have something like a flock(), do this:

        $f = "filelock";
        sub lockfile   {open(F,">$f") || die "can't create: $!\n";close F}
        sub unlockfile {unlink $f}

        &lockfile;
        print "Locked!\n" if (-e $f);
        &unlockfile;
        print "Unlocked!\n" if (! -e $f);

Then before you do anything that would require a locked file, just check to
see if your lockfile exists (-e $f).  And you can even "wait" for a file to
become available:

        $f = "filelock";
        sub lockfile   {open(F,">$f") || die "can't create: $!\n";close F}
        sub unlockfile {unlink $f}

        &lockfile;
        print "Locked!\n" if (-e $f);
        sleep while (-e $f);
        print "Unlocked!\n" if (! -e $f);

While it is sleeping, go manually remove the lockfile (drag it to the
trash).  The script will then complete.

--
Chris Nandor                 pudge@pobox.com                 http://pudge.net/
%PGPKey=('B76E72AD',[1024,'08 24 09 0B CE 73 CA 10  1F F7 7F 13 81 80 B6 B6'])





Attachment converted: catnip:testing.pl (TEXT/McPL) (000029CB)