Linux file lock details (underlying operations of design files)

Source: Internet
Author: User
Tags flock
In linux low-level file programming, file locks are an important part of multi-user multi-task operating systems. When updating a file, you expect to use a mechanism to prevent both processes from simultaneously updating the file in the same region, or prevent the file content from being read when it is not updated... information & n

In linux low-level file programming
File locks are an important part of multi-user multi-task operating systems. When updating files, you expect to use a mechanism to prevent the loss of files in the same region when both processes update the files at the same time, or prevent the file content from being read when it is not updated. this mechanism is a file lock.
During file operations, a process can use a file lock to lock the sensitive part of the file to prevent other processes from unauthorized operations on the part of the data. The fcntl function provides the ability to lock any part of the file, which can lock all files and some records of the file. Therefore, the file lock becomes a "record Lock ".
You can distinguish between the read lock and write lock based on the access method of the file lock. A read lock, also known as a shared lock, is used to prevent the file records read by the process from being changed. Multiple read locks can be set for file records at the same time. However, when one read lock exists, the write lock cannot be set for this record.
The write lock, also known as mutex, is used to ensure that file changes are not disturbed, ensure file consistency and integrity, and prevent write loss or read "dirty" data. Once a write lock is set for a file record, no more lock can be set unless the write lock is in contact.
Multiple read locks can be set for file records at the same time. Only one write lock can be set for a single record, and the read and write locks cannot coexist.
When the fntl function is dedicated to lock operations, its prototype is:
Int fcntl (int fildes, int cmd, struct flock * arg );
Here, the structure flock is used to describe the file lock information, defined in "fcntl. h", as shown below:
 
1. struct flock
2 .{
3. short l_type;/* lock type. values: F_RDLCK, F_WRLCK, or F_UNLCK, which respectively indicate applying for read lock, applying for write lock, and releasing Lock */
4. short l_whence;/* the relative location of the start address of the lock area, similar to the whence parameter in lseek. The value is one of SEEK_SET SEEK_CUR SEEK_END, indicates the start position, current position, and end position of the relative File */
5. long l_start;/* start address offset of the lock region, which is determined by l_whence */
6. long l_len;/* length of the lock. 0 indicates that the lock is reached the end of the file */
7. long l_pid;/* id of the locked process */
8 .};
When the function fcntl is used for locking, the cmd parameter has three values:
F_GETLK
F_SETLK
F_SETLKW
{File locks are typically used in two aspects: the critical data in a locked file, such as the number of votes recorded in the file during concurrent voting; the second is the use of mutually exclusive write locks, implement process Concurrency Control}
2) file lock operation
In the use of the lock mechanism, the most common operations include lock requests, release and testing. these operations are basically similar
The following functions are encapsulated by the author:
(1) test the lock
The design function SeeLock is used to query the lock information of the file corresponding to the file descriptor fd. its prototype is:
Void SeeLock (int fd, int start, int len );
Function query the lock information in len bytes starting from the offset start of the file corresponding to the descriptor fd
1./* ---- test source code ---- lock1.c ----*/
2. void SeekLock (int fd, int start, int len)
3 .{
4. struct flock arg;
5. arg. l_type = F_WRLCK;
6. arg. l_whence = SEEK_SET;
7. arg. l_start = start;
8. arg. l_len = len;
9. if (fcntl (fd, F_GETLK, & arg) =-1)
10. fprintf (stderr, "See Lock failed./n ");
11. else if (arg. l_type = F_UNLCK)
12. fprintf (stderr, "no lock from % d TO % d, n", start, len );
13. else if (arg. l_type = F_WRLCK)
14. fprintf (stderr, "write lock from % d TO % d, id = % d/n", start, len, arg. l_pid );
15. else if (arg. l_type = F_RDLCK)
16. fprintf (stderr, "read lock from % d To % d, id = % d/n", start, len, arg. l_pid );
17 .}
(2) apply for a read lock
GetReadLock, a shared lock application function, is prototype:
Void GetReadLock (int fd, int start, int len );
Apply for a shared read lock in the file descriptor fd corresponding to the file in blocking mode. len bytes starting from the offset start in the locked area
1./* --- block requests for shared read lock source code --- lockl. c */
2. void GetReadLock (int fd, int start, int len)
3 .{
4. struct flock arg;
5. arg. l_type = F_RDLCK;
6. arg. l_whence = SEEK_SET;
7. arg. l_start = start;
8. arg. l_len = len;
9. if (fcntl (fd, F_SETLKW, & arg) =-1)
10. fprintf (stderr, "[% d] Set Read Lock failed./n", getpid ());
11. else
12. fprintf (stderr, "[% d] Set Read Lock From % d To % d", getpid (), start, len );
13 .}
(3) apply for a write lock
Void GetWriteLock (int fd, int start, int len );
1./* --- block requests for shared write lock source code --- lockl. c */
2. void GetReadLock (int fd, int start, int len)
3 .{
4. struct flock arg;
5. arg. l_type = F_WRLCK;
6. arg. l_whence = SEEK_SET;
7. arg. l_start = start;
8. arg. l_len = len;
9. if (fcntl (fd, F_SETLKW, & arg) =-1)
10. fprintf (stderr, "[% d] Set Write Lock failed./n", getpid ());
11. else
12. fprintf (stderr, "[% d] Set Write Lock From % d To % d", getpid (), start, len );
13 .}
(4) release the lock
Design the file lock release function ReleaseLock, prototype:
Void ReleaseLock (int fd, int start, int len );
1./* --- release lock source code --- lockl. c */
2. void GetReadLock (int fd, int start, int len)
3 .{
4. struct flock arg;
5. arg. l_type = F_UNLCK;
6. arg. l_whence = SEEK_SET;
7. arg. l_start = start;
8. arg. l_len = len;
9. if (fcntl (fd, F_SETLKW, & arg) =-1)
10. fprintf (stderr, "[% d] UnLock failed./n", getpid ());
11. else
12. fprintf (stderr, "[% d] UnLock From % d To % d", getpid (), start, len );
13 .}
The following is an example of a file lock control process:
1. # include
2. # include
3. void main ()
4 .{
5. int fd;
6. struct flock arg;
7. if (fd = open ("/tmp/tlockl", O_RDWR | O_CREAT, 0755) <0)
8 .{
9. fprintf (stderr, "open file failed./n ");
10. retrun;
11 .}
12. SeeLock (fd, 0, 10 );
13. GetReadLock (fd,);/* apply for a read lock */
14. SeeLock (fd, 11,20 );
15. GetWriteLock (fd, 11,20);/* apply for a write lock */
16. sleep (30 );
17. ReleaseLock (fd, 0, 10 );
18. ReleaseLock (fd, 11,20 );
19 .}
 
Author "pstary"

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.