Original: http://blog.csdn.net/wangtiewei/article/details/51112668
A memory-mapped file uses virtual memory to map a file to the address space of a process, after which the process operates the file as if it were an address in the process space, such as a function that uses memory operations such as C-memcpy. This method can be used very well in situations where a file or a large file is required to be processed frequently, which is more efficient than normal IO in this way.
Shared memory is a special case of memory-mapped files, where memory is mapped to a piece of memory rather than to a file on disk. The subject of shared memory is a process, and the operating system allocates a memory space by default for each process, and each process only allows access to the memory allocated to it by the operating system and not to other processes. And sometimes you need to access the same memory between different processes. The operating system gives an API to create access to shared memory, and processes that require shared memory can access memory that is shared between multiple processes through this set of defined APIs, and each process accesses the memory as if it were a file on a hard disk. System.IO was introduced in. Net 4.0. The MemoryMappedFiles namespace, the namespace class, encapsulates the Windows shared memory-related APIs, making it easier for. NET programmers to use memory-mapped files.
Use shared memory in C #. The following APP1 code lets the user enter a line of text into the shared memory; APP2 refreshes the console continuously, outputting the latest shared memory content, APP3 implements the same functionality as APP2, but with different reading methods.
[CSharp]View PlainCopy
- APP1 Code:
- Using System;
- Using system.collections.generic;android reads the file stream display from the resource file
- Using System.Linq;
- Using System.Text;
- Using System.IO;
- Referencing a memory-mapped file namespace
- Using System.IO.MemoryMappedFiles;
- Namespace App1
- {
- Class Program
- {
- static void Main (string[] args)
- {
- Long capacity = 1<<10<<10;
- //Create or open shared memory
- using (var mmf = Memorymappedfile.createoropen ("TESTMMF", capacity, memorymappedfileaccess.readwrite))
- {
- //gain access to shared memory through MemoryMappedFile's Createviewaccssor method
- var viewaccessor = mmf. Createviewaccessor (0, capacity);
- //cyclic write so that different string values can be written to shared memory in this process
- While (true)
- {
- Console.WriteLine ("Please enter a line of text to write to the shared memory:");
- string input = Console.ReadLine ();
- //write the length of the string to the shared memory start location
- Viewaccessor.write (0, input. Length);
- //write characters to shared memory 4 location
- viewaccessor.writearray<char> (4, input. ToArray (), 0, input. Length);
- }
- }
- }
- }
- }
[CSharp]View PlainCopy
- APP2 Code:
- Using System;
- Using System.Collections.Generic;
- Using System.Linq;
- Using System.Text;
- Using System.Threading;
- Referencing namespaces required for using memory-mapped files
- Using System.IO.MemoryMappedFiles;
- Namespace App2
- {
- Class Program
- {
- static void Main (string[] args)
- {
- Long capacity = 1<<10<<10;
- using (var mmf = memorymappedfile.openexisting ("TESTMMF"))
- {
- Memorymappedviewaccessor viewaccessor = mmf. Createviewaccessor (0, capacity);
- //Cycle refresh value of shared memory string
- While (true)
- {
- //Read character length
- int strlength = viewaccessor.readint32 (0);
- char[] charsinmmf = new char[strlength];
- //Read characters
- viewaccessor.readarray<char> (4, CHARSINMMF, 0, strlength);
- Console.clear ();
- Console.Write (CHARSINMMF);
- Console.Write ("\ r");
- Thread.Sleep (200);
- }
- }
- }
- }
- }
[CSharp]View PlainCopy
- APP3 Code:
- Using System;
- Using System.Collections.Generic;
- Using System.Linq;
- Using System.Text;
- Using System.IO.MemoryMappedFiles;
- Using System.IO;
- Namespace APP3
- {
- Class Program
- {
- static void Main (string[] args)
- {
- Long capacity = 1 << << 10;
- //Open Shared memory
- using (var mmf = memorymappedfile.openexisting ("TESTMMF"))
- {
- //Use the Createviewstream method to return a stream instance
- using (var mmviewstream = mmf. Createviewstream (0, capacity))
- {
- //There will be a problem with Unicode encoding
- using (BinaryReader RDR = new BinaryReader (Mmviewstream,encoding.unicode))
- {
- While (true)
- {
- Mmviewstream.seek (0, Seekorigin.begin);
- int length = rdr. ReadInt32 ();
- char[] chars = rdr. ReadChars (length);
- Console.Write (chars);
- Console.Write ("\ r");
- System.Threading.Thread.Sleep (200);
- Console.clear ();
- }
- }
- }
- }
- }
- }
- }
There are 2 ways to read the data.
Because the communication between processes is seldom used before, this method just wants to get a preliminary understanding. This program is too simple to write, there are many things are not to judge. For example, how to create a shared memory how to delete it and so on ...
I hope I share it with the author of this blog post.
C # implements memory-mapped file share memory