简体   繁体   中英

Multi-processing with singletons C++ on Linux x86_64

For the following question, I am looking for an answer that is based on "pure" C/C++ fundamentals, so I would appreciate a non-Boost answer. Thanks.

I have an application (for example, a telecommunications infrastructure server) which will, when started, spawn several processes on a Linux environment (one for logging, one for Timer management, one for protocol messaging, one for message processing etc.). It is on an x86_64 environment on Gentoo. The thing is, I need a singleton to be able to be accessible from all the processes.

This is different from multi-threading using say, POSIX threads, on Linux because the same address space is used by all POSIX threads, but that is not the case when multiple processes, generated by fork () function call, is used. When the same address space is used, the singleton is just the same address in all the threads, and the problem is trivially solved (using the well known protections, which are old hat for everybody on SO). I do enjoy the protections offered to me by multiple processes generated via fork().

Going back to my problem, I feel like the correct way to approach this would be to create the singleton in shared memory, and then pass a handle to the shared memory into the calling tasks.

I imagine the following (SomeSingleton.h):

#include <unistd.h>
#... <usual includes>
#include "SomeGiantObject.h"

int size = 8192; // Enough to contain the SomeSingleton object
int shm_fd = shm_open ("/some_singleton_shm", O_CREAT | O_EXCL | O_RDWR, 0666);
ftruncate (shm_fd, size);
sharedMemoryLocationForSomeSingleton = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);

class SomeSingleton
{
   public:
     SomeSingleton* getInstance ()
     {
        return reinterpret_cast<SomeSingleton*>sharedMemoryLocationForSomeSingleton;
     }
   private:
     SomeSingleton();
     /*
        Whole bunch of attributes that is shared across processes.
        These attributes also should be in shared memory.
        e.g., in the following
        SomeGiantObject* obj;
        obj should also be in shared memory.
     */
};

The getInstance() method returns the shared memory location for the SomeSingleton object.

My questions are as follows:

  1. Is this a legitimate way to handle the problem? How have folks on SO handled this problem before?
  2. For the code above to work, I envision a global declaration (static by definition) that points to the shared memory as shown before the class declaration.
  3. Last, but not the least, I know that on Linux, the overheads of creating threads vs. processes is "relatively similar," but I was wondering why there is not much by way of multi-processing discussions on SO (gob loads of multi-threading, though!). There isn't even a tag here! Has multi-processing (using fork()) fallen off favors among the C++ coding community? Any insight on that is also appreciated. Also, may I request someone with a reputation > 1500 to create a tag "multi-processing?" Thanks.

If you create the shared memory region before forking, then it will be mapped at the same address in all peers.

You can use a custom allocator to place contained objects inside the shared region also. This should probably be done before forking as well, but be careful of repetition of destructor calls (destructors that eg flush buffers are fine, but anything that makes an object unusable should be skipped, just leak and let the OS reclaim the memory after all processes close the shared memory handle).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM