I am creating a server, where I have a class that handles the clients, a class that handles the server, and another class that displays each new client into a listbox. The way that I want to do it and have mostly working is, that I have the server running on a child process using fork, and the listbox running on the parent process. I want to be able to have a shared list that holds the different client classes. In python it would be done with multiprocessing manager
. I have been unable to find something like that. The way I am currently attempting to do it is with mmap
. This is basically the way I have made the code so far:
my relevant header files:
#include <list>
#include <sys/mman.h>
//creating the list
//the name of the client class is called ClientHolder
size_t pagesize = getpagesize();
std::list<ClientHolder> *Clients = static_cast<std::list<ClientHolder>*>(mmap(NULL, (pagesize*(1 << 20)),
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, 0, 0));
//then it is passed into the server class as such:
Server newServer(*Clients);
//then initialized in my listbox class as well:
ListboxHandler newListboxHandler(listbox, *Clients);
This is what my Server class initialization looks like:
class Server{
bool running=false;
char *ip;
int Port;
std::list<ClientHolder> *Clients;
size_t pagesize = getpagesize();
int *quit_call = static_cast<int*>(mmap(NULL, (pagesize*(1 << 20)),
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, 0, 0));
public:
Server(std::list<ClientHolder> Clients){Clients = Clients;};
Also, the quit_call variable works as I want it to. Obviously there is a lot more to my classes and my code. I just wanted to show exactly what was relevant.
It should also be noted that this does compile without any warnings or errors using clang++. Except that every time that I run this I get a zsh: segmentation fault
which means it is trying to read and or write to illegal memory. Is there a way to do what I want?