为什么cat不改变访问时间?

nel*_*aro 27 linux bash cat timestamp

第二次调用 cat a 文件不会更新访问时间。我期望每次显示文件内容时都会更新访问时间。

如果我在 Web 浏览器中打开文件,我会看到相同的行为。它的访问时间不会持续更新。

我误解了访问时间吗?两次猫叫有什么不同?

$ 触摸测试
$ 统计测试
  文件:'测试'
  大小:0 块:0 IO 块:4096 常规空文件
设备:803h/2051d 索引节点:152694 链接:1
访问:(0664/-rw-rw-r--) Uid:( 1001/ aaron) Gid:( 1001/ aaron)
访问:2012-08-21 11:05:40.586020996 +0200
修改:2012-08-21 11:05:40.586020996 +0200
更改:2012-08-21 11:05:40.586020996 +0200
 出生:-

$ vim 测试
$ 统计测试
  文件:'测试'
  大小:5 块:8 IO 块:4096 常规文件
设备:803h/2051d 索引节点:152694 链接:1
访问:(0664/-rw-rw-r--) Uid:( 1001/ aaron) Gid:( 1001/ aaron)
访问:2012-08-21 11:05:52.890021630 +0200
修改:2012-08-21 11:06:31.606023626 +0200
更改:2012-08-21 11:06:31.638023629 +0200
 出生:-

$猫测试
测试

$ 统计测试
  文件:'测试'
  大小:5 块:8 IO 块:4096 常规文件
设备:803h/2051d 索引节点:152694 链接:1
访问:(0664/-rw-rw-r--) Uid:( 1001/ aaron) Gid:( 1001/ aaron)
访问:2012-08-21 11:06:44.662024298 +0200
修改:2012-08-21 11:06:31.606023626 +0200
更改:2012-08-21 11:06:31.638023629 +0200
 出生:-

$猫测试
测试

$ 统计测试
  文件:'测试'
  大小:5 块:8 IO 块:4096 常规文件
设备:803h/2051d 索引节点:152694 链接:1
访问:(0664/-rw-rw-r--) Uid:( 1001/ aaron) Gid:( 1001/ aaron)
访问:2012-08-21 11:06:44.662024298 +0200
修改:2012-08-21 11:06:31.606023626 +0200
更改:2012-08-21 11:06:31.638023629 +0200
 出生:-

nel*_*aro 50

http://en.wikipedia.org/wiki/Stat_(system_call)

对时间的批评

写入文件会更改其 mtime 和 ctime,而读取文件会更改其 atime。因此,在符合 POSIX 的系统上,读取文件会导致写入,这受到了批评。通常可以通过在 /etc/fstab 中添加挂载选项来禁用此行为。

但是,关闭 atime 更新会破坏 POSIX 合规性,以及一些应用程序,尤其是 mutt 邮件阅读器(在某些配置中)和一些文件使用监视实用程序,尤其是 tmpwatch。在最坏的情况下,不更新 atime 会导致某些备份程序无法备份文件。

Linux 内核开发人员 Ingo Molnár 称 atime “可能是有史以来最愚蠢的 Unix 设计理念”,并补充道:“[T] 想一想:'对于从磁盘读取的每个文件,让我们做一个......写到磁盘!而且,对于每个已经缓存并从缓存中读取的文件......写入磁盘!'”他进一步强调了性能影响,因此:

Atime 更新是迄今为止 Linux 最大的 IO 性能缺陷。摆脱 atime 更新将为我们提供比过去 10 年所有页面缓存加速加起来更多的日常 Linux 性能。

如何知道 noatime 或 relatime 是否是内核中的默认挂载选项?

man mount
....
   relatime
          Update inode access times relative to  modify  or  change  time.
          Access time is only updated if the previous access time was ear?
          lier than the current modify or change time. (Similar  to  noat?
          ime,  but  doesn't break mutt or other applications that need to
          know if a file has been read since the last time  it  was  modi?
          fied.)

          Since Linux 2.6.30, the kernel defaults to the behavior provided
          by this option (unless noatime was  specified), and the stricta?
          time  option  is  required  to  obtain traditional semantics. In
          addition, since Linux 2.6.30, the file's  last  access  time  is
          always  updated  if  it  is more than 1 day old.
....
Run Code Online (Sandbox Code Playgroud)

这就是该特定分区的挂载方式以及 cat 没有按我预期更新访问时间的原因。