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

8,301

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.

Share:
8,301

Related videos on Youtube

Lesmana
Author by

Lesmana

Updated on September 18, 2022

Comments

  • Lesmana
    Lesmana over 1 year

    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?

    • Admin
      Admin about 11 years
      Yes, many users also mounts /tmp into shared memory.
    • Admin
      Admin about 9 years
      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.
  • Paul Fenney
    Paul Fenney over 9 years
    The most appropriate place below /run might be /run/user/${UID}/.
  • lgeorget
    lgeorget over 9 years
    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.
  • user2394284
    user2394284 about 8 years
    @PaulFenney: /run/user/$UID is also known as $XDG_RUNTIME_DIR