UNIX Environment Advanced Programming: Log Lock (Fcntl function) and deadlock detection

Source: Internet
Author: User
Tags flock range

Records lock record Locking

Function: When a process is reading or modifying a part of a file, it can prevent other processes from modifying the same file area.

BYTE range lock Byte-range locking

Second, history

Flock function, you can lock the entire file, you cannot lock a part of the file.

Fcntl function, which increases the function of recording locks.

The LOCKF function, which constructs the LOCKF function on the basis of FCNTL, provides a simplified interface. An area that can lock any number of bytes in a file

Third, Fcntl record lock

Function Prototypes:

int fcntl (int fd, int cmd, struct flock *flockptr); 
/* cmd = f_getlk, test can establish a lock 
cmd = f_setlk, set the lock 
cmd = f_setlkw, blocking set a lock
// POSIX only defines the Fock structure must have the following data members, the implementation can be increased  
struct Flock {short  
      l_type;    /* Lock type: F_rdlck, F_wrlck, F_unlck * * short
      l_whence;  /* The starting position of the lock: Seek_set, Seek_cur, seek_end * *
      off_t L_start;   /* Lock start offset, relative to l_whence * * *
      off_t L_len;     * * Locked bytes
      /pid_t l_pid;     /* has occupied the lock PID (only to f_getlk command valid)/
      /*...*/
};

(1) desired lock type: F_RDLCK (Shared read lock), F_WRLCK (exclusive write lock), F_unlck (unlock a region).

(2) The starting byte offset to be locked or unlocked is determined by both L_start and l_whence.

(3) Note: The range can begin at the end of the current file or over its end, but not before the start of the file.

(4) If L_len is 0, the area that represents the lock starts at its starting point (determined by L_start and l_whence) until the maximum possible offset.

(5) In order to lock the entire file, we set the L_start and l_whence so that the starting point of the lock is at the beginning of the file and the length (L_len) is 0.

Two types of locks are mentioned: shared read locks (F_RDLCK) and exclusive write locks (f_wrlck), and the basic rules are:

Multiple processes can have a shared read lock on a given byte, but there can be only one write lock for a single process on a given byte. Further, if you have one or more read locks on a given byte, you cannot write a lock on that byte; If you already have an exclusive write lock on a byte, you cannot add any read locks to it.

Read-write lock rules for different process lock requests:

-------------------read lock plus write lock

No lock allow allowed

One or more read locks allow deny

A write lock refuses to reject

Note: The above rule applies to lock requests made by different processes and does not apply to multiple lock requests made by a single process. When a single process presents multiple lock requests, with the last lock as the standard, that is, the new lock replaces the old lock. For the same file, if a different process continues to add a read lock to it, other processes that want to add blocking write locks to it may extend the wait time (in which case starvation will occur for the process that adds the write lock).

The descriptor must be read open when the lock is read, and the descriptor must be write-open when the lock is written.

F_GETLK: Determine whether the lock described by FLOCKPTR will be rejected by another lock (blocking). If there is a lock that prevents the creation of the lock described by FLOCKPTR, the information for the existing lock is written to the FLOCKPTR point structure. If this is not the case, other information in the structure that flockptr points to will remain unchanged in addition to setting L_type to F_unlck.

F_setlk: Gets (L_type to F_rdlck or f_wrlck) or releases the lock described by flockptr to the flock structure, which returns a eaccess or eagain error immediately without blocking if the lock cannot be acquired.

The difference between f_setlkw:f_setlkw and f_setlk is that when a lock cannot be set, the calling thread blocks to the location where the lock can be authorized.

It should be noted here that the F_GETLK test can be used to establish a lock, and then use F_SETLK or f_setlkw attempt to establish a lock, since the two are not an atomic operation, so there is no guarantee that two times fcntl will not have another process to insert and establish a related lock, So that the initial test condition is invalid. So generally do not want to lock when blocking, will be directly through the call F_setlk, and the return results are tested to determine whether the required lock was successfully established.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.