0

I'm using MMF as a way to share memory between multiple processes.

In my app, I have a process that creates a file mapping object that writes a bool value and other processes access(read) that value.

// First Process (Server)
// main.cpp
BOOL *pbVal;
HANDLE hFile = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4, L"Global\\Test123");
if (hFile)
{
    pbVal = (BOOL *) MapViewOfFile(hFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
    if (pbVal)
    {
        Logger.LogPrint(_T("Mapped Memory."));
        *pbVal = TRUE;
        UnmapViewOfFile(pbVal);
    }
}
...
...
//closes the handle after use
// Other Processes (Clients)
// main.cpp
BOOL *pBool;
HANDLE hMapFile = OpenFileMappingW(FILE_MAP_READ, FALSE, L"Global\\Test123");
if (hMapFile)
{
    pBool = (BOOL*) MapViewOfFile(hMapFile, FILE_MAP_READ, 0 ,0, 4);
    if (pBool)
    {
        if (*pBool)
        {
            // Do Something
        }
        UnMapViewOfFile(pBool);
    }
}
CloseHandle(hMapFile);

I've skimmed through MSDN article Managing Memory-Mapped Files and some other articles and I understood MMF performs better than traditional file I/O in most cases. But, I want to know about the cost of MMF when it is used as a mean of IPC.

Because the fact that this method still involves I/O, I'm worried if accessing the shared memory consistently would harm the performance of my app.

Thanks in advance.

11
  • 1
    Sounds like a case of premature optimization to me, if it's good enough for whatever you are doing then don't worry about it. Although I would probably do this via a DLL load-time linked with both executibles that hosts a shared memory segment. Simply because while it would have all the same code steps the OS would take care of the actual mechanics. May 20, 2020 at 3:41
  • 1
    "Because the fact that this method still involves I/O, I'm worried if accessing the shared memory consistently would harm the performance of my app. " I wouldn't say this is I/O in the traditional sense, as it is not looking up a file in a HDD, or waiting for user interaction. In any case it's difficult to say if it would harm performance of your code, because you aren't comparing it to anything else.
    – Roy2511
    May 20, 2020 at 4:33
  • 1
    Your other options seems to be to use something like a pipe, or a tcp connection to transfer the boolean. Which honestly seems better (and safer) to me because dealing with construction and destruction (and dereferencing) of shared memory to transfer a bool seems like overkill especially when it seems like you already have a server-client architecture in mind.
    – Roy2511
    May 20, 2020 at 4:38
  • 2
    The cost of OpenFileMapping should be insignificant, assuming you open it just once and reuse the mapping rather than constantly opening and closing (which can get expensive). Once the map has been opened and mapped, it's just regular memory and is as fast as regular memory. May 20, 2020 at 5:35
  • 1
    You're not memory-mapping a file, so this is not I/O -- not anymore than how all physical memory is backed by the system paging file(s). You only need to map the Section object (file mapping) once. Assign the mapped address to a static variable and close the handle. The Section object is just required to setup references to the prototype page-table entries (software PTEs), which the memory manager uses as the true state of shared pages in order to make process hardware PTEs valid, as shared pages transition in and out of the working set of a process.
    – Eryk Sun
    May 20, 2020 at 13:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.