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?

  • Yes, many users also mounts /tmp into shared memory. – Eddy_Em Apr 22 '13 at 11:08
  • I would imagine that /tmp is more semantic. If your users have enough ram for using /dev/shm for temporary files to speed things up, then they'll mount it there. But if they don't have enough ram, and mounting it there slows their computer down by forcing all processes to use swap files, then they'll save their ram for more important things, and use ordinary disk space for the /tmp filesystem. In other words, /tmp is more tunable. – Jack Feb 18 '15 at 23:31

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.

| improve this answer | |
  • 2
    The most appropriate place below /run might be /run/user/${UID}/. – Paul Fenney Dec 10 '14 at 9:57
  • 1
    Indeed, on my system, pulseaudio and systemd install some files like the pid file there. However, /tmp would still be my preferred location for temporary files. – lgeorget Dec 10 '14 at 15:26
  • 2
    @PaulFenney: /run/user/$UID is also known as $XDG_RUNTIME_DIR – user2394284 Jan 27 '16 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.