c# 通過內存映射實現(xiàn)文件共享內存的示例代碼
內存映射文件是利用虛擬內存把文件映射到進程的地址空間中去,在此之后進程操作文件,就像操作進程空間里的地址一樣了,比如使用c語言的 memcpy等內存操作的函數(shù)。這種方法能夠很好的應用在需要頻繁處理一個文件或者是一個大文件的場合,這種方式處理IO效率比普通IO效率要高
共享內存是內存映射文件的一種特殊情況,內存映射的是一塊內存,而非磁盤上的文件。共享內存的主語是進程(Process),操作系統(tǒng)默認會給每一 個進程分配一個內存空間,每一個進程只允許訪問操作系統(tǒng)分配給它的哪一段內存,而不能訪問其他進程的。而有時候需要在不同進程之間訪問同一段內存,怎么辦 呢?操作系統(tǒng)給出了創(chuàng)建訪問共享內存的API,需要共享內存的進程可以通過這一組定義好的API來訪問多個進程之間共有的內存,各個進程訪問這一段內存就 像訪問一個硬盤上的文件一樣。而.Net 4.0中引入了System.IO. MemoryMappedFiles命名空間,這個命名空間的類對windows 共享內存相關API做了封裝,使.Net程序員可以更方便的使用內存映射文件。
在C#中使用共享內存。以下App1的代碼讓用戶輸入一行文本到共享內存中;App2不停的刷新控制臺,輸出最新的共享內存內容;App3實現(xiàn)的功能和App2相同,但讀取方法不同。
App1代碼:
using System; using System.Collections.Generic;android從資源文件中讀取文件流顯示 using System.Linq; using System.Text; using System.IO; //引用內存映射文件命名空間 using System.IO.MemoryMappedFiles; namespace App1 { class Program { static void Main(string[] args) { long capacity = 1<<10<<10; //創(chuàng)建或者打開共享內存 using (var mmf = MemoryMappedFile.CreateOrOpen("testMmf", capacity, MemoryMappedFileAccess.ReadWrite)) { //通過MemoryMappedFile的CreateViewAccssor方法獲得共享內存的訪問器 var viewAccessor = mmf.CreateViewAccessor(0, capacity); //循環(huán)寫入,使在這個進程中可以向共享內存中寫入不同的字符串值 while (true) { Console.WriteLine("請輸入一行要寫入共享內存的文字:"); string input = Console.ReadLine(); //向共享內存開始位置寫入字符串的長度 viewAccessor.Write(0, input.Length); //向共享內存4位置寫入字符 viewAccessor.WriteArray<char>(4, input.ToArray(), 0, input.Length); } } } } }
App2代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; //引用使用內存映射文件需要的命名空間 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); //循環(huán)刷新共享內存字符串的值 while (true) { //讀取字符長度 int strLength = viewAccessor.ReadInt32(0); char[] charsInMMf = new char[strLength]; //讀取字符 viewAccessor.ReadArray<char>(4, charsInMMf, 0, strLength); Console.Clear(); Console.Write(charsInMMf); Console.Write("\r"); Thread.Sleep(200); } } } } }
App3代碼:
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 << 10; //打開共享內存 using (var mmf = MemoryMappedFile.OpenExisting("testMmf")) { //使用CreateViewStream方法返回stream實例 using (var mmViewStream = mmf.CreateViewStream(0, capacity)) { //這里要制定Unicode編碼否則會出問題 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(); } } } } } } }
在讀數(shù)據時用了2種方法。
因為在之前很少會用到進程之間的通信,所以此方法只是想初步的認識下。此程序寫的過于簡陋,有很多東西都沒有去判斷。比如說是怎么創(chuàng)建了一個共享內存怎么取刪除它等等。。。
以上就是c# 通過內存映射實現(xiàn)文件共享內存的示例代碼的詳細內容,更多關于c# 實現(xiàn)文件共享內存的資料請關注腳本之家其它相關文章!
相關文章
C#具名參數(shù)(Named Parameters)的使用
在C#中,具名參數(shù)是一種在方法調用中使用參數(shù)名稱來指定參數(shù)值的技術,本文主要介紹了C#具名參數(shù)的使用,具有一定的參考價值,感興趣的可以了解一下2024-01-01