Use `/run/shm` (formerly `/dev/shm`) as a temp directory

directory-structureshared memorytmpfs

Is it good practice to create a directory in /run/shm (formerly /dev/shm) and use that like a temp directory for an application?

Background: I am writing black box tests for a program which does a lot of stuff with files and directories. For every test I create a lot of files and directories and then run the program and then create the expected set of files and directories and then run diff to compare. I now have about 40 tests and they are already taking over 2 seconds to run. Hoping to speed things up I want to run the tests in a directory on some sort of ramdisk.

Researching about ram disk I stumbled upon a question with an answer stating that it is okay to create a directory in /dev/shm and use that like a temp directory. Researching some more however I stumbled upon a wiki page from debian stating that it is an error to use /dev/shm directly. I should use the shm_* functions. Unfortunately the shm_* functions seem to be not available for use in a shell script.

Now I am confused. Is it okay or not to use /run/shm (formerly /dev/shm) like a temp directory?

Best Answer

It's perfectly okay to use some directory in /run as long as you have the appropriate rights on it. In some modern distros, /tmp is already a virtual file system in memory or a symlink to a directory inside /run. If this is your case (you can check that in /etc/fstab, or typing mtab), you could use /tmp as your temporary directory.

Also, don't get confused with the article from Debian. shm_* functions are used to create shared memory segments for Inter-Process Communication. With those functions, you can share a fragment of memory between two or more processes to have them communicate or collaborate using the same data. The processes have the segment of memory attached in their own address space and can read and write there as usual. The kernel deals with the complexity. Those functions are not available as shell functions (and wouldn't be very useful in a shell context). For further information, have a look at man 7 shm_overview. The point of the article is that no program should manage directly the pseudo-files representing shared segments, but instead use the appropriate functions to create, attach and delete shared memory segments.

Related Question