发生错误时释放羊群吗? [英] Release of flock in case of errors?

查看:74
本文介绍了发生错误时释放羊群吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象下面的Perl代码(此处为伪代码):

Imagine the following Perl code (here in pseudo code):

successfully acquired flock for FILEHANDLER       # line 1
some error or maybe simply a call to exit()       # line 2
close FILEHANDLER (which also releases the lock)  # line 3

在这种情况下,由于Perl脚本在第2行结束,因此我不会释放该锁.在这种情况下,操作系统是否曾经释放过该锁?它是否看到嘿,获取锁的脚本崩溃了"并释放了锁?它会立即释放锁吗?另外,每个脚本是否都运行一个Perl实例,以便清楚地知道哪个脚本崩溃/停止而不释放锁?

In this case I wouldn't release the lock, as the Perl script ends in line 2. In that case, is the lock ever released by the operating system? Does it see "hey, the script that acquired the lock crashed" and release the lock? Does it release the lock immediately? Also, is there one Perl instance running for each script, so that it's clear which script crashed/stopped without releasing the lock?

推荐答案

在这种情况下,操作系统是否曾经释放过锁?
是否看到嘿,获取锁的脚本崩溃了"并释放了锁?
它会立即释放锁吗?

In that case, is the lock ever released by the operating system?
Does it see "hey, the script that acquired the lock crashed" and release the lock?
Does it release the lock immediately?

所有这些问题均取决于系统. Perl 5不实现文件锁定功能,它仅提供flock(2)fcntl(2)锁定或lockf(3)的通用接口(取决于操作系统中提供的功能).程序退出,segfaults或被sigkill杀死时,会发生什么情况.

All of these questions are system dependent. Perl 5 does not implement a file locking function, it just provides a common interface to flock(2), fcntl(2) locking, or lockf(3) (depending on what is available in the OS). There may also be a difference between what happens when a program exits, segfaults, or is killed with a sigkill.

在Linux下进行的快速测试表明,在正常退出情况下,锁已被删除:

A quick test under Linux shows that a lock is removed under normal exit conditions:

$ perl -le 'open my $fh, ">", "f" or die $!; print flock($fh, 6) ? "got lock" : "was already locked", "\n"'
got lock
$ perl -le 'open my $fh, ">", "f" or die $!; print flock($fh, 6) ? "got lock" : "was already locked", "\n"'
got lock

让我们看看当我们die时会发生什么:

Let's see what happens when we die:

$ perl -le 'open my $fh, ">", "f" or die $!; print flock($fh, 6) ? "got lock" : "was already locked", "\n"; die "died"'
got lock
died at -e line 1.
$ perl -le 'open my $fh, ">", "f" or die $!; print flock($fh, 6) ? "got lock" : "was already locked", "\n"; die "died"'
got lock
died at -e line 1.

要获取段错误,我们需要访问C,我正在使用Inline来获取它:

To get a segfault, we will need access to C, I am using Inline to get it:

$ cat segfault.pl
#!/usr/bin/perl

use strict;
use warnings;

use Inline "C";

open my $fh, ">", "f" or die $!;

print flock($fh, 6) ? "got lock" : "was already locked", "\n";

crash();

__DATA__
__C__

void crash() {
    int* ptr = NULL;
    *ptr = 5;
}
$ perl segfault.pl
got lock
Segmentation fault
$ perl segfault.pl
got lock
Segmentation fault

最后,这是发送程序SIGKILL时发生的事情:

And finally, here is what happens when a program is sent SIGKILL:

$ cat fork.pl
#!/usr/bin/perl

use strict;
use warnings;

$SIG{CHLD} = "IGNORE"; #auto-reap children

die "could not fork: $!" unless defined(my $pid = fork);
unless ($pid) {
    #child
    open my $fh, ">", "f" or die $!;
    print flock($fh, 6) ? "got lock" : "was already locked", "\n";
    sleep(100);
    exit;
}

kill 9, $pid;

die "could not fork: $!" unless defined($pid = fork);
unless ($pid) {
    #child
    open my $fh, ">", "f" or die $!;
    print flock($fh, 6) ? "got lock" : "was already locked", "\n";
    exit;
}
$ perl fork.pl
got lock
got lock

从这些实验中,我们可以看到针对您所关心的每种情况,锁都是在Linux中释放的.

From these experiments, we can see that the lock is released in Linux for each of the cases you were concerned with.

此外,每个脚本是否都运行一个perl实例,以便清楚地知道哪个脚本崩溃/停止而不释放锁?

Also, is there one perl instance running for each script, so that it's clear which script crashed/stopped without releasing the lock?

是的,Perl 5每个脚本有一个perl进程.即使您分叉,孩子也会得到自己的perl进程.线程没有提供单独的perl进程.

Yes, Perl 5 has one perl process per script. Even if you fork, the child gets its own perl process. Threading does not provide a separate perl process.

注意:如果父进程获得了锁并且在锁之前没有放弃,那么即使父进程退出,子进程也将拥有相同的锁.

Note: if a parent process gets a lock and does not give it up before locking, then the child will have the same lock even if the parent exits.

这篇关于发生错误时释放羊群吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆