什么時候用memory-mapped files:
1.System use it to load and execute .exe and dll files.
2.Use memory-mapped files to access a data file on disk.
3.allow multiple processes running on the same machine to share data with each other.
------------------------------------------------------------------------------------------------------
用途一:使用mapping file executables and dlls
當(dāng)調(diào)用createProcess時候,系統(tǒng)做了以下的事情:
1.locates CreateProcess 中指定的the .exe file,失敗則return false
2.創(chuàng)建新的process kernel object
3.creates a private address space for new process
4.reserves a region of address space large enough to contain the .exe file.The desired location of this region is specified by the .exe file itself.by default ,base address is 0x00400000 .You can change it by create your app .exe fiel using linker’s /BASE option.
5.the systems notes that the physical storage baking the reserved region is in the .exe file on disk instread of the system’s paging file.(系統(tǒng)注意到,支持已保留區(qū)域的物理存儲區(qū)域是在磁盤上的.exe 文件而不是在系統(tǒng)頁面中)
當(dāng)。exe 被mapped into the process address space,系統(tǒng)訪問.exe的一個區(qū)域,那里列出了.exe含有的dll,然后系統(tǒng)利用 LoadLibrary for each dlls。系統(tǒng)map dll時候,如果dll的 preferred base address 被占據(jù)或者不夠大,dllwill try to find another region of address space to reserve for the dll。如果dll 被設(shè)定了/Fixed when it build,也就是不支持重定位,那么加載失敗。
如果加載dll或者exe 失敗,彈出對話框并且釋放申請的地址空間。
after all .exe dll mapped into the process address space, system can begin exec the .exe file starup code. the sys takes care of all paging,buffering, and caching. 例如,如果一段代碼訪問了一個并不在主存的地址,那么一個錯誤產(chǎn)生,系統(tǒng)察覺到錯誤并且自動的調(diào)入page of code from the files image into a page of RAM。
the sys maps the page of ram to the proper location in the process address and allows the thread to continue.
當(dāng)為一個app創(chuàng)建第二個實例時,系統(tǒng)只是簡單的打開另外一個memory-mapped view of file-mapping object that identifies the exec files image and create a new process object and a new thread object.利用內(nèi)存映射文件,多個實例可以共享代碼和數(shù)據(jù)。實際上,file 是分為多個section ,多個節(jié)均對齊于頁邊界。如果一個instance of the app 修改了全局變量,系統(tǒng)應(yīng)用了copy-on-write 機(jī)制,復(fù)制修改的頁面,并更新實例的file-mapping view。當(dāng)我們調(diào)試程序時同樣的事情會發(fā)生,debuger modify code,sys use cow again。
當(dāng)一個進(jìn)程被加載,系統(tǒng)檢查其所有映射文件頁,系統(tǒng)將所有通常用cow保護(hù)的頁面提交給存儲系統(tǒng),這些頁面僅僅是被提交,當(dāng)文件被訪問的時候,系統(tǒng)讀入相應(yīng)的頁面,如果頁面沒有被修改,那么他們可以被換出,如果已經(jīng)修改,系統(tǒng)將修改過的頁面轉(zhuǎn)入已經(jīng)提交的頁面之一(這點很晦澀啊 system swaps the modified page to one of the perviously committed pages in the paging file ,怎么翻譯呢~~~~ :( )
------------------------------------------------------------------------------------------------------
在可執(zhí)行文件或者dll中共享靜態(tài)變量
------------------------------------------------------------------------------------------------------
內(nèi)存映射數(shù)據(jù)文件
例子:要把一個文件所有字節(jié)倒置
如果利用file mapping 我們告訴系統(tǒng)使用一個虛擬空間的區(qū)域來倒置文件,然后告訴把文件的第一個字節(jié)映射到保留空間的第一個字節(jié),然后就像處理內(nèi)存中的字符串一樣處理文件即可,引文系統(tǒng)會幫助你完成文件緩存以及調(diào)頁等工作。
使用流程:
1.創(chuàng)建一個file kernel object that identifies the file on disk that you want to use as a memory –mapped file
2.創(chuàng)建一個file mapping kernel object 告訴系統(tǒng)文件的大小,以及你準(zhǔn)備如何訪問文件
3.告訴系統(tǒng)map all或者部分文件到你的進(jìn)程地址空間
當(dāng)使用結(jié)束后要:
1告訴系統(tǒng) unmap file-mapping kernel object form your process add
2cloes filemapping kernel object
3close file kernel object
---------
具體步驟
--1. 創(chuàng)建文件內(nèi)核對象
CreateFile
失敗返回 INVALID_HANDLE_VALUE = –1 一般來說windows func 失敗return null這個比較特殊
createfile dwdesiredAccess 需要設(shè)置為 GENERIC_READ 或者 GENERIC_WRITE
--2. 創(chuàng)建file-mapping 內(nèi)核對象
CreatefileMapping(HANDLE,PSECURITY_ATTRIBUTES,DWORD fdwProtect,DWORD dwMaximumsizeHigh,DWORD dwMaximumSizeLow,PCTSTR pszName);
第一個參數(shù)使用createfile 返回的handle。psa一般使用默認(rèn)null。當(dāng)創(chuàng)建一個file mapping object 時候,系統(tǒng)并不會 馬上保留一個地址空間,然后將file映射到這個區(qū)域。但很i,當(dāng)系統(tǒng)map時候,系統(tǒng)必須知道為physical storage分配什么樣的保護(hù)屬性,第三個參數(shù)指明了這些。
后面兩個參數(shù)指明file的大小,ensure enouth physical storage is available for the file-mapping object.
high指明高32位,low指明低32位。如果想創(chuàng)建一個反應(yīng)現(xiàn)在文件大小的map,均傳0.
pszName 用于與其它進(jìn)程共享內(nèi)存映射文件
--3.將文件數(shù)據(jù)map to process address space
使用這個函數(shù)
PVOID MapViewOfFile(HANDLE hfileMappingObject,dwDesireaccess,dwFileOffsetHigh,dwFileOffsetLow,dwNumberOfbytestomap)
文件沒必要一次全映射,一次映射一部分,這一部分成為一個view
首先通過high和low 指定開始映射的字節(jié)
其次是指定映射多大,0則映射到文件尾部。
--4.unmapping the file data from the process address space
UnmapviewOfFile(PVOID pvBaseAdddress);
參數(shù)使用mapview的返回值
如果強(qiáng)制write back to disk 則使用 FlushViewOfFile(PVOID pvAddress,SIZE_T dwNumberOfBytesToFlush)
第一個地址是想要開始flush的地址
--5.關(guān)閉filemapping object 以及file object
-----------------------------------------------------------------------------------
使用filemap 在進(jìn)程之間共享數(shù)據(jù)
例子:
app開始時候,系統(tǒng)調(diào)用createfile to open .exe file onthe disk。sys call creatFileMapping to create filemapping object.然后系統(tǒng)調(diào)用 mapviewofffileEX (with sec_image flag to point out it is a pe file),So that the file’s image is mapped to the address of the first byte of exectuable code of this mapped view. System creates the primary thread , puts the address of the first byte of exec code of this mapped view in the thread instruction pointer,and then lets the cpu start exec the code.
If user 再啟動同一個app,sys 看到file-mapping已經(jīng)存在了,系統(tǒng)maps a view of file a second time,this time in the context of the newly created process address space.
像所有內(nèi)核對象一樣,有三種方法共享他,繼承,命名對象以及賦值handle。
···頁文件支持的內(nèi)存映射文件
許多應(yīng)用程序運(yùn)行是產(chǎn)生數(shù)據(jù)需要和其他進(jìn)程共享如果必須在硬盤建立文件才可以共享,那么效率很低。因此微軟提供了由system paging file 支持的 file mapping。不需要createfile ,只需要在調(diào)用createFilemapping 的時候傳進(jìn)一個 INVALID_HANDLE_VALUE 作為hFile 參數(shù)即可。 映射文件大小同樣是由high 和low 參數(shù)決定的。
`````稀疏提交的內(nèi)存映射文件
--看來需要把虛擬內(nèi)存那章一起看看了~~~~