您的位置:首页 > 其它

System.IO之内存映射文件共享内存

2011-08-01 08:53 253 查看
内存映射文件是利用虚拟内存把文件映射到进程的地址空间中去,在此之后进程操作文件,就像操作进程空间里的地址一样了,比如使用c语言的memcpy等内存操作的函数。这种方法能够很好的应用在需要频繁处理一个文件或者是一个大文件的场合,这种方式处理IO效率比普通IO效率要高

共享内存是内存映射文件的一种特殊情况,内存映射的是一块内存,而非磁盘上的文件。共享内存的主语是进程(Process),操作系统默认会给每一个进程分配一个内存空间,每一个进程只允许访问操作系统分配给它的哪一段内存,而不能访问其他进程的。而有时候需要在不同进程之间访问同一段内存,怎么办呢?操作系统给出了创建访问共享内存的API,需要共享内存的进程可以通过这一组定义好的API来访问多个进程之间共有的内存,各个进程访问这一段内存就像访问一个硬盘上的文件一样。而.Net 4.0中引入了System.IO. MemoryMappedFiles命名空间,这个命名空间的类对windows
共享内存相关API做了封装,使.Net程序员可以更方便的使用内存映射文件。

在C#中使用共享内存。以下App1的代码让用户输入一行文本到共享内存中;App2不停的刷新控制台,输出最新的共享内存内容;App3实现的功能和App2相同,但读取方法不同。

App1代码:

using System;
using System.Collections.Generic;
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;

//创建或者打开共享内存
using (var mmf = MemoryMappedFile.CreateOrOpen("testMmf", capacity, MemoryMappedFileAccess.ReadWrite))
{
//通过MemoryMappedFile的CreateViewAccssor方法获得共享内存的访问器
var viewAccessor = mmf.CreateViewAccessor(0, capacity);
//循环写入,使在这个进程中可以向共享内存中写入不同的字符串值
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);

//循环刷新共享内存字符串的值
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();
}
}
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: