C # implements memory-mapped file share memory

Source: Internet
Author: User

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
  1. APP1 Code:
  2. Using System;
  3. Using system.collections.generic;android reads the file stream display from the resource file
  4. Using System.Linq;
  5. Using System.Text;
  6. Using System.IO;
  7. Referencing a memory-mapped file namespace
  8. Using System.IO.MemoryMappedFiles;
  9. Namespace App1
  10. {
  11. Class Program
  12. {
  13. static void Main (string[] args)
  14. {
  15. Long capacity = 1<<10<<10;
  16. //Create or open shared memory
  17. using (var mmf = Memorymappedfile.createoropen ("TESTMMF", capacity, memorymappedfileaccess.readwrite))
  18. {
  19. //gain access to shared memory through MemoryMappedFile's Createviewaccssor method
  20. var viewaccessor = mmf. Createviewaccessor (0, capacity);
  21. //cyclic write so that different string values can be written to shared memory in this process
  22. While (true)
  23. {
  24. Console.WriteLine ("Please enter a line of text to write to the shared memory:");
  25. string input = Console.ReadLine ();
  26. //write the length of the string to the shared memory start location
  27. Viewaccessor.write (0, input. Length);
  28. //write characters to shared memory 4 location
  29. viewaccessor.writearray<char> (4, input. ToArray (), 0, input.  Length);
  30. }
  31. }
  32. }
  33. }
  34. }

[CSharp]View PlainCopy
  1. APP2 Code:
  2. Using System;
  3. Using System.Collections.Generic;
  4. Using System.Linq;
  5. Using System.Text;
  6. Using System.Threading;
  7. Referencing namespaces required for using memory-mapped files
  8. Using System.IO.MemoryMappedFiles;
  9. Namespace App2
  10. {
  11. Class Program
  12. {
  13. static void Main (string[] args)
  14. {
  15. Long capacity = 1<<10<<10;
  16. using (var mmf = memorymappedfile.openexisting ("TESTMMF"))
  17. {
  18. Memorymappedviewaccessor viewaccessor = mmf. Createviewaccessor (0, capacity);
  19. //Cycle refresh value of shared memory string
  20. While (true)
  21. {
  22. //Read character length
  23. int strlength = viewaccessor.readint32 (0);
  24. char[] charsinmmf = new char[strlength];
  25. //Read characters
  26. viewaccessor.readarray<char> (4, CHARSINMMF, 0, strlength);
  27. Console.clear ();
  28. Console.Write (CHARSINMMF);
  29. Console.Write ("\ r");
  30. Thread.Sleep (200);
  31. }
  32. }
  33. }
  34. }
  35. }

[CSharp]View PlainCopy
  1. APP3 Code:
  2. Using System;
  3. Using System.Collections.Generic;
  4. Using System.Linq;
  5. Using System.Text;
  6. Using System.IO.MemoryMappedFiles;
  7. Using System.IO;
  8. Namespace APP3
  9. {
  10. Class Program
  11. {
  12. static void Main (string[] args)
  13. {
  14. Long capacity = 1 << << 10;
  15. //Open Shared memory
  16. using (var mmf = memorymappedfile.openexisting ("TESTMMF"))
  17. {
  18. //Use the Createviewstream method to return a stream instance
  19. using (var mmviewstream = mmf. Createviewstream (0, capacity))
  20. {
  21. //There will be a problem with Unicode encoding
  22. using (BinaryReader RDR = new BinaryReader (Mmviewstream,encoding.unicode))
  23. {
  24. While (true)
  25. {
  26. Mmviewstream.seek (0, Seekorigin.begin);
  27. int length = rdr.  ReadInt32 ();
  28. char[] chars = rdr.  ReadChars (length);
  29. Console.Write (chars);
  30. Console.Write ("\ r");
  31. System.Threading.Thread.Sleep (200);
  32. Console.clear ();
  33. }
  34. }
  35. }
  36. }
  37. }
  38. }
  39. }

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

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.