Loading the lesson...
In the print dialog, choose "Save as PDF" as the destination.
Operating Systems 1 - Week 06 Lab
Treating a file on disk as if it were part of your own memory, and sharing that region between processes.
| Advantage | Why it matters |
|---|---|
| Fast access | direct memory access beats traditional file I/O |
| Random access | jump to any position in the file instantly |
| Shared between processes | two programs can map the same region |
| Efficient for large files | only the needed portions are loaded |
using System.IO.MemoryMappedFiles;
var mmf = MemoryMappedFile.CreateFromFile(
filePath, FileMode.OpenOrCreate, null, fileSize);FileMode, an optional mapping name, and the size of the region to reserve.long fileSize = 1024 * // = 1 KB
1024 * // = 1 MB
100; // = 100 MBusing var accessor = mmf.CreateViewAccessor();
string dataToWrite = "Hello, Memory-Mapped Files!";
byte[] dataBytes = Encoding.UTF8.GetBytes(dataToWrite);
accessor.WriteArray(30, dataBytes, 0, dataBytes.Length);WriteArray takes the position in the file, the data, and the offset and count inside that data.byte[] readData = new byte[2];
accessor.ReadArray(34, readData, 0, 2);
string readDataString = Encoding.UTF8.GetString(readData);3034 lands inside that stringCreateFromFile opens or creates the file and reserves the size of the mapped regionWriteArray and ReadArray at any byte positionMahmoud Abas|Operating System Lab - Week 6