Advanced Programming in UNIX Environment Episode 71

本文深入探讨了UNIX系统中记录锁定(Record Locking)的概念及其重要性,特别是在数据库系统等需要确保文件唯一写入权限的应用场景中。记录锁定允许进程防止其他进程修改文件的特定区域,确保数据的一致性和安全性。
Record Locking

In most UNIX systems, the final state of the file corresponds to the last process that wrote the file. In some applications, however, such as a database system, a process needs to be certain that it alone is writing to a file. To provide this capability for processes that need it, commercial UNIX systems provide record locking.

Record locking is the term normally used to describe the ability of a process to prevent other processes from modifying a region of a file while the first process is reading or modifying that portion of the file. Under the UNIX System, ‘‘record’’ is a misnomer; the UNIX kernel does not have a notion of records in a file. A better term is byte-range locking, given that it is a range of a file (possibly the entire file) that is locked.

Record locking was originally added to Version 7 in 1980 by John Bass. The system call entry into the kernel was a function named locking. This function provided mandatory record locking and propagated through many versions of System III. Xenix systems picked up this function, and some Intel-based System V derivatives, such as OpenServer 5, continued to support it in a Xenix-compatibility library.

fcntl Record Locking
#include <fctnl.h

int fcntl(int fd, int cmd, .../*struct flock *flockptr */);

The third argument (which we’ll call flockptr) is a pointer to an flock structure.


struct flock {
	short l_type; /* F_RDLCK, F_WRLCK, or F_UNLCK */
	short l_whence; /* SEEK_SET, SEEK_CUR, or SEEK_END */
	off_t l_start; /* offset in bytes, relative to l_whence */
	off_t l_len; /* length, in bytes; 0 means lock to EOF */
	pid_t l_pid; /* returned with F_GETLK */
};

This structure describes

  • The type of lock desired: F_RDLCK (a shared read lock), F_WRLCK (an exclusive write lock), or F_UNLCK (unlocking a region)
  • The starting byte offset of the region being locked or unlocked (l_start and l_whence)
  • The size of the region in bytes (l_len)
  • The ID (l_pid) of the process holding the lock that can block the current process (returned by F_GETLK only)

Numerous rules apply to the specification of the region to be locked or unlocked.

  • The two elements that specify the starting offset of the region are similar to the last two arguments of the lseek function (Section 3.6). Indeed, the l_whence member is specified as SEEK_SET, SEEK_CUR, or SEEK_END.
  • Locks can start and extend beyond the current end of file, but cannot start or extend before the beginning of the file.
  • If l_len is 0, it means that the lock extends to the largest possible offset of the file. This allows us to lock a region starting anywhere in the file, up through and including any data that is appended to the file. (We don’t have to try to guess how many bytes might be appended to the file.)
  • To lock the entire file, we set l_start and l_whence to point to the beginning of the file and specify a length (l_len) of 0. (There are several ways to specify the beginning of the file, but most applications specify l_start as 0 and l_whence as SEEK_SET.)

We can now describe the three commands for the fcntl function.

F_GETLK

Determine whether the lock described by flockptr is blocked by some other lock.

F_SETLK

Set the lock described by flockptr.

Although POSIX allows an implementation to return either error code, all four implementations described in this text return EAGAIN if the locking request cannot be satisfied.

F_SETLKW

This command is a blocking version of F_SETLK.

Note that POSIX.1 doesn’t specify what happens when one process read locks a range of a file, a second process blocks while trying to get a write lock on the same range, and a third processes then attempts to get another read lock on the range.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值