Linux file Lock flock

Source: Internet
Author: User
Tags flock mutex

In the process of simultaneous operation of the same file by multiple processes, it is very easy to cause data confusion in the file, need to lock operation to ensure the integrity of the data, the file-based lock described here, called "File lock"-flock.

Flock, the proposed lock, is not mandatory. A process uses flock to lock the file, another process can directly manipulate the file being locked, modify the data in the file, because flock is only used to detect whether the file is locked, for the file has been locked, the other process to write data, the kernel will not block the process of write operations, That is, the kernel processing strategy of the proposed lock.

Flock The main three types of operations:
Lock_sh, shared locks, multiple processes can use the same lock, often used as a read shared lock,
lock_ex, exclusive locks, while allowing only one process to use, often used as a write lock;
Lock_un, releasing the lock When the

process uses flock to try to lock a file, if the file is already locked by another process, the process is blocked until the lock is released, or when the flock is called, the LOCK_NB parameter is used, and when the file is attempted to be locked, the other service is locked and an error is returned. The errno error code is ewouldblock. This provides two modes of operation: blocking and non-blocking types. The

Service blocks the wait until the lock is freed: The
Flock (LOCKFD,LOCK_EX)
Service returns an error when the file is locked:
ret = Flock (lockfd,lock_ex| LOCK_NB)
Simultaneous ret =-1, errno = ewouldblock 

Flock the release of the lock is very special, you can call the Lock_un parameter to release the file lock, You can also release the file lock by turning off FD (the first parameter of flock is FD), which means that the flock is automatically freed as the process shuts down.

Flock One of the usage scenarios is: whether the detection process already exists;

int Checkexit (char* pfile) {    if (pfile = = NULL)    {           return-1;     }       int lockfd = open (PFILE,O_RDWR);    if (LOCKFD = =-1)     {           return-2;     }       int iret = Flock (lockfd,lock_ex| LOCK_NB);    if (iret = =-1)     {           return-3;     }       return 0;}

  

1. Overview of the scene

In the multi-threaded development, the mutex can be used to protect the critical resources and prevent inconsistent data, which is the most common use method. How do you handle synchronization between files in multiple processes? Let's take a look at the following figure:

The figure shows the process of updating the same file at the same time without synchronizing the two processes, the main operation being:

    • 1. Read the serial number from the file.
    • 2. Use this sequence number to complete the application-defined tasks.
    • 3. Increment the sequence number and write it back to the file.

It is known that two process reads have increased the number of reads, and write back to the file, but if there is mutual exclusion, the last value should be 1002 instead of the 1001 shown. To prevent this, Linux provides flock (for the entire file locking), Fcntl (locking for the entire file area) two functions for inter-process file synchronization. You can also use semaphores to accomplish the required synchronization, but it is usually better to use a file lock because the kernel can automatically associate locks with files.

2. Flock ()

Flock's statement is as follows

1234 #include <sys/file.h> //Returns 0 on success, or-1 on Error int flock (intfd, int operation);

The Fcntl () function provides a more powerful function than the function, and features that are also covered by flock (), but in some applications the flock () function is used, and in some semantics of inheritance and lock release flock () and fcntl () is still different.

The flock () system call is to lock the entire file by manipulating the file pointed to by the incoming FD, and then determining what to do with the value set by the operation parameter. Operation can be assigned the following values:

By default, if another process already holds an incompatible lock on the file, then flock () blocks. If you need to prevent this from occurring, you can take an OR (|) value in the operation parameter. In this case, if a process has already held an incompatible lock on the file, then flock () will block, instead it will return-1, and the errno will be set to Ewouldblock.

Any number of processes can hold a shared lock on a file at the same time, but only one process can hold the mutex on a file at any one time (this is a bit like a read-write lock). is the support for process A to set the lock after the lock is set, and process B:

Regardless of the mode in which the program opens the file (read, write, or read/write), a shared or mutex can be placed on the file. During the actual operation, the parameter operation can specify the corresponding value to convert the shared lock into a mutex (and vice versa). Converting a shared lock to a mutex is blocked if another process is acquiring a shared lock on the file, unless the operation parameter specifies the LOCK_NB tag, which is: (Lock_sh | LOCK_NB). The conversion of a lock is not an atomic operation, and in the process of conversion, the lock is removed first, and then a new lock is created.

3. The semantics of lock inheritance and release

Flock () releases a file lock based on the value of the operation parameter passed in to the Lock_un at the time of invocation. Additionally, the lock is automatically freed after the corresponding file descriptor is closed. Also, when a file descriptor is copied (DUP (), dup2 (), or a fcntl () F_DUPFD operation), the new file description inode references the same file lock.

123 < Span class= "CRAYON-E" >flock (fd, lock_ex) new_fd = dup (fd flock (new_fd,< Span class= "crayon-h" > lock_un)

This code first sets a mutex on the FD, then creates a new file descriptor new_fd with the same file through FD, and finally unlocks by NEW_FD. So we can learn that the new file descriptor points to the same lock. Therefore, if a lock is acquired through a particular file descriptor and one or more copies of the descriptor are created, the lock will not be released until the file descriptor copy is closed, if an unlock operation is not displayed.

From the above we can roll, if you use fork () to create a child process, the child process copies all the descriptors in the parent process, so that they also point to the same file lock. For example, the following code causes a child process to delete a lock on a parent process:

12345 flock (fd, lock_ex); if (0 = = fork ()) { flock (fd, lock_un); }

Therefore, it is sometimes possible to use these semantics to transfer a file lock from the parent process to the child process: after the fork (), the parent process closes its file descriptor, and the lock is only under the control of the child process. Locks created by fork () are retained in exec () (unless the close-on-exec tag is set on the file descriptor and the file descriptor is the last descriptor that references the underlying open file description).

If you use open () in your program to get a second descriptor that references the same file, flock () treats it as a different file descriptor. The following code will block on the second flock ().

12345 fd1 = open ("test.txt", o_rdwd); fd2 = open ("test.txt", o_rdwd); flock (fd1, lock_ex); flock (fd2, lock_ex);

4. Limitations of Flock ()

Flock () locks are placed with the following restrictions

    • Only the entire file can be locked. This coarse-grained lock limits concurrency between collaboration processes. If there are multiple processes, each of these processes wants to access different parts of the same file at the same time.
    • Only an advisory lock can be placed through flock ().
    • Many NFS implementations do not recognize locks placed by flock ().

Note: File locks are advised by default, which means that a process can simply ignore the locks placed on the file by another process. To make the exhortation lock model work properly, all processes that access the file must be matched by placing a lock before the file IO is executed.

Linux file Lock flock

Related Article

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.