2

I am developing a shared memory suite of applications, in C, on Ubuntu 12.04 server.

This is my first attempt at dealing with shared memory, so I am not sure of all the pitfalls.

My suite starts with a daemon that creates a shared memory segment, and a few other apps that use it.

During development, of course, the segment can get created, then the program crash without removing it. I can see it listed using ipcs -m.

Normally, this isn't much of an issue, as the next time I start the "controlling" program, it simply reuses the existing segment.

However, from time to time, the size of the segment changes, so the program fails the shmget() function.

My question...

Before I create and attach the segment, I would like to first determine if it already exists, then if needed, unattach it, then I think, shmctl(..., ipc_rmd, ...).

Can I have some help to figure the proper sequence of events to do this?

I think I should use shmctl to see what's up, (number of attachments, if it exists, etc..) but I don't really know.

It may be there is another app still using the segment, of course that app will have to first be stopped, and any other possible exceptions handled.

I was thinking, in the shared memory segment, I could copy the system clock, and when the app detects the clock of the shared mem differs more then a few seconds, then detach or something.

Thanks, Mark.

Rob Starling
  • 3,868
  • 3
  • 23
  • 40
Cool Javelin
  • 776
  • 2
  • 10
  • 26
  • 1
    Why did you choose System V IPC instead of POSIX? I find POSIX shared memory easier to use and manage. Besides, you have tons of example on the Net. See http://stackoverflow.com/questions/4582968/system-v-ipc-vs-posix-ipc – Déjà vu Dec 25 '15 at 05:41
  • The way I understand the POSIX approach is, any shared mem must come from some child of a parent that created the shared segment (fork, exec, etc...) perhaps I am wrong, at least I haven't figured out how to do the following. I will be creating some other apps that will use the shared segment totally independent of the creator. – Cool Javelin Dec 26 '15 at 08:47

0 Answers0