PERLFUNC: flock - lock an entire file with an advisory lock 
Author Message
 PERLFUNC: flock - lock an entire file with an advisory lock

(This excerpt is from the perlfunc manpage, part of the standard set of
documentation included with every valid Perl distribution--like the one on
your own system.  See also http://www.*-*-*.com/
or http://www.*-*-*.com/
if your negligent system adminstrator has been remiss in his duties.)

NAME
    flock - lock an entire file with an advisory lock

SYNOPSIS
    flock FILEHANDLE,OPERATION

DESCRIPTION
    Calls flock(2), or an emulation of it, on FILEHANDLE. Returns TRUE
    for success, FALSE on failure. Produces a fatal error if used on a
    machine that doesn't implement flock(2), fcntl(2) locking, or
    lockf(3). `flock' is Perl's portable file locking interface,
    although it locks only entire files, not records.

    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'.
    See the perlport manpage, your port's specific documentation, or
    your system-specific local manpages for details. It's best to
    assume traditional behavior if you're writing portable programs.
    (But if you're not, you should as always feel perfectly free to
    write for your own system's idiosyncrasies (sometimes called
    "features"). Slavish adherence to portability concerns shouldn't
    get in the way of your getting your job done.)

    OPERATION is one of LOCK_SH, LOCK_EX, or LOCK_UN, possibly combined
    with LOCK_NB. These constants are traditionally valued 1, 2, 8 and
    4, but you can use the symbolic names if import them from the Fcntl
    module, either individually, or as a group using the ':flock' tag.
    LOCK_SH requests a shared lock, LOCK_EX requests an exclusive lock,
    and LOCK_UN releases a previously requested lock. If LOCK_NB is
    added to LOCK_SH or LOCK_EX then `flock' will return immediately
    rather than blocking waiting for the lock (check the return status
    to see if you got it).

    To avoid the possibility of miscoordination, Perl now flushes
    FILEHANDLE before locking or unlocking it.

    Note that the emulation built with lockf(3) doesn't provide shared
    locks, and it requires that FILEHANDLE be open with write intent.
    These are the semantics that lockf(3) implements. Most if not all
    systems implement lockf(3) in terms of fcntl(2) locking, though, so
    the differing semantics shouldn't bite too many people.

    Note also that some versions of `flock' cannot lock things over the
    network; you would need to use the more system-specific `fcntl' for
    that. If you like you can force Perl to ignore your system's
    flock(2) function, and so provide its own fcntl(2)-based emulation,
    by passing the switch `-Ud_flock' to the Configure program when you
    configure perl.

    Here's a mailbox appender for BSD systems.

        use Fcntl ':flock'; # import LOCK_* constants

        sub lock {
            flock(MBOX,LOCK_EX);
            # and, in case someone appended
            # while we were waiting...
            seek(MBOX, 0, 2);
        }

        sub unlock {
            flock(MBOX,LOCK_UN);
        }

        open(MBOX, ">>/usr/spool/mail/$ENV{'USER'}")
                or die "Can't open mailbox: $!";

        lock();
        print MBOX $msg,"\n\n";
        unlock();

    On systems that support a real flock(), locks are inherited across
    fork() calls, whereas those that must resort to the more capricious
    fcntl() function lose the locks, making it harder to write servers.

    See also the DB_File manpage for other flock() examples.

--
Imagination is more important than knowledge --Albert Einstein



Wed, 12 Dec 2001 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. PERLFUNC: flock - lock an entire file with an advisory lock

2. Best way to do advisory file locking?

3. PERLFUNC: lock - get a thread lock on a variable, subroutine, or method

4. PERLFUNC: lock - get a thread lock on a variable, subroutine, or method

5. file locking (flock)

6. file locking without usinf flock ft.

7. file locking without flock

8. File Locking NOT using flock

9. File Locking with flock

10. flock not locking the file

11. Locking files -- 'flock'/'lockf/ question.

12. ".lock"-style locking module?

 

 
Powered by phpBB® Forum Software