lsof 总是报告偏移量等于 OSX 中的文件大小

lsof always report offset equal to file size in OSX

我试图估计我的程序处理了多少文件,对我来说一个明显的解决方案是使用 lsof -o。但令人惊讶的是,lsof 输出中的 OFFSET 总是等于 SIZE(就像在 lsof -s -- 中一样),所以我决定编写一些简单的程序来测试该行为,并且...

C:

#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

int main(void) {
  int filedesc = open("path/to/file", O_RDONLY);
  printf("%i\n", filedesc);
  while(1) {};
}

Scala:

io.Source.fromFile(path)

Python:

open(path)

OFFSET 始终位于 OS X:

下的文件末尾
$ lsof -o /path/to/file
COMMAND  PID  USER   FD   TYPE DEVICE    OFFSET     NODE NAME
a.out   5390 folex    3r   REG    1,4 631302648 48453926 /path/to/file

$ lsof -s -- /path/to/file
COMMAND  PID  USER   FD   TYPE DEVICE      SIZE     NODE NAME
a.out   5390 folex    3r   REG    1,4 631302648 48453926 /path/to/file

如能对这些语言中的每一种进行解释,我们将不胜感激。

更新:在 Ubuntu 下按预期工作。仅在 OS X.

下有错误的偏移量

以下是 OSX 手册页关于 lsof 的 size/offset 列的说明(强调已添加):

SIZE, SIZE/OFF, or OFFSET is the size of the file or the file offset in bytes. A value is displayed in this column only if it is available. Lsof displays whatever value - size or offset - is appropriate for the type of the file and the version of lsof. On some UNIX dialects lsof can't obtain accurate or consistent file offset information from its kernel data sources, [...]

The file size is displayed in decimal; the offset is normally displayed in decimal with a leading '0t' if it contains 8 digits or less; in hexadecimal with a leading '0x' if it is longer than 8 digits. [...]

Thus the leading '0t' and '0x' identify an offset when the column may contain both a size and an offset (i.e., its title is SIZE/OFF).

尽管列标题显示 OFFSET,但该数字没有前导 '0t''0x',因此我认为文件偏移信息根本无法从 OSX 内核.