3

I'm curious because today the only way I know how to give two different processes the same shared-memory is through a memory-mapped file, in other words, both processes open the same memory-mapped file and write/read to/from it.

That has penalties / drawbacks as the operating system needs to swap between disk and memory.

Apologies in advance if that is a silly question, but is there such a thing as a pure shared memory between processes, not backed by a file. If yes, how would the processes get a hold of it if not using a memory-mapped file or /dev/shm file?

1

1 Answer 1

4

Apologies in advance if that is a silly question, but is there such a thing as a pure shared memory between processes, not backed by a file.

Not a silly question!

There is, it's the default way of getting it; (SYSV) shmget is the function you use to get these shared memory buffers. You assign a string name to it, a key, and another process can use that key with shmget to get access to the same. The POSIX way is shm_open with very similar semantics: you give your segment a name, which might look a lot like a file name, but isn't backed by some hard drive, and subsequent shm_open calls on the same name (with compatible/no flags) will grant access to the same memory.

5
  • Thanks! I would love to do that in Java, but it looks like rocket science. Even ChatGPT knows nothing about it => chat.openai.com/share/59754a56-6842-4531-979a-6044ca680c23 Commented Jun 4, 2023 at 3:03
  • 2
    Chatgpt generally knows nothing, so that literally tells us nothing at all. I'll not even read this link, sorry. ChatGPT just makes things up, and it has zero understanding of the things it says. It just gives answers that sound good. That is really literally the only thing it's been trained to do -giving good sounding answers, not correct ones, not understanding the subject. Asking ChatGPT about anything is really a waste of time if you're looking for understanding. That's why it's even against stack overflow rules to base posts on ChatGPT - it's a free nonsense machine, and debunking Commented Jun 4, 2023 at 8:01
  • What it says takes valuable volunteer time, while generating the nonsense costs no human time. Commented Jun 4, 2023 at 8:02
  • Sorry, man. You are right and I do take everything ChatGPT says with a grain of salt. That said, do you know a good and clear example / article for using shmget to share data between two processes? It could be in C/C++ and I can wrap with JNI to port it to Java. Commented Jun 4, 2023 at 11:05
  • 1
    the shm_open (which probably is more what you want than shmget) manual page (man shm_open) has an example. Commented Jun 4, 2023 at 11:13

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.