3

I have two questions:

  1. while using shm_open, how to know if I have opened an already existing shared memory, I am using O_CREATE | O_RDWR.

  2. I am using shm_open to create/open a shared memory object with some name and mmap for mapping it into process' virtual address space. If the process crashes and fails to clean up shared memory it stays until system shutdown. Though it this contradictory with what has been mentioned on wiki, which says, "The shared memory created by shm_open is persistent. It stays in the system until explicitly removed by a process. This has a drawback that if the process crashes and fails to clean up shared memory it will stay until system shutdown. To avoid this issue mmap can be used to create a shared memory". I am talking about the file with name mentioned in shm_open, which gets created in /dev/shm, it remains if process gets crashed without cleaning up the shared memory (unmap and shm_unlink). I am expecting, if there are no other references to shared memory by any process, and the crashed process was the only one referring, that shared memory object and file should get cleaned up.

Abhishek Jain
  • 9,614
  • 5
  • 26
  • 40
  • I don't understand your second question. Everything you say is perfectly consistent. What do you think is inconsistent? Why do you expect the shared memory to be cleaned up? – David Schwartz Oct 29 '14 at 07:19
  • if there are no more processes referring to the shared memory, then it should get cleaned up. When i restart my processes, i expect to create a new shared memory rather than using the same as earlier. – Abhishek Jain Oct 29 '14 at 07:40
  • Why do you expect that? Why don't you expect this: "*The shared memory created by shm_open is persistent. It stays in the system until explicitly removed by a process.*" That expectation seems to be unjustified by any reasons whatsoever. – David Schwartz Oct 29 '14 at 07:41
  • Ok, what else should i do, any alternative to shm_open that can meet my expectations? – Abhishek Jain Oct 29 '14 at 07:57
  • Your question answered that already: "*To avoid this issue mmap can be used to create a shared memory*" – David Schwartz Oct 29 '14 at 08:01
  • How? any links/code? right now also i am using shm_open for creating the shared memory object: vMappedFile = shm_open(shmemname.c_str(), O_CREAT | O_RDWR, 0666); and mmap for mapping like vMemory = mmap(NULL, pBytes, PROT_READ | (vReadOnly ? 0 : PROT_WRITE), MAP_SHARED, vMappedFile, 0); – Abhishek Jain Oct 29 '14 at 08:05
  • Instead, open `/dev/zero` and `mmap` [it](http://stackoverflow.com/questions/8507945/mmap-with-dev-zero). – David Schwartz Oct 29 '14 at 08:36
  • David, what about the first question? – Abhishek Jain Oct 30 '14 at 05:37

1 Answers1

2

I know this answer is late, but I was busy with the same subject. According to this shm_open manual use the O_EXCL oflag to detect if the shared memory object already exists.

Marvin Effing
  • 2,693
  • 3
  • 20
  • 35
  • 1
    That's true, but there appears to be no way to avoid a race condition. If shm_open fails because the object already exists, it could easily be created by another process before you get a chance to try again. – H. Guijt Aug 11 '21 at 12:52
  • to avoid a race condition i would try a flock. This works over process boundaries – kuga Mar 03 '22 at 16:43