perl – 当您再次使用open时,文件锁会保留吗?
发布时间:2020-12-16 06:05:44 所属栏目:大数据 来源:网络整理
导读:假设: my $fh;open $fh,"","file.txt";flock($fh,LOCK_EX);open $fh,"+","file.txt";close $fh; 文件锁会保留还是会被释放?如果它会被释放有没有办法让它留下来? 我没有找到相关信息. 解决方法 在已分配给打开文件描述符的文件句柄上调用open会对文件句柄
假设:
my $fh; open $fh,">>","file.txt"; flock($fh,LOCK_EX); open $fh,"+<","file.txt"; close $fh; 文件锁会保留还是会被释放?如果它会被释放有没有办法让它留下来? 解决方法
在已分配给打开文件描述符的文件句柄上调用open会对文件句柄执行隐式关闭.关闭一个锁定的文件句柄会释放锁定.
通过在两种不同的模式下使用相同的文件句柄打开相同的文件,我不清楚你要做什么.如果您使用第二个文件句柄怎么办? open my $fh,LOCK_EX); open my $fh2,"file.txt"; ... rewrite 'file.txt' with $fh2 ... close $fh2; # done with rewrite close $fh; # done with lock 对于<中的文件,看起来像flock一样受到尊重模式(在Linux中工作,这可能不是可移植的),所以使用它和一些搜索语句,你只需要一个文件句柄. # make sure file exists before you use '+<' mode { open my $touch,'>>','file.txt'; } open my $fh,'+<','file.txt'; flock $fh,LOCK_EX; seek $fh,2; print $fh 'stuff for the end of the file'; seek $fh,0; print $fh 'something for the start of the file'; close $fh; # releases lock (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |