Friday, September 5, 2014

How to use shared memory for process intercommunication on Windows?

Introduction
Shared memory is the fastest way for process intercommunication. In the other hand it is the most danger. One mistake in code will crash your program at once.

Shared memory server
Here is an example of shared memory server that allocates shared memory and writes there a message.
#include <windows.h>
#include <conio.h>
#include <stdio.h>

const unsigned int c_uiSharedMemorySegmentSize = 512;

const char * szcSharedMemorySegmentName = "Global\\HelloMessage";

int main()
{
 printf("Shared memory server has been started successfully.\n");

 HANDLE hMapFile = CreateFileMapping(
    INVALID_HANDLE_VALUE,   // use paging file
    NULL,       // default security
    PAGE_READWRITE,     // read/write access
    0,        // maximum object size (high-order DWORD)
    c_uiSharedMemorySegmentSize, // maximum object size (low-order DWORD)
    szcSharedMemorySegmentName); // name of mapping object

 if (hMapFile == NULL)
 {
  fprintf(stderr, "Could not create file mapping object (%d).\n", GetLastError());
  return 1;
 }

 char * pcBuf = (char *) MapViewOfFile(hMapFile,   // handle to map object
  FILE_MAP_ALL_ACCESS, // read/write permission
  0,
  0,
  c_uiSharedMemorySegmentSize);

 if (pcBuf == NULL)
 {
  fprintf(stderr, "Could not map view of file (%d).\n", GetLastError());
  CloseHandle(hMapFile);
  return 2;
 }

 // Set memory for child process
 const char * szcMessageHelloWorld = "Hello world!";
 strcpy(pcBuf, szcMessageHelloWorld);

 printf("Message has been written to shared memory segment.\n");
 printf("You can start the client to read it.\n");
 printf("Press any key to remove shared memory segment and exit ...\n");

 getch();

 UnmapViewOfFile(pcBuf);

 CloseHandle(hMapFile);

 return 0;
}

Shared memory client
Here is an example of shared memory client that attaches to the shared memory and reads the message from the server.
#include <windows.h>
#include <stdio.h>
#include <conio.h>

const unsigned int c_uiSharedMemorySegmentSize = 512;
const char *  szcSharedMemorySegmentName = "Global\\HelloMessage";

int main()
{
 printf("Shared memory client has been started.\n");
 printf("Press any key to read the message from shared memory.\n");
 getch();

 HANDLE hMapFile = OpenFileMapping(
     FILE_MAP_ALL_ACCESS,   // read/write access
     FALSE,       // do not inherit the name
     szcSharedMemorySegmentName); // name of mapping object

 if (hMapFile == NULL)
 {
  fprintf(stderr, "Could not open file mapping object (%d).\n", GetLastError());
  return 1;
 }

 const char * pcBuf = static_cast<char *> (MapViewOfFile(hMapFile, // handle to map object
  FILE_MAP_ALL_ACCESS,           // read/write permission
  0,
  0,
  c_uiSharedMemorySegmentSize));

 if (pcBuf == NULL)
 {
  fprintf(stderr, "Could not map view of file (%d).\n", GetLastError());
  CloseHandle(hMapFile);
  return 2;
 }

 printf("Message from shared memory server: '%s'\n", pcBuf);
 printf("Press any key to exit ...");
 getch();

 UnmapViewOfFile(pcBuf);
 CloseHandle(hMapFile);

 return 0;
}
After you successfully built both examples you can start server at first:

Then start shared memory client and press any key:

As you can see client successfully reads the message from server.

Hope this post is useful. Good luck!


No comments:

Post a Comment