English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Detailed Explanation of tempfs in Linux System and/dev/shm

tmpfs is Linux/A memory-based file system on Unix systems, that is, tmpfs uses memory or swap partition to store files.

The VM subsystem in the Linux kernel is responsible for managing virtual memory resources in the background, that is, Virtual Memory, which includes RAM and swap resources. It transparently moves RAM pages to the swap partition or from the swap partition to RAM pages. The tmpfs file system requires pages from the VM subsystem to store files. tmpfs itself does not know whether these pages are in the swap partition or in RAM; making this decision is the work of the VM subsystem. What the tmpfs file system knows is that it is using some form of virtual memory.

Since tmpfs is based on memory, it is quite fast. In addition, the VM resources used by tmpfs are dynamic, when files in tmpfs are deleted, the tmpfs filesystem driver will dynamically reduce the filesystem and release VM resources, of course, VM resources will also be dynamically allocated when files are created in it. In addition, tmpfs does not have persistence, data is not retained after reboot.

/dev/shm is a device based on tmpfs, in some Linux distributions /dev/shm is /run/shm/ A symbolic link of the directory. In fact, on many systems on /run is mounted as tmpsf. Use df -T You can view the disk mounting status in the system:

Filesystem  1K-Block Used Available Used% Mount point
udev  1859684 4 1859680 1% /dev
tmpfs  374096 1524 372572 1% /run
/dev/sda8 76561456 36029540 36619724 50% /
none   4 0 4 0% /sys/fs/cgroup
none  5120 0 5120 0% /run/lock
none  1870460 27688 1842772 2% /run/shm
none  102400 56 102344 1% /run/user

So, let's talk about /run directory. Now we know that this directory is based on memory, and actually its predecessor is /var/The run directory, later known as /run replacement. This is because /var/The run filesystem is not ready when the system starts, and the processes that have started before that will first store their runtime information in /In dev,/dev is also a tmpfs, and it is available as soon as the system starts. However /The original intention of dev is to store device files, not to save process information during runtime, so to avoid confusion,/The files storing process information in dev are named with ".", which means they are all hidden folders. However, even so, with the increasing number of folders,/The contents of the dev directory are becoming more and more chaotic, so an alternative solution is introduced, namely /run. In fact, on many systems /var/The run directory still exists, but it is /A symbolic link of the run directory.

/var/The run directory mainly stores files that describe system information since the system started. A common use is for daemon processes to save their pid to this directory.

/dev/shm/ It is a very useful directory under Linux, which means Shared memory, that is, shared memory. Since it is in memory, all system processes can share this directory. By default, its size is half of the memory. If you want to change its size, you can manage it with mount:

mount -o size=4000M -o nr_inodes=1000000 -o noatime,nodiratime -o remount /dev/shm

If you want it to take effect permanently, you can modify /etc/fstab file:

tmpfs /dev/shm tmpfs defaults,size=4G 0 0

By using /dev/shm can do many things, here is a Python application. When using Python to do data processing, numpy may be used, and the amount of data to be processed is usually very large. If multiple processes need to use the same data, then /dev/shm comes into play, that is, using shared memory technology. Python has a third-party library that can be used to share numpy arrays between multiple processes, that is, SharedArray. SharedArray is based on /dev/shm is, and uses POSIX standards, which can be compatible with multiple platforms.

Summary

That's all for this article. I hope the content of this article can bring you some help in learning or work. If you have any questions, you can leave a message for communication. Thank you for your support of the呐喊 tutorial.

Declaration: The content of this article is from the Internet, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, has not been edited by humans, and does not bear relevant legal liabilities. If you find any content suspected of copyright infringement, please send an email to: notice#w3If you find any infringing content, please send an email to notice#w with the content changed from # to @ and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.

You May Also Like