Memory ing file shared memory between processes

Source: Internet
Author: User

Memory ing file shared memory between processes

 

Another function of the memory ing file is to share data between processes. It provides an effective way for different processes to share memory.

And a simple method. Shared memory will be used in many examples later.

The shared memory is mainly implemented through the ing mechanism.

In Windows, the address spaces of processes are logically isolated from each other, but physically they overlap. The so-called weight

Stack means that the same memory area may be used by multiple processes at the same time. When createfilemapping is called to create the named Memory

When you map a file object, Windows requests a memory area of the specified size in the physical memory and returns the file ing object.

Hmap. To access this memory area, you must call the mapviewoffile function

This memory space is mapped to the address space of the process. When other processes Access this memory area, you must use

The openfilemapping function retrieves the object handle hmap and calls the mapviewoffile function to obtain

Mappings. In this way, the system maps the same memory area to the address space of different processes to achieve sharing.

Memory.

The following example shows how to use the memory ing file for shared memory.

When this example is run for the first time, it creates a shared memory and writes "123456" of data ". As long as shared memory is created

The process does not close the hmap handle. Later programs will read and print the data in the shared memory. This

It is the process of communication between processes in the sharing. The program code is as follows.

Void main () // under the 08sharemem Project

{

Char szname [] = "08 sharemem"; // name of the memory ing object

Char szdata [] = "123456"; // data in the shared memory

Lpvoid pbuffer; // shared memory pointer

// First try to open a named Memory ing file object

Handle hmap =: openfilemapping (file_map_all_access, 0,
Szname );

If (hmap! = NULL)

{// Open successfully. Map a view of the object to get a pointer to the shared memory, showing the data in it.

Pbuffer =: mapviewoffile (hmap, file_map_all_access, 0, 0, 0 );

Printf ("read shared memory data:" % s "\ n", (char *) pbuffer );

}

Else

{// Open failed, created

Hmap =: createfilemapping (

Invalid_handle_value, // No physical file

Null,

Page_readwrite,

0,

Strlen (szdata) + 1,

"08 sharemem"); // name

// Map a view of the object to get a pointer to the shared memory and set the data in it.

Pbuffer =: mapviewoffile (hmap, file_map_all_access, 0, 0, 0 );

Strcpy (char *) pbuffer, szdata );

Printf ("Write shared memory data:" % s "\ n", (char *) pbuffer );

}

// Unbind the file ing and disable the memory ing file object handle.

: Unmapviewoffile (pbuffer );

: Closehandle (hmap );

Getchar (); // note that after the process is closed, all handles are automatically closed, so pause here.

Return;

}

Run the program twice in a row. The output results are shown in.

Figure using communication between processes in a shared file

 

Encapsulate shared memory class csharememory

For ease of use, this book encapsulates the shared memory function into the csharememory class, the Code is as follows.

# In the ifndef _ sharememory_h _ // sharememory. h file

# DEFINE _ sharememory_h __

Class csharememory

{

Public:

// Constructor and destructor

Csharememory (const char * pszmapname, int nfilesize = 0, bool bserver = false );

~ Csharememory ();

// Attributes

Lpvoid getbuffer () const {return m_pbuffer ;}

// Implementation

PRIVATE:

Handle m_hfilemap;

Lpvoid m_pbuffer;

};

Inline csharememory: csharememory (const char * pszmapname,

Int nfilesize, bool bserver): m_hfilemap (null), m_pbuffer (null)

{

If (bserver)

{// Create a memory ing file object

M_hfilemap = createfilemapping (invalid_handle_value,

Null, page_readwrite, 0, nfilesize, pszmapname );

}

Else

{// Open a memory ing file object

M_hfilemap = openfilemapping (file_map_all_access, false, pszmapname );

}

// Map it To the memory to obtain the first address of the shared memory.

M_pbuffer = (lpbyte) mapviewoffile (

M_hfilemap,

File_map_all_access,

0,

0,

0 );

}

Inline csharememory ::~ Csharememory ()

{

// Cancel file ing and close the file ing object handle

Unmapviewoffile (m_pbuffer );

Closehandle (m_hfilemap );

}

# Endif/_ sharememory_h __

Shared Memory is mainly used for inter-process communication. Both parties are the server side and the customer side. Service

The user creates a named Memory ing file object. The user opens the object. Functions of both parties in mapviewoffile

Exchange data in the returned shared memory.

The csharememory class is designed based on this principle. The server must

Specify the file size (the size of the shared memory) and set the bserver parameter to true. The client only specifies the object name.

That's all. The unique interface function of the class getbuffer returns a pointer to the shared memory.

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.