2

I'm trying to share an instance of a class between two programs. This is a glorified producers consumers problem; however, for abstraction purposes, I have put a mutex in the class.

I've seen instances of sharing structs between processes, but this generally involved a fork. I want to keep the processes separate, because they will be doing two different things so half of the program\code segment will be wasted on each process.

It might be easier to show than try to explain.

class my_class
{
  private:
    sem_t mutex;
    data_type *my_data; //just some linked list
    fstream some_file;
  public:
    my_class();
    data_type* retrieve();
    void add(string add);
};

my_class::my_class()
{
  my_data = new data_type();
  sem_init(&mutex, 0, 1);
  my_file.open("log", ios::out);
}

data_type* my_class::retrieve()
{
  data_type *temp = NULL;
  sem_wait(&mutex);
  if(my_data -> next != NULL)
  {   
    temp = my_data;
    my_data = my_data -> next;
  }
  sem_post(&mutex);
  return my_data;
}

void my_class::add(string data) 
{
  data_type *temp = new data_type();
  temp -> data = data;
  data_type *top;
  sem_wait(&mutex);
  top = my_data;
  while(top -> next -> next) //adds it to the end. The end's next is set to NULL
  {
    top = top -> next;
  }
  top -> next = temp;
  my_file << name << "\n";
  sem_post(&mutex);
}

What I'm really looking for is a way to share an instance of this class as a pointer. This way, I can then have threads that can access this instance. I think because of how much sharing I want to do, it needs to go on the heap and not the stack.

I wouldn't consider making this its own program and then using networking i\o to interact because of how simple it is. Needless to say, this is not exactly what I'm doing; however I think I've made a simplified\generic enough example that if this can be solved then I can easily apply it to my solution and it might help others.

Again, I'm looking for a way to share one instance of this code between two separate processes.

I don't know if this can be done because the class has a linked list in it let alone a file in it. If it can, then whose heap and file table does it fill (both?).

EDIT:

Thanks for the help so far; however, it should be worth pointing out that both processes may not be running at the same time. One acts as a daemon and the other will appears intermittently. Both programs already have threads, so that's why I want to do it on the heap.

trincot
  • 317,000
  • 35
  • 244
  • 286
SailorCire
  • 548
  • 1
  • 7
  • 24
  • First there will be no waist, as fork has super cow powers: the processes will share the code. Second fork does not result in shared memory, you need to allocate shared memory. – ctrl-alt-delor Feb 10 '14 at 23:24
  • Lookup named shared memory. – ctrl-alt-delor Feb 10 '14 at 23:28
  • 1
    http://stackoverflow.com/questions/13274786/how-to-share-memory-between-process-fork – ctrl-alt-delor Feb 10 '14 at 23:32
  • @richard Sharing code is the opposite of what I'm looking for. If the code segment of the program **is shared** then it would be a huge waste. It would be like including code to download a file in a webserver instead of just leaving that in the web browser. – SailorCire Feb 10 '14 at 23:33
  • Your choices are shared memory of some flavor, or communications of some flavor. Processes normally maintain separate memory spaces, with no ability to address each others' contents. – keshlam Feb 10 '14 at 23:54
  • I know you are not trying to share code, I am just correcting an error in the question. When forking shares the code segment **there is no waste**, it is shared. It uses cow (copy on write) powers: If it is not modified it does **not** make a copy. Any page (not segment) that is changed is copied. The code segment can not be modified (though it can replaced with exec). – ctrl-alt-delor Feb 11 '14 at 12:36
  • @richard Ah...I understand now, thanks. Some reason I thought fork make a complete copy of everything (except the heap). – SailorCire Feb 11 '14 at 14:55
  • Yes it even “copies” the heap, using cow powers, so real cheep. – ctrl-alt-delor Feb 12 '14 at 11:42

1 Answers1

1

You cannot share memory that is on the heap between processes. Using mmap() with MAP_ANON | MAP_SHARED you can share whole pages, but not on the heap. Using shm_open etc. you can share other objects, but again not on the heap.

Perhaps what you want is threads. These will allow you to share items on the heap.

I think your understanding of fork() is garbled. fork() will result in a copy-on-write image of your program in memory. As your code won't be written to, if you don't exec(), it will be only use one copy of physical memory. If you exec() a different version of your program (e.g. if the producer exec()s the consumer), it's likely to be less memory efficient than having it all in one place and fork()ing. And in either case you are going to have the overhead of some sort of IPC. Threads here seem a far better solution.

abligh
  • 24,573
  • 4
  • 47
  • 84
  • I agree. Though on Linux the distinction between thread and process is not so clear cut. Processes don't have the performance problems of MS-Windows. There is little difference between 2 processes sharing all memory and a process with 2 threads. – ctrl-alt-delor Feb 11 '14 at 12:19
  • threads won't work as the two processes may not be run at the same time. I think I'm going to edit the original question to reflect that. – SailorCire Feb 11 '14 at 14:49