Windows Shared Memory sample

Source: Internet
Author: User

Original: http://blog.csdn.net/ljianhui/article/details/10253345

Shared memory is mainly achieved through the mapping mechanism.
The address space of processes under Windows is logically isolated from each other, but is physically overlapping. The so-called overlap means that the same piece of memory may be used by multiple processes simultaneously. When calling CreateFileMapping to create a named memory-mapped file object, Windows is requesting a block of memory of the specified size in physical memory, returning a handle to the file-mapping object Hmap. In order to be able to access this area of memory, the MapViewOfFile function must be called, prompting Windows to map this memory space to the process's address space. When other processes access this area of memory, you must use the OpenFileMapping function to obtain the object handle Hmap and call the MapViewOfFile function to get a mapping of this memory space. In this way, the system maps the same piece of memory to the address space of different processes, thus achieving the purpose of shared memory.

The following example shows how to use a memory-mapped file for shared memory.
The first time you run this example, it creates the shared memory and writes the data "This is common data!". As long as the process of creating the shared memory does not close the handle HMAP, the program that runs later reads the data from the shared memory and prints it out. This is the process of using inter-process communication within a share. The program code is as follows.

[CSharp]View PlainCopy
  1. #include <windows.h>
  2. #include <iostream>
  3. #include <string>
  4. #include <cstring>
  5. Using namespace std;
  6. int main ()
  7. {
  8. string Strmapname ("sharememory"); //memory-mapped object name
  9. string Strcomdata ("This is common data!"); //Shared in-memory data
  10. LPVOID pbuffer; //Shared memory pointers
  11. //First attempt to open a named memory-mapped file object
  12. HANDLE Hmap =:: OpenFileMapping (file_map_all_access, 0, Strmapname.c_str ());
  13. if (NULL = = Hmap)
  14. { //open failed, created
  15. Hmap =:: CreateFileMapping (Invalid_handle_value,
  16. Null
  17. Page_readwrite,
  18. 0,
  19. Strcomdata.length () +1,
  20. Strmapname.c_str ());
  21. //Map A view of the object, get pointers to shared memory, set the data inside
  22. pbuffer =:: MapViewOfFile (Hmap, file_map_all_access, 0, 0, 0);
  23. strcpy ((char*) pbuffer, Strcomdata.c_str ());
  24. cout << "Write Shared memory data:" << (char *) pbuffer << Endl;
  25. }
  26. Else
  27. { //open successfully, map object to a view, get pointer to shared memory, show the data inside
  28. pbuffer =:: MapViewOfFile (Hmap, file_map_all_access, 0, 0, 0);
  29. cout << "read shared memory data:" << (char *) pbuffer << Endl;
  30. }
  31. GetChar (); //Note that after the process is closed, all handles are automatically closed, so pause here
  32. //de-file mapping, close memory-mapped file object handle
  33. :: UnmapViewOfFile (pbuffer);
  34. :: CloseHandle (HMAP);
  35. System ("pause");
  36. return 0;
  37. }

Run the program twice in a row, and their output is as follows:

Top
0
Step

Windows Shared Memory sample

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.