#include <sys/mman.h> // for shared memory
#include <fcntl.h>
#include <unistd.h>
#include <cstring>
#include <cerrno>
#include <string>
#include <iostream>
class SharedMemoryDetacher
{
public:
void operator() (int* p) {
std::cout << "unlink /tmp1234" << std::endl;
if(shm_unlink("/tmp1234") != 0){
std::cerr << "OOPS:shm_unlink() failed" << std::endl;
}
}
};
std::shared_ptr<int> getSharedIntMemory (int num)
{
void *mem;
int shmfd = shm_open("/tmp1234",O_CREAT|O_RDWR,S_IRWXU|S_IRWXG);
if(shmfd < 0 ) {
throw std::string(strerror(errno));
}
if(ftruncate(shmfd,num*sizeof(int)) == -1) {
throw std::string(strerror(errno));
}
mem = mmap(nullptr,num*sizeof(int),PROT_READ | PROT_WRITE,MAP_SHARED,shmfd,0);
if (mem == MAP_FAILED) {
throw std::string(strerror(errno));
}
return std::shared_ptr<int>(static_cast<int*>(mem),
SharedMemoryDetacher());
}
int main()
{
// get and attach shared memory for 100 ints
std::shared_ptr<int> smp(getSharedIntMemory(100));
//init the shared memory
for( int i = 0; i<100; ++i){
smp.get()[i] = i*42;
}
std::cout << "<return>" << std::endl;
std::cin.get();
}
-----------------------------
g++ -o sharptr3Out sharedptr3.cpp -std=c++11 -lrt
--------------------------------
man shm_open:
SHM_OPEN(3) Linux Programmer's Manual SHM_OPEN(3)
NAME
shm_open, shm_unlink - create/open or unlink POSIX shared memory
objects
SYNOPSIS
#include <sys/mman.h>
#include <sys/stat.h> /* For mode constants */
#include <fcntl.h> /* For O_* constants */
int shm_open(const char *name, int oflag, mode_t mode);
int shm_unlink(const char *name);
Link with -lrt.
DESCRIPTION
shm_open() creates and opens a new, or opens an existing, POSIX shared
memory object. A POSIX shared memory object is in effect a handle
which can be used by unrelated processes to mmap(2) the same region of
shared memory. The shm_unlink() function performs the converse opera‐
tion, removing an object previously created by shm_open().
The operation of shm_open() is analogous to that of open(2). name
specifies the shared memory object to be created or opened. For porta‐
ble use, a shared memory object should be identified by a name of the
form /somename; that is, a null-terminated string of up to NAME_MAX
(i.e., 255) characters consisting of an initial slash, followed by one
or more characters, none of which are slashes.
oflag is a bit mask created by ORing together exactly one of O_RDONLY
or O_RDWR and any of the other flags listed here:
O_RDONLY Open the object for read access. A shared memory object
opened in this way can be mmap(2)ed only for read
(PROT_READ) access.
O_RDWR Open the object for read-write access.
O_CREAT Create the shared memory object if it does not exist. The
user and group ownership of the object are taken from the
corresponding effective IDs of the calling process, and the
object's permission bits are set according to the low-order
9 bits of mode, except that those bits set in the process
file mode creation mask (see umask(2)) are cleared for the
new object. A set of macro constants which can be used to
define mode is listed in open(2). (Symbolic definitions of
these constants can be obtained by including <sys/stat.h>.)
A new shared memory object initially has zero length—the
size of the object can be set using ftruncate(2). The newly
----------------------------