LOCK_EX是表示排它锁(Exclude),是独占的,通常用于写操做,而若是换成LOCK_SH(Share)则是共享,多用于读取。有时为了保证多进程对文件的读写都是惟一的,不管读写都进行LOCK_EX的请求。 spa
如下是一段演示程序,名叫flock.pl use Fcntl qw(:flock); use POSIX qw(strftime); open (FD, " > source.txt") or die "$!\n"; flock(FD, LOCK_EX); print "Yeah i get the lock by pid=$$ at ", cur_time(), "\n"; sleep 10; flock(FD, LOCK_UN); print "Oops i lose the lock by pid=$$ at ", cur_time(), "\n"; close FD; sub cur_time { strftime "%H:%M:%S", localtime; }