ramfs和tmpfs的區別


簡介

ramfs和tmpfs是在內存上建立的文件系統(Filesystem)。其優點是讀寫速度很快,但存在掉電丟失的風險。如果一個進程的性能瓶頸是硬盤的讀寫,那么可以考慮在ramfs或tmpfs上進行大文件的讀寫操作。

ramfs和tmpfs之間的區別:

ramfs和tmpfs的區別
特性  tmpfs ramfs
 達到空間上限時繼續寫入 提示錯誤信息並終止  可以繼續寫尚未分配的空間
是否固定大小
 是否使用swap
 具有易失性  是

查看

通過下面的方法可以查看系統中的tmpfs和ramfs:

not@linux-numy:~> mount | grep -E "(tmpfs|ramfs)"
devtmpfs on /dev type devtmpfs (rw,relatime,size=1945280k,nr_inodes=486320,mode=755)
tmpfs on /dev/shm type tmpfs (rw,relatime)
tmpfs on /run type tmpfs (rw,nosuid,nodev,relatime,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (rw,nosuid,nodev,noexec,mode=755)
tmpfs on /var/lock type tmpfs (rw,nosuid,nodev,relatime,mode=755)
tmpfs on /var/run type tmpfs (rw,nosuid,nodev,relatime,mode=755)

或者:

not@linux-numy:~> df -h | grep -E "(tmpfs|ramfs)"
devtmpfs        1.9G   16K  1.9G   1% /dev
tmpfs           1.9G   27M  1.9G   2% /dev/shm
tmpfs           1.9G  4.3M  1.9G   1% /run
tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
tmpfs           1.9G  4.3M  1.9G   1% /var/lock
tmpfs           1.9G  4.3M  1.9G   1% /var/run

我的系統(openSUSE 13.1 "Bottle", kernel version: 3.11.10-21)中,使用的都是tmpfs。我想原因可能是,當存在寫溢出時,tmpfs比ramfs更加安全,因為前者會給出錯誤提示並禁止寫操作。

創建

創建tmpfs:

linux-numy:~ # mkdir -p /mnt/tmp
linux-numy:~ # mount -t tmpfs -o size=20m tmpfs /mnt/tmp/
linux-numy:~ # df -h | grep "/mnt/tmp"
tmpfs            20M     0   20M   0% /mnt/tmp

創建ramfs:

linux-numy:~ # mkdir -p /mnt/ram
linux-numy:~ # mount -t ramfs -o size=20m ramfs /mnt/ram/
linux-numy:~ # df -ah | grep "/mnt/ram"
ramfs              0     0     0    - /mnt/ram

這里df只使用h選項是無法顯示ramfs的內容的。

df無法顯示ramfs信息的原因(無-a選項)

根據superuser.com上的問答《Have I successfully created an ramfs drive?》,Sachin Divekar給出了一段資料引用:

For a ramfs filesystem, the newer kernels report nothing back using "df". There is meant to be a patch for this (to allow for accounting in a ramfs). Philosophically, ramfs is mean to be as simple as possible, apparently, hence the lack of accounting. So data can be stored and used on the ramfs disk, but no accounting of it is possible, other than a loss of memory shown with "free". For this reason the tmpfs is better, since it does keep accounting and "df" shows what's going on.

即,tmpfs會對內存進行accounting(統計內存的使用情況),而ramfs被設計為盡可能的簡單,所以不會進行accounting。因此,針對ramfs,在較新的內核中,使用df不會返回ramfs的信息。

參考資料

Overview of RAMFS and TMPFS on Linux


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM