|
perl语言编程中说:"flock缺省时是一个阻塞操作。也就是说,如果不能立即获取
你需要的锁,操作系统会推迟你的处理,直到能够获得锁为止。"
因此,编写如下测试程序:
#!/usr/bin/perl
use Fcntl qw(EFAULT :flock);
open(FH,"<test.lock");
if ($PID=fork()) {
sleep 2;
print "father:try to get lock\n";
flock(FH,LOCK_EX);
print "father:i have got lock\n";
flock(FH,LOCK_UN);
print "\nfather:I have released lock\n";
} else {
print "son:try to get lock\n";
flock(FH,LOCK_EX);
print "son:I have got lock\n";
sleep 5;
flock(FH,LOCK_UN);
print "\nson:I have released lock\n";
}
运行结果:
son:try to get lock
son:I have got lock
father:try to get lock
father:i have got lock
father:I have released lock
son:I have released lock
很显然 flock 没有进行阻塞,请教各位TX,错误在哪。 |
|