C # Shared Memory grooming

Source: Internet
Author: User

1.system.io.memorymappedfiles Memory-mapped file classes

The memory-mapped file maps the contents of the file to the application's logical address space. memory-mapped files allow programmers to handle huge files (because they can manage memory concurrently), and they allow full random access to files without the need to find files.

createfromfile methods Create a memory-mapped file from a specified path Or a filestream of a existing file on disk. " The CreateFromFile method creates a memory-mapped file based on the specified path or FileStream of an existing file on disk. When mapping files are not mapped, changes are automatically propagated to the disk.

The CreateNew method creates a memory-mapped file that is not mapped to an existing file on disk, and these methods are also suitable for creating shared memory for interprocess communication (IPC).

The memory-mapped file is associated with the name.

You can create multiple views of a memory-mapped file, including a view of some files. you can map the same part of a file to multiple addresses to create concurrent memory. to enable two views to be concurrent, both views must be created based on the same memory-mapped file. Two views that create two file mappings for the same file do not have concurrency. (Https://msdn.microsoft.com/zh-cn/library/system.io.memorymappedfiles.memorymappedfile (v=vs.100). aspx)

Common functions:

[SecurityPermissionAttribute (SecurityAction.Demand, Flags = securitypermissionflag.unmanagedcode)]public static MemoryMappedFileCreateoropen(String mapName,//the name to assign to the memory-mapped file. Long capacity,//the maximum size, in bytes, to allocate to the memory-mapped file. Memorymappedfileaccess access,//One of the enumeration values that specifies the type of access allowed for the memory-mapped file. The default value is ReadWrite. Memorymappedfileoptions options,//A bitwise combination of enumeration values that specifies memory allocation options for memory-mapped files. Memorymappedfilesecurity memorymappedfilesecurity,//file access and Operation permissions can be granted for memory-mapped files. This parameter can be null. Handleinheritability inheritability//One of the enumeration values that specifies whether a handle to a memory-mapped file can be inherited by a child process. The default value is None. )
creates or opens a memory-mapped file in system memory that has the specified capacity, Access type, memory allocation, security permissions, and inheritance.
Use this method to create or open a memory-mapped file that is not persisted (that is, not associated with a file on disk) that you can use to share data between processes.
using (var mmf = Memorymappedfile.createoropen ("TESTMMF", capacity, memorymappedfileaccess.readwrite))
{
   // Obtain   of shared memory by MemoryMappedFile's Createviewaccssor method;
    var Viewaccessor = mmf. Createviewaccessor (0, capacity);
Viewaccessor.write (0, input. Length);
  //write character   to the shared memory 4 position;
   viewaccessor.writearray< Char> (4, input. ToArray (), 0, input. Length);
}
CreateNew (    string mapName,// name to be assigned to the memory-mapped file.)     Long capacity   // The maximum size, in bytes, to allocate to the memory-mapped file. )
Use this method to create a memory-mapped file that is not persisted (that is, not associated with a file on disk) that you can use to share data between processes.
static void Main (string[] args) {using (memorymappedfile mmf = memorymappedfile.createnew ("TestMap", 10000)){bool mutexcreated; Mutex mutex = new Mutex (true, "Testmapmutex", out mutexcreated);using (memorymappedviewstream stream = mmf. Createviewstream ()){BinaryWriter writer = new BinaryWriter (stream); Writer. Write (1);} mutex.         ReleaseMutex (); Mutex.         WaitOne (); using (Memorymappedviewstream stream = mmf.Createviewstream())         {BinaryReader reader = new BinaryReader (stream);Console.WriteLine ("Process A says: {0}", reader.)             Readboolean ()); Console.WriteLine ("Process B says: {0}", reader.)             Readboolean ()); Console.WriteLine ("Process C says: {0}", reader.)         Readboolean ()); } mutex.     ReleaseMutex (); }}

C # Shared Memory grooming

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.