12 #ifndef SST_CORE_INTERPROCESS_TUNNEL_SHM_PARENT_H
13 #define SST_CORE_INTERPROCESS_TUNNEL_SHM_PARENT_H 1
29 namespace Interprocess {
36 template<
typename TunnelType>
49 SHMParent(uint32_t comp_id,
size_t numBuffers,
size_t bufferSize, uint32_t expectedChildren = 1) : shmPtr(nullptr), fd(-1)
52 memset(key,
'\0',
sizeof(key));
54 snprintf(key,
sizeof(key),
"/sst_shmem_%u-%" PRIu32
"-%d", getpid(), comp_id, rand());
57 fd = shm_open(filename.c_str(), O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
65 }
while ( (fd < 0) && (errno == EEXIST) );
68 fprintf(stderr,
"Failed to create IPC region '%s': %s\n", filename.c_str(), strerror(errno));
72 tunnel =
new TunnelType(numBuffers, bufferSize, expectedChildren);
73 shmSize = tunnel->getTunnelSize();
75 if ( ftruncate(fd, shmSize) ) {
77 fprintf(stderr,
"Resizing shared file '%s' failed: %s\n", filename.c_str(), strerror(errno));
81 shmPtr = mmap(
nullptr, shmSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
82 if ( shmPtr == MAP_FAILED ) {
84 fprintf(stderr,
"mmap failed: %s\n", strerror(errno));
87 memset(shmPtr,
'\0', shmSize);
88 tunnel->initialize(shmPtr);
96 munmap(shmPtr, shmSize);
116 std::string filename;
virtual ~SHMParent()
Destructor.
Definition: shmparent.h:92
SHMParent(uint32_t comp_id, size_t numBuffers, size_t bufferSize, uint32_t expectedChildren=1)
Parent/master manager for an IPC tunnel Creates a shared memory region and initializes a TunnelType d...
Definition: shmparent.h:49
Class supports an IPC tunnel between two or more processes via posix shared memory This class creates...
Definition: shmparent.h:37
const std::string & getRegionName(void) const
returns name of the mmap'd region
Definition: shmparent.h:107
TunnelType * getTunnel()
return the created tunnel pointer
Definition: shmparent.h:110