Perl中的Flock不起作用(Flock in Perl doesnt work)

系统教程 行业动态 更新时间:2024-06-14 17:02:17
Perl中的Flock不起作用(Flock in Perl doesnt work)

我有一个Perl文件。 用户打开文件,读取数据并以网格显示数据。 用户编辑它并将其保存回文件。

我正在尝试使用flock,以便当用户读取文件时,文件被锁定。 我试过下面的代码,但它没有用。

参考这篇文章的接受答案。 如何在Perl中锁定文件?

use Fcntl ':flock'; #added this at the start $filename= dsfs.com/folder1/test.txt; #location of my file open(my $fh, '<', $filename) or die $!; #file open flock($fh, LOCK_EX) or die "Could not lock '$file' - $!"; #inserted flock before reading starts so that no other user can use this file #reading of file starts here #once read, user saves file. close($fh) or die "Could not write '$file' - $!"; #release lock after user writes.

我想这是一个正常的操作,没有任何竞争条件,但这对我不起作用。我不确定perl脚本是否能够检测到flock。

出于测试目的,我尝试在写入和保存功能完成之前打开文件。 当我尝试在保存完成之前打开相同的文件时,这意味着锁尚未释放。 在这种情况下,如果我在后端打开文件并编辑文件,我仍然可以保存更改。 在实际情况中,一旦文件被锁定,它就不能编辑任何内容。

任何人都可以建议我对此进行任何故障排除或者我使用flock的程序不正确?

I have a Perl file. The user opens a file, reads data and displays the data in grid. user edits it and saves it back to the file.

I am trying to use flock so that when the user reads the file, the file gets locked. I tried below code but it didnt work.

Referring to the accepted answer of this post. How do I lock a file in Perl?

use Fcntl ':flock'; #added this at the start $filename= dsfs.com/folder1/test.txt; #location of my file open(my $fh, '<', $filename) or die $!; #file open flock($fh, LOCK_EX) or die "Could not lock '$file' - $!"; #inserted flock before reading starts so that no other user can use this file #reading of file starts here #once read, user saves file. close($fh) or die "Could not write '$file' - $!"; #release lock after user writes.

I guess this is a normal operation without any race around conditions but this doesnot work for me.I am not sure if the perl script is able to detect flock or not.

For testing purposes, i try to open the file before my writing and saving function gets completed. when i try to open the same file before saving gets completed, it means that the lock is not released yet. in this situation if i open the file at backend and edit the file, i am still able to save changes. In practical case, it should not be able to edit anything once the file is locked.

can anyone please suggest me any troubleshooting for this or is my procedure of using flock incorrect ??

最满意答案

如果您的flock实现基于lockf(3)或fcntl(2) ,则可能存在另一个问题。 即, LOCK_EX应与打开输出的文件一起使用“写入意图”。

对于lockf(3) , perldoc -f flock说

请注意,使用lockf(3)构建的仿真不提供共享锁,并且它要求使用写意图打开FILEHANDLE。

对于fcntl(2) :

请注意,flock(3)的fcntl(2)仿真要求FILEHANDLE以读取意图打开以使用LOCK_SH,并且要求使用LOCK_EX打开写入意图。

输入文件或更复杂的同步操作的变通方法是让所有进程在简单的锁文件上同步,如:

open my $lock, '>>', "$filename.lock"; flock $lock, LOCK_EX; # can't get here until our process has the lock ... open(my $fh, '<', $filename) or die $!; #file open ... read file, manipulate ... close $fh; open my $fh2, '>', $filename; ... rewrite file ... close $fh2; # done with this operation, can release the lock and let another # process access the file close $lock;

There's another problem if your flock implementation is based on lockf(3) or fcntl(2), which it probably is. Namely, LOCK_EX should be used with "write intent", on a file opened for output.

For lockf(3), perldoc -f flock says

Note that the emulation built with lockf(3) doesn't provide shared locks, and it requires that FILEHANDLE be open with write intent.

and for fcntl(2):

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.

A workaround for input files or for more complicated synchronized operations is for all processes to sync on a trivial lock file, like:

open my $lock, '>>', "$filename.lock"; flock $lock, LOCK_EX; # can't get here until our process has the lock ... open(my $fh, '<', $filename) or die $!; #file open ... read file, manipulate ... close $fh; open my $fh2, '>', $filename; ... rewrite file ... close $fh2; # done with this operation, can release the lock and let another # process access the file close $lock;

更多推荐

本文发布于:2023-04-21 18:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/086be8a51548258a1ebb64d90b489315.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   Flock   Perl   doesnt   work

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!