Linux System file I/O programming (ii) file lock function

Source: Internet
Author: User

Previous section: http://blog.csdn.net/mybelief321/article/details/8989755 5 basic function Functions open (), read (), write (), Lseek (), and Close () The implementation of the file open, read/write operations, this section will discuss how the file has been shared, that is, when multiple users to use, manipulate a file. At this point, Linux is often used to lock files to avoid the competing state of shared resources.

File locks include recommended locks and mandatory locks. The proposed lock requires that each locked file process check for a lock and respect the existing lock. In general, the kernel and the system do not use a recommended lock. A mandatory lock is a lock executed by the kernel, which prevents any other file from reading and writing when a file is locked for writing. The use of mandatory locks has a great impact on performance, each read and write must check whether there is a lock.

In Linux, a file-locked function has LOCKF () and Fcntl (), where LOCKF () is used to impose a recommended lock on the file, while Fcntl () can impose a mandatory lock as well as a recommended lock. At the same time, fcntl () can also lock a record of a file, that is, record the lock.

Record locks can be divided into read locks and write locks, where read locks are also known as shared locks, which enable multiple processes to establish read locks in the same part of the file. A write lock is also known as an exclusion lock, and only one process can establish a write lock in a certain part at any time. Of course, both read and write locks cannot be established in the same part of the file.

The Fcntl () function is rich in functionality that allows you to perform various operations on an open file descriptor, including not only managing file locks, but also getting a set of file descriptors and file descriptor flags, and copying file descriptors! This time I'm going to take a look at the Fcntl () function to create a file lock, about its other usage ... Let's learn this first.

Fcntl () function format

The lock in table 1 is a flock structure with the following structure:

The off_t in the figure above is the data type long int; pid_t is the data type int, and there's an explanation: dot this explanation

So the value of each variable in lock of this structure is as follows table 2

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.