We can create and attach a shared memory to a process using shmget()
and shmat()
. What will happen if we don't destroy and detach the shared memory in Ubuntu OS ? According to my understanding it will still exist in the physical memory until the system is restarted, but why is that ? I mean we can load the physical memory with many shared memory blocks through creating multiple of shared memory blocks and this can fill up the physical memory inefficiently ???
-
1Your question is incomplete. "If we don't destroy and detach the shared memory" and what else happens? The processes attached to the shared memory continue to run? Or they exit? What is the scenario you're asking about?– Sotto VoceCommented Mar 20, 2023 at 23:01
-
Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.– Community BotCommented Mar 21, 2023 at 4:18
1 Answer
From an implementation perspective, it's not much different from a file system inside a ramdisk, and this is how Linux implements it.
If you do not delete a file you created before exiting your program, the file will continue to exist and consume resources.
SystemV SHM is rarely used, and the only reason to do that is that it guarantees that the mapping will be backed by anonymous memory, and not generate disk writes on change, and do so in a portable way.