【问题标题】:Can perl sysopen open file for atomic writes?perl sysopen 可以打开文件进行原子写入吗?
【发布时间】:2019-06-25 02:06:25
【问题描述】:

在阅读 APUE(第 3 版)一书时,我遇到了 open 系统调用及其允许用户以 O_APPEND 模式打开文件以进行 write 原子操作的能力,这意味着多个进程可以写入文件描述符内核确保多个进程写入单个文件的数据不重叠,所有行都完好无损。

在使用 C/C++ 程序成功尝试开放系统调用后,我能够验证相同的结果,并且它的工作原理与书中描述的一样。我能够启动写入单个文件的多个进程,并且所有行都可以考虑到它们的进程 PID。

我希望通过 perl sysopen 观察到相同的行为,因为我有一些工作中的任务可以从这种行为中受益。试过了,但实际上没有用。当我分析输出文件时,我能够看到竞争条件的迹象(可能),因为有很多次交错的行。

问题:perl sysopen call 和 linux 的 open 系统调用不一样吗?这种多进程对单个文件的原子写操作是否可以实现?

编辑:添加 C 代码和用于测试的 perl 代码。

C/C++ 代码

int main(void)
{
  if ((fd = open("outfile.txt",O_WRONLY|O_CREAT|O_APPEND)) == -1) { 
    printf ("failed to create outfile! exiting!\n");
    return -1;
  }

  for (int counter{1};counter<=MAXLINES;counter++)
  { /* write string 'line' for MAXLINES no. of times */
    std::string line = std::to_string(ACE_OS::getpid())
      + " This is a sample data line ";
    line += std::to_string(counter) + " \n";
    if ((n = write(fd,line.c_str(),strlen(line.c_str()))) == -1) {
      printf("Failed to write to outfile!\n";
    }
  }
  return 0;
}

Perl 代码

#!/usr/bin/perl

use Fcntl;
use strict;
use warnings;

my $maxlines = 100000;

sysopen (FH, "testfile", O_CREAT|O_WRONLY|O_APPEND) or die "failed sysopen\n";
while ($maxlines != 0) {
  print FH "($$) This is sample data line no. $maxlines\n";
  $maxlines--;
}
close (FH);
__END__

更新(初始故障排除后):

感谢下面答案中提供的信息,我能够让它工作。虽然我遇到了一些缺失行的问题,这是由于我使用O_TRUNC 打开每个进程的文件,我不应该这样做,但最初错过了它。经过一些仔细的分析 - 我发现了这个问题并纠正了它。一如既往 - linux 永远不会让你失望:)。

这是我用来启动进程的 bash 脚本:

#!/bin/bash

# basically we spawn "$1" instances of the same 
# executable which should append to the same output file.

max=$1
[[ -z $max ]] && max=6
echo "creating $max processes for appending into same file"

# this is our output file collecting all
# the lines from all the processes.
# we truncate it before we start
>testfile

for i in $(seq 1 $max)
do
    echo $i && ./perl_read_write_with_syscalls.pl 2>>_err & 
done

# end.

从输出文件验证:

[compuser@lenovoe470:07-multiple-processes-append-to-a-single-file]$  ls -lrth testfile 
-rw-rw-r--. 1 compuser compuser 252M Jan 31 22:52 testfile
[compuser@lenovoe470:07-multiple-processes-append-to-a-single-file]$  wc -l testfile 
6000000 testfile
[compuser@lenovoe470:07-multiple-processes-append-to-a-single-file]$  cat testfile |cut -f1 -d" "|sort|uniq -c
1000000 (PID: 21118)
1000000 (PID: 21123)
1000000 (PID: 21124)
1000000 (PID: 21125)
1000000 (PID: 21126)
1000000 (PID: 21127)
[compuser@lenovoe470:07-multiple-processes-append-to-a-single-file]$  

观察:

令我惊讶的是,系统上根本没有任何等待平均负载。我没想到。我相信内核必须以某种方式处理它,但不知道它是如何工作的。我很想知道更多。

这有什么可能的应用?

我做了很多文件对文件的核对,我们(在工作中)总是需要解析巨大的数据文件(比如每个 30gb - 50gb)。通过这个工作 - 我现在可以执行并行操作,而不是我以前的方法,其中包括:散列 file1,然后散列 file2,然后比较来自 2 个文件的键、值对。现在我可以并行执行散列部分并缩短所需时间 - 甚至更进一步。

谢谢

【问题讨论】:

  • 你能包含你试验过的代码吗(C 和 Perl 都可以?)
  • @choroba:添加代码。
  • 我怀疑写入的大小是原子的。我怀疑限制不少于 4 KiB,但这只是猜测。

标签: linux perl system-calls atomic file-descriptor


【解决方案1】:

opensysopen 无所谓;关键是使用syswritesysread 而不是print/printf/say/etc 和readline/read/eof/etc。

syswrite 映射到单个write(2) 调用,而print/printf/say/etc 可能导致对write(2) 的多次调用(即使启用了自动刷新)。[ 1]

sysread 映射到单个read(2) 调用,而readline/read/eof/etc 可以导致对read(2) 的多次调用。

因此,如果您使用的是 POSIX 系统,则通过使用 syswritesysread,您将受到 POSIX 针对这些调用(无论它们可能是什么)提供的所有保证。


  1. 如果您使用print/printf/say/etc,并且将写入限制在小于(显式或自动)刷新之间的缓冲区大小,您将获得一个write(2) 调用.在旧版本的 Perl 中缓冲区大小为 4 KiB,在新版本的 Perl 中默认为 8 KiB。 (大小在构建perl时决定。)

【讨论】:

  • 有什么方法可以在 NFS 文件系统上进行这项工作吗?虽然我已经读到 NFS 文件系统不符合 POSIX 标准,但我能以某种方式破解它吗?欢迎您提出宝贵的cmets/建议!提前致谢。
  • 我对 POSIX 保证一无所知。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-22
  • 1970-01-01
  • 1970-01-01
  • 2020-06-23
  • 2011-05-27
  • 1970-01-01
相关资源
最近更新 更多