2

I need to create some (small) temporary files in my zsh scripts, and I want them to live on RAM so as to avoid disk IO. What are the idiomatic ways of doing this? I want it to work on Linux and macOS, though, of course, I can just handle them with a conditional.

The best solution I have found thus far is creating a ramfs (https://stackoverflow.com/questions/46224103/create-apfs-ram-disk-on-macos-high-sierra). This works, but needs setup on the machine which I prefer to avoid.

PS: The only usecase I have in mind currently is capturing stdout and stdin in different variables, but I believe I’ll find other use cases as well in time.

HappyFace
  • 1,493
  • 9
  • 21
  • 1
    The best way, IMHO, would be to use the location given by `$TMPDIR` and then document this in the script's documentation. The user may then make sure that `$TMPDIR` points to a suitable directory on a filesystem that is fast. Not an answer, just my opinion. – Kusalananda Aug 16 '19 at 10:29
  • @Kusalananda Yes, I'm currently thinking of creating a `$RAMFSDIR`, but users can be lazy ... – HappyFace Aug 16 '19 at 11:34
  • 1
    You could have `$RAMFSDIR` default to `$TMPDIR` if not set (this may be faster storage on some systems). If the user is lazy, they get a slow program. – Kusalananda Aug 16 '19 at 12:39

1 Answers1

-1

You could use named pipes. They behave like files, but are created in RAM, iirc.

They are created using the mkfifo-command.

markgraf
  • 2,849
  • 1
  • 8
  • 20
  • 3
    No on modern system, named pipes instantiate a pipe (and they are only instantiated when there's both a reader and writer) which is an IPC mechanism with a limited buffer size. So storing data in a named or unnamed pipe is a recipe for deadlocks unless the processes that read and write to the pipe run concurrently. Think of them as a real-life pipe. IIRC, named pipes in the original UNIX implementation where disk-backed, it may still be the case in some systems. – Stéphane Chazelas Aug 16 '19 at 10:12
  • A `tmpfs` temporary file system or `shm` shared memory filesystem resides on memory which you could use to store data. Have a look at that – Valentin Bajrami Aug 16 '19 at 10:16
  • 1
    linux has a ramdisk in /dev/shm but i don't know about osx – Jasen Aug 16 '19 at 10:30