Skip to content

Instantly share code, notes, and snippets.

@naveenspace7
Last active April 6, 2021 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naveenspace7/ffed1c3ea33690b686699add198b9ba5 to your computer and use it in GitHub Desktop.
Save naveenspace7/ffed1c3ea33690b686699add198b9ba5 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <algorithm>
#include <sys/ipc.h>
#include <sys/shm.h>
using namespace std;
void * shared_mem;
template<typename type>
struct MyAlloc : allocator<type>
{
type* allocate(size_t size)
{
cout << "Allocate function called with size " << size << endl;
return new(shared_mem) type[size];
}
template <typename U> struct rebind
{
typedef MyAlloc<U> other;
};
};
void * setup_shm()
{
int shmid = shmget(777, 100, 0644 | IPC_CREAT);
shared_mem = shmat(shmid, (void*)(0), 0);
if (shared_mem == (char *) (-1)) perror("error creating shared mem\n");
return shared_mem;
}
int main()
{
cout << "Starting process 1" << endl;
void * shm_addr = setup_shm();
vector<int, MyAlloc<int>> v1(7,7);
for_each(begin(v1), end(v1), [](const int& i) { cout << i << ' ';});
cout << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment