use strict; use Fcntl qw(:flock); use POSIX qw(strftime); open (FD, " < test.txt") or die "$!\n"; flock(FD, LOCK_EX); print "I get the lock by pid=$$ at ", cur_time(), "\n"; sleep 10; flock(FD, LOCK_UN); print "I lose the lock by pid=$$ at ", cur_time(), "\n"; close FD; sub cur_time { strftime "%Y-%m-%d %H:%M:%S", localtime; }
转载于:https://blog.51cto.com/xiangjishi/1530359