檔案操作之flock
Calls flock(2), or an emulation of it, on FILEHANDLE. Returns true for success, false on failure. Produces a fatal error if used on a machine that doesn't implement flock(2), fcntl(2) locking, or lockf(3). flock
is Perl's portable file locking interface, although it locks only entire files, not records.
Two potentially non-obvious but traditional flock
semantics are that it waits indefinitely until the lock is granted, and that its locks merely advisory. Such discretionary locks are more flexible, but offer fewer guarantees. This means that programs that do not also use flock
may modify files locked with flock
. See perlport, your port's specific documentation, or your system-specific local manpages for details. It's best to assume traditional behavior if you're writing portable programs. (But if you're not, you should as always feel perfectly free to write for your own system's idiosyncrasies (sometimes called "features"). Slavish adherence to portability concerns shouldn't get in the way of your getting your job done.)
OPERATION is one of LOCK_SH, LOCK_EX, or LOCK_UN, possibly combined with LOCK_NB. These constants are traditionally valued 1, 2, 8 and 4, but you can use the symbolic names if you import them from the Fcntl module, either individually, or as a group using the ':flock' tag. LOCK_SH requests a shared lock, LOCK_EX requests an exclusive lock, and LOCK_UN releases a previously requested lock. If LOCK_NB is bitwise-or'ed with LOCK_SH or LOCK_EX then flock
will return immediately rather than blocking waiting for the lock (check the return status to see if you got it).
To avoid the possibility of miscoordination, Perl now flushes FILEHANDLE before locking or unlocking it.
Note that the emulation built with lockf(3) doesn't provide shared locks, and it requires that FILEHANDLE be open with write intent. These are the semantics that lockf(3) implements. Most if not all systems implement lockf(3) in terms of fcntl(2) locking, though, so the differing semantics shouldn't bite too many people.
Note that the fcntl(2) emulation of flock(3) requires that FILEHANDLE be open with read intent to use LOCK_SH and requires that it be open with write intent to use LOCK_EX.
Note also that some versions of flock
cannot lock things over the network; you would need to use the more system-specific fcntl
for that. If you like you can force Perl to ignore your system's flock(2) function, and so provide its own fcntl(2)-based emulation, by passing the switch -Ud_flock
to the Configure program when you configure perl.
Here's a mailbox appender for BSD systems.
- use Fcntl qw(:flock SEEK_END); # import LOCK_* and SEEK_END constants
- sub lock {
- my ($fh) = @_;
- flock($fh, LOCK_EX) or die "Cannot lock mailbox - $!n";
- # and, in case someone appended while we were waiting...
- seek($fh, 0, SEEK_END) or die "Cannot seek - $!n";
- }
- sub unlock {
- my ($fh) = @_;
- flock($fh, LOCK_UN) or die "Cannot unlock mailbox - $!n";
- }
- open(my $mbox, ">>", "/usr/spool/mail/$ENV{'USER'}")
- or die "Can't open mailbox: $!";
- lock($mbox);
- print $mbox $msg,"nn";
- unlock($mbox);
On systems that support a real flock(), locks are inherited across fork() calls, whereas those that must resort to the more capricious fcntl() function lose the locks, making it harder to write servers.
See also DB_File for other flock() examples.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7591490/viewspace-1026600/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Linux 程式設計中的檔案鎖之 flockLinux程式設計
- PHP flock 檔案鎖的使用問題PHP
- 檔案操作之按照行讀寫檔案
- Java 檔案 IO 操作之 DirectIOJava
- 7、python之檔案操作Python
- Java操作PDF檔案之ITextJava
- React Native之檔案操作React Native
- IO流之 檔案操作字元流字元
- Java審計之檔案操作漏洞Java
- Linux學習之檔案操作Linux
- php檔案操作之提取檔案/目錄的名稱PHP
- 1.4檔案操作之修改程式配置檔案小應用
- node 之fs 操作檔案 ? 快取Buffer ?快取
- Python3之檔案操作filePython
- (十七)Python學習之檔案操作Python
- python–模組之os操作檔案模組Python
- pandas操作txt檔案的方便之處
- Node.Js之FS檔案操作篇Node.js
- 07 Windows批處理之檔案操作Windows
- 檔案操作
- FastAPI基礎之 表單和檔案操作ASTAPI
- java安全編碼指南之:檔案IO操作Java
- C檔案與檔案的操作
- vivado常規操作之燒寫bit檔案_固化mcs檔案_除錯介面debug之ila與vio的操作除錯
- Go檔案操作Go
- 檔案操作(下)
- lua檔案操作
- JAVA 操作檔案Java
- golang操作檔案Golang
- JavaUtils - 檔案操作Java
- C檔案操作
- perl檔案操作
- 【shell 】檔案操作
- unix檔案操作
- java 檔案操作Java
- 2.8檔案操作
- 檔案IO操作
- 05 檔案操作