0

Ignoring some details there are two low-level SHM APIs available for in Linux.

We have the older (e.g System V IPC vs POSIX IPC) SysV interface using:

  • ftok
  • shmctl
  • shmget
  • shmat
  • shmdt

and the newer Posix interface (though Posix seems to standardize the SysV one as well):

  • shm_open
  • shm_unlink

It is possible and safe to share memory such that one program uses shm_open() while the other uses shmget() ?

Bruce Adams
  • 4,953
  • 4
  • 48
  • 111

1 Answers1

0

I think the answer is no, though someone wiser may know better.

shm_open(path,...) maps one file to a shared memory segment whereas ftok(path,id,...) maps a named placeholder file to one or more segments. See this related question - Relationship between shared memory and files

So on the one hand you have a one to one mapping between filenames and segments and on the other a one to many - as in the linked question.

Also the path used by shmget() is just a placeholder. For shm_open() the map might be the actual file (though this is implementation defined).

I'm not sure there is anyway to make shm_open() and shmat() refer to the same memory location. Even if you could mix them somehow it would probably be undefined behaviour.

If you look the glibc implementation of shm_open it is simply a wrapper to opening a file. The implementation of shmget and shmat are internal system calls.

It may be that they share an implementation further down in the Linux kernal but this is not a detail that should be exposed or relied upon.

Bruce Adams
  • 4,953
  • 4
  • 48
  • 111