Linux Record Lock __linux

Source: Internet
Author: User
Tags flock

The function of a record lock is that when a process is reading or modifying a part of a file, it can prevent other processes from modifying the same file area. Record locks are actually
A byte range lock, because it locks only one area of the file, or it may be the entire file. 1. Basic Introduction

SVR3 adds the record lock function through the FCNTL function. The prototype of the FCNTL function has been given before, and this one is repeated here.

#include <fcntl.h>  

For record locks, CMD is f_getlk, f_setlk, or f_setlkw. The third parameter is a pointer to the flock structure.

struct flock{short  
    l_type//f_rdlck,f_wrlck, or F_unlck  
    off_t l_start.//offset in bytes, relative to L_whence
  short l_whence; Seek_set, seek_cur or seek_end  
    off_t l_len;//length in bytes; 0 means lock to EOF  
    pid_t l_pid; F_getlk  
};  

Note the following points about the lock unlocking area:
1.l_start is the relative offset, l_whence determines the starting point of the relative offset. This is similar to the last two parameters in the Lseek function.
2. The zone can begin at the end of the current file or over its end, but not before the start of the file.
3. 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. (that is, no matter how much data is added to the file, they are in the range of locks)
4. 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 description l_en is 0.

The read and write locks mentioned above are similar to the read and write locks in the thread mutex:

The following three commands for the FCNTL function are described below:
F_GETLK: Determine whether the lock described by FLOCKPTR will be rejected by another lock. If there is a lock, it blocks the creation of the lock described by Flockptr,
The information of the existing lock is written to the structure pointed to by flockptr, and if this is not the case, the flockptr points to the L_type except the Unlck.
The other information about the structure remains unchanged.
F_SETLK: Sets the lock described by Flockptr, and if an attempt is made to establish a lock (read lock or write lock), which cannot be permitted by the above compatibility rule, then fcntl immediately
Error returns, at which point errno is set to Eacces or Eagain. This command is also used to eliminate the locks described by flockptr (l_type as Unlock)
F_SETLKW: This is a blocked version of the F_SETLK, and if because there is already a lock in another process in one part of the requested interval, the compatibility
A rule that is requested by flockptr cannot be created, causes the calling process to hibernate and wakes up if the requested lock is already available or is dormant by a signal terminal.

When you set or release a lock on a file, the system groups or splits adjacent areas as required, for example, if the byte 100~199 is a lock area and you need to unlock the 150th byte, the kernel
Two locks will be maintained, one for byte 100~149 and the other for byte 151~199.

Assuming that we set a lock on the 150th byte, the system will merge three adjacent lock regions into one area, as shown in the first image of the previous figure. 2. Function call 2.1 Request and release a lock

To avoid assigning flock structures each time, and then filling in the information, you can use Lock_reg to handle all of these details:

int lock_reg (int fd, int cmd, int type, off_t offset, int whence, off_t len)
{
    struct lock;
    Lock.l_type = type; /* F_rdlck, F_WRLCK, F_unlck * *
    lock.l_start = offset;/* byte offset, relative to l_whence/lock.l_whence
    = whe nCE /* Seek_set, Seek_cur, seek_end/
    lock.l_len = len;/* #bytes (0 means to EOF)/return
    (Fcntl (fd, CMD, &lock ));
}

Use the following macro to invoke:

#define READ_LOCK (FD, offset, whence, len) \
    Lock_reg (FD), F_setlk, F_rdlck, (offset), (whence), (len)
#define Readw_lock (fd, offset, whence, len) \
    Lock_reg (FD), f_setlkw, F_rdlck, (offset), (whence), (len)
#define Write _lock (fd, offset, whence, len) \
    Lock_reg (FD), F_setlk, F_wrlck, (offset), (whence), (len)
#define Writew_lock (FD, offset, whence, len) \
    Lock_reg (FD), f_setlkw, F_wrlck, (offset), (whence), (len)
#define Un_lock (FD, offs ET, whence, len) \
    Lock_reg (FD), F_setlk, F_unlck, (offset), (whence), (Len)
2.2 Test a lock
pid_t lock_test (int fd, int type, off_t offset, int whence, off_t len)
{struct
    lock;
    Lock.l_type = type;  /* F_rdlck or F_wrlck/
    Lock.l_start = offset;/* byte offset, relative to l_whence
    /lock.l_whence = whence; *  Seek_set, Seek_cur, seek_end/
    lock.l_len = len;/* #bytes (0 means to EOF)/
    if (Fcntl (FD, F_GETLK, &lock) < 0)
        Err_sys ("Fcntl error");
    if (Lock.l_type = = F_unlck) return
        (0); * false, Region isn ' t locked by another proc
    /return (LOCK.L_PID);/* TR UE, return PID of lock owner */
}

If a lock exists, the function returns the process ID of the process holding the lock, or returns 0.
You typically invoke a function with the following macro:

#define IS_READ_LOCKABLE (FD, offset, whence, Len)
    (Lock_test (FD), F_rdlck, (offset), (whence), (len)) = = 0)
#define IS_WRITE_LOCKABLE (FD, offset, whence, Len)
    (Lock_test (FD), F_wrlck, (offset), (whence), (len)) = = 0)

Note: The process cannot use the Lock_test function to test whether it itself holds a lock on a part of the file.

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.