Windows Shared Memory sample

Source: Internet
Author: User

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.

1#include <windows.h>2#include <iostream>3#include <string>4#include <cstring>5 using namespacestd;6 7 intMain ()8 {9     stringStrmapname ("sharememory");//memory-Mapped object nameTen     stringStrcomdata ("This is common data!");//sharing in-memory data OneLPVOID pbuffer;//Shared Memory Pointers A  -     //first attempt to open a named memory-mapped file object -HANDLE Hmap =:: OpenFileMapping (File_map_all_access,0, Strmapname.c_str ()); the     if(NULL = =hmap) -{//Open failed, created -Hmap =:: CreateFileMapping (Invalid_handle_value, - NULL, + Page_readwrite, -                                    0, +Strcomdata.length () +1, A strmapname.c_str ()); at         //A view of the mapped object that gets a pointer to the shared memory, sets the data inside -pbuffer =:: MapViewOfFile (Hmap, File_map_all_access,0,0,0); -strcpy ((Char*) pbuffer, Strcomdata.c_str ()); -cout <<"Write shared memory data:"<< (Char*) Pbuffer <<Endl; -     } -     Else in{//Open Success, a view of the mapped object, a pointer to the shared memory, showing the data inside -pbuffer =:: MapViewOfFile (Hmap, File_map_all_access,0,0,0); tocout <<"read shared memory data:"<< (Char*) Pbuffer <<Endl; +     } -  the     //to dismiss a file mapping, close the memory-mapped file object handle * :: UnmapViewOfFile (pbuffer); $ :: CloseHandle (HMAP);Panax NotoginsengSystem"Pause");//Note that after the process is closed, all handles are automatically closed, so pause here -     return 0; the}

Windows Shared Memory sample

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.