AIX 系统上 perl 模块中的 Flock 调用

问题描述 投票:0回答:1

我正在尝试使以下项目在 AIX 系统上运行。我快完成了,即将结束,但我在执行代理时遇到了问题。

我的 AIX 系统没有安装了集群,并且在下载过程中此文件

中存在问题

flock测试结果全部返回false

例如,即使我删除了文件,我也会系统性地出现以下错误消息

[Sat Jun 29 09:47:24 2024][error] [download] /opt/ocsinventory/var/lib/ocsinventory-agent/https:__hostname.com_ocsinventory/download/lock locked. Cannot begin work... :-(

来自以下失败的群体测试


# At the beginning of end handler
sub begin{
    my ($pidfile,$logger) = @_;

    open LOCK_R, "$pidfile" or die("Cannot open pid file: $!");
    if (flock(LOCK_R,LOCK_EX|LOCK_NB)){
        open LOCK_W, ">$pidfile" or die("Cannot open pid file: $!");
        select(LOCK_W) and $|=1;
        select(STDOUT) and $|=1;
        print LOCK_W $$;
        $logger->info("Beginning work. I am $$.");
        return 0;
    } else {
        close(LOCK_R);
        $logger->error("$pidfile locked. Cannot begin work... :-(");
        return 1;
    }
}

我尝试构建 util-linux 2.40 2.21 和 2.13 但它们都失败了

我还读到这里,flock 在 Unix 系统上不会那么强制

我认为我的分析很好,但我对如何实施修复感到困惑。

如果你必须让它发挥作用,你会怎么做?我确实需要修复此代码,以便它与 AIX 兼容

perl aix flock
1个回答
0
投票

尝试使用

File::FcntlLock
而不是
flock
。这是一个例子:

羊群

read_shared.pl

use v5.38;
use Fcntl ':flock';  # Import LOCK_* constants

my $filename = 'example.txt';
open(my $fh, '<', $filename) or die "Could not open file '$filename': $!";
flock($fh, LOCK_SH) or die "Could not lock file '$filename': $!";

# Read from the file
while (my $line = <$fh>) {
    print $line;
    sleep 1;  # Simulate a long operation
}

# Close the file (automatically releases the lock)
close($fh);

print "File has been read with a shared lock.\n";

write_exclusive.pl

use v5.38;
use Fcntl ':flock';  # Import LOCK_* constants

my $filename = 'example.txt';

# Open the file for both reading and writing
# NOTE: Do not open the for writing only, as this will truncate the file before acquiring the lock
open(my $fh, '+<', $filename) or die "Could not open file '$filename': $!";

# Try to acquire an exclusive lock
if (flock($fh, LOCK_EX | LOCK_NB)) {
    print "Acquired exclusive lock on file '$filename'.\n";

    truncate($fh, 0) or die "Could not truncate file '$filename': $!";
    # Write to the file
    print $fh "Adding a new line to the file.\n";

    # Close the file handle (automatically releases the lock)
    close($fh);
} else {
    print "Could not acquire exclusive lock on file '$filename': $!\n";
}

print "Attempt to acquire exclusive lock has finished.\n";

文件::FcntlLock

File::FcntlLock
做同样的事情:

read_shared.pl

use v5.38;
use File::FcntlLock;

my $filename = 'example.txt';

open(my $fh, '<', $filename) or die "Could not open file '$filename': $!";

my $lock = File::FcntlLock->new(
    l_type => F_RDLCK,
    l_whence => SEEK_SET,  # Lock from the beginning of the file
    l_start => 0,
    l_len => 0,  # Lock the entire file
    l_pid => $$,  # Lock is owned by the current process
);

# Acquire a shared lock
$lock->lock( $fh, F_SETLK ) or die "Could not lock file '$filename': $!";

while (my $line = <$fh>) {
    print $line;
    sleep 1;  # Simulate a long operation
}

# Close the file (automatically releases the lock)
close($fh);

print "File has been read with a shared lock.\n";

write_exclusive.pl

use v5.38;
use File::FcntlLock;
use Fcntl qw(SEEK_SET);

my $filename = 'example.txt';

# Open the file for both reading and writing without truncating it
open(my $fh, '+<', $filename) or die "Could not open file '$filename': $!";

# Create a File::FcntlLock object
my $lock = File::FcntlLock->new(
    l_type => F_WRLCK,
    l_whence => SEEK_SET,  # Lock from the beginning of the file
    l_start => 0,
    l_len => 0,  # Lock the entire file
    l_pid => $$,  # Lock is owned by the current process
);

# Try to acquire an exclusive lock in non-blocking mode
if ($lock->lock( $fh, F_SETLK )) {
    print "Acquired exclusive lock on file '$filename'.\n";

    # Truncate the file
    truncate($fh, 0) or die "Could not truncate file '$filename': $!";

    print $fh "Adding a new line to the file.\n";
    
    # Close the file handle (automatically releases the lock)
    close($fh);
} else {
    print "Could not acquire exclusive lock on file '$filename': $!\n";
}

print "Attempt to acquire exclusive lock has finished.\n";
© www.soinside.com 2019 - 2024. All rights reserved.