為什麼 cat 不改變訪問時間?

為什麼 cat 不改變訪問時間?

第二次呼叫 cat 檔案不會更新存取時間。我期望每次顯示文件內容時都會更新訪問時間。

如果我在網頁瀏覽器中開啟該文件,我會看到相同的行為。它的訪問時間不會持續更新。

我是否誤解了訪問時間?兩隻貓的叫聲有什麼不同?

$ 觸控測試
$ 統計測試
  文件:“測試”
  大小: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
 出生:-

答案1

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 設計理念”,並補充道:“稍微思考一下:‘對於從磁碟讀取的每個文件,讓我們做一個……寫入並且,對於已快取並從快取中讀取的每個檔案…寫入磁碟!

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.
....

這就是該特定分割區的安裝方式以及為什麼 cat 沒有按我的預期更新存取時間。

相關內容