Last active
January 10, 2017 21:58
-
-
Save elmazzun/3831d40dfb4b1f2fa9d23ccd11de0587 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/mman.h> | |
#include <unistd.h> | |
#include <sys/shm.h> /* shmat(), IPC_RMID */ | |
#include <semaphore.h> /* sem_open(), sem_destroy(), sem_wait().. */ | |
#include <fcntl.h> /* O_CREAT, O_EXEC */ | |
#include <stdlib.h> | |
#include <iostream> | |
#include <map> | |
#include <string> | |
#include <chrono> | |
#include <cstddef> | |
#include <vector> | |
#include <limits> | |
#include <memory> | |
template<typename T> class stringAllocator { | |
public : | |
typedef T value_type; | |
typedef size_t size_type; | |
typedef ptrdiff_t difference_type; | |
typedef T* pointer; | |
typedef T const * const_pointer; | |
typedef T& reference; | |
typedef T const & const_reference; | |
template<typename U> struct rebind { | |
typedef stringAllocator<U> other; | |
}; | |
pointer address (reference value ) const { | |
return &value; | |
} | |
const_pointer address (const_reference value) const { | |
return &value; | |
} | |
size_type max_size () const throw() { | |
return std::numeric_limits <size_type>::max() / sizeof(T); | |
} | |
stringAllocator () throw () {} | |
stringAllocator (stringAllocator const &) throw () {} | |
template <typename U> | |
stringAllocator(stringAllocator <U> const &) throw () {} | |
~stringAllocator() throw () {} | |
pointer allocate (size_type n) { | |
pointer ptr = (pointer)malloc(n * sizeof(value_type)); | |
return ptr; | |
} | |
void deallocate (pointer p, size_type n) { | |
free(p); | |
} | |
void construct (pointer p, const_reference value) { | |
new(p) T(value); | |
} | |
void destroy (pointer p) { | |
p->~T(); | |
} | |
}; | |
template <class T1, class T2> | |
bool operator==(const stringAllocator<T1>&, const stringAllocator<T2>&) throw() { | |
return true; | |
} | |
template <class T1, class T2> | |
bool operator!=(const stringAllocator<T1>&, const stringAllocator<T2>&) throw() { | |
return false; | |
} | |
typedef std::basic_string< | |
char, | |
std::char_traits<char>, | |
stringAllocator<char> | |
> myString; | |
/*************************************** map allocator ****************************************/ | |
template<typename T> class mapAllocator{ | |
public : | |
typedef T value_type; | |
typedef value_type* pointer; | |
typedef const value_type* const_pointer; | |
typedef value_type& reference; | |
typedef const value_type& const_reference; | |
typedef std::size_t size_type; | |
typedef std::ptrdiff_t difference_type; | |
template<typename U> | |
struct rebind { | |
typedef mapAllocator<U> other; | |
}; | |
mapAllocator() throw() {} | |
mapAllocator (mapAllocator const &) throw () {} | |
~mapAllocator() throw () {} | |
template<typename U> | |
mapAllocator(mapAllocator<U> const&) {} | |
pointer address(reference r) { return &r; } | |
const_pointer address(const_reference r) { return &r; } | |
pointer allocate(size_type cnt, typename std::allocator<void>::const_pointer = 0) { | |
pointer new_memory = reinterpret_cast<pointer>(::operator new(cnt * sizeof (T))); | |
return new_memory; | |
} | |
void deallocate(pointer p, size_type n) { | |
::operator delete(p); | |
} | |
size_type max_size() const { | |
return std::numeric_limits<size_type>::max() / sizeof(T); | |
} | |
void construct(pointer p, const T& t) { | |
new(p) T(t); | |
} | |
void destroy(pointer p) { | |
p->~T(); | |
} | |
}; | |
template <class T1, class T2> | |
bool operator==(const mapAllocator<T1>&, const mapAllocator<T2>&) throw() { | |
return true; | |
} | |
template <class T1, class T2> | |
bool operator!=(const mapAllocator<T1>&, const mapAllocator<T2>&) throw() { | |
return false; | |
} | |
/*************************************** end map allocator ****************************************/ | |
// class compare for map with std::string as Key | |
class strless { | |
public: | |
bool operator() (const myString first, const myString second ) const { | |
return first.compare(second) < 0; | |
} | |
}; | |
template<typename Key, typename T> | |
using Map = std::map< | |
Key, // class Key | |
T, // class T | |
strless, // class Compare = std::less<Key> | |
mapAllocator<std::pair<const Key, T> // class Allocator = std::allocator<std::pair<const Key, T> > | |
> | |
>; | |
// typedef for the actual map I need to share between processes | |
typedef Map<myString, std::chrono::system_clock::time_point> frequencyMap; | |
class MmapManager { | |
private: | |
MmapManager() { | |
std::cout << "MmapManager()" << std::endl; | |
semMmap = sem_open("semaphore", O_CREAT|O_EXCL, 0644, 1); | |
sem_unlink("semaphore"); | |
}; | |
~MmapManager() { | |
std::cout << "~MmapManager()" << std::endl; | |
} | |
public: | |
static MmapManager& getInstance() { | |
static MmapManager instance; | |
return instance; | |
} | |
private: | |
frequencyMap fmap; | |
sem_t *semMmap; | |
public: | |
void start(void) {} | |
bool isHostAlreadyVisited(myString host) { | |
return fmap.find(host) != fmap.end(); | |
} | |
void addHost(myString host) { | |
sem_wait(semMmap); | |
fmap[host] = std::chrono::system_clock::now(); | |
sem_post(semMmap); | |
std::cout << "PROC " << getpid() << " added " << host << std::endl; | |
} | |
// get time of the visit for site "host" | |
std::chrono::system_clock::time_point getElement(myString host) { | |
return fmap[host]; | |
} | |
void printMap(void) { | |
std::cout << "printMap" << std::endl; | |
if (!fmap.empty()) { | |
for (auto it : fmap) { | |
std::cout << it.first << ' '; | |
} | |
std::cout << std::endl; | |
} else { | |
std::cout << "map empty" << std::endl; | |
} | |
} | |
}; | |
int main(void) { | |
MmapManager::getInstance().start(); | |
for (int i=0; i<3; i++) { | |
if (fork() == 0) { | |
if (!MmapManager::getInstance().isHostAlreadyVisited("www.google.com")) { | |
std::cout << "PID " << getpid() << " www.google.com is new" << std::endl; | |
MmapManager::getInstance().addHost("www.goole.com"); | |
} | |
else { | |
// if child already visited it, calculate | |
// how much time passed since last visit | |
auto now = std::chrono::system_clock::now(); | |
auto before = MmapManager::getInstance().getElement("www.google.com"); | |
std::chrono::duration<double> diff = now-before; | |
std::cout << "PID " << getpid() << " visited www.google.com " << diff.count() << " seconds ago" << std::endl; | |
} | |
_exit(EXIT_SUCCESS); | |
} | |
} | |
MmapManager::getInstance().printMap(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment