什么時(shí)候用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時(shí)候,系統(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ū)域的物理存儲(chǔ)區(qū)域是在磁盤(pán)上的.exe 文件而不是在系統(tǒng)頁(yè)面中)
當(dāng)。exe 被mapped into the process address space,系統(tǒng)訪問(wèn).exe的一個(gè)區(qū)域,那里列出了.exe含有的dll,然后系統(tǒng)利用 LoadLibrary for each dlls。系統(tǒng)map dll時(shí)候,如果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 失敗,彈出對(duì)話框并且釋放申請(qǐng)的地址空間。
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. 例如,如果一段代碼訪問(wèn)了一個(gè)并不在主存的地址,那么一個(gè)錯(cuò)誤產(chǎn)生,系統(tǒng)察覺(jué)到錯(cuò)誤并且自動(dò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)為一個(gè)app創(chuàng)建第二個(gè)實(shí)例時(shí),系統(tǒng)只是簡(jiǎn)單的打開(kāi)另外一個(gè)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)存映射文件,多個(gè)實(shí)例可以共享代碼和數(shù)據(jù)。實(shí)際上,file 是分為多個(gè)section ,多個(gè)節(jié)均對(duì)齊于頁(yè)邊界。如果一個(gè)instance of the app 修改了全局變量,系統(tǒng)應(yīng)用了copy-on-write 機(jī)制,復(fù)制修改的頁(yè)面,并更新實(shí)例的file-mapping view。當(dāng)我們調(diào)試程序時(shí)同樣的事情會(huì)發(fā)生,debuger modify code,sys use cow again。
當(dāng)一個(gè)進(jìn)程被加載,系統(tǒng)檢查其所有映射文件頁(yè),系統(tǒng)將所有通常用cow保護(hù)的頁(yè)面提交給存儲(chǔ)系統(tǒng),這些頁(yè)面僅僅是被提交,當(dāng)文件被訪問(wèn)的時(shí)候,系統(tǒng)讀入相應(yīng)的頁(yè)面,如果頁(yè)面沒(méi)有被修改,那么他們可以被換出,如果已經(jīng)修改,系統(tǒng)將修改過(guò)的頁(yè)面轉(zhuǎn)入已經(jīng)提交的頁(yè)面之一(這點(diǎn)很晦澀啊 system swaps the modified page to one of the perviously committed pages in the paging file ,怎么翻譯呢~~~~ :( )
------------------------------------------------------------------------------------------------------
在可執(zhí)行文件或者dll中共享靜態(tài)變量
------------------------------------------------------------------------------------------------------
內(nèi)存映射數(shù)據(jù)文件
例子:要把一個(gè)文件所有字節(jié)倒置
如果利用file mapping 我們告訴系統(tǒng)使用一個(gè)虛擬空間的區(qū)域來(lái)倒置文件,然后告訴把文件的第一個(gè)字節(jié)映射到保留空間的第一個(gè)字節(jié),然后就像處理內(nèi)存中的字符串一樣處理文件即可,引文系統(tǒng)會(huì)幫助你完成文件緩存以及調(diào)頁(yè)等工作。
使用流程:
1.創(chuàng)建一個(gè)file kernel object that identifies the file on disk that you want to use as a memory –mapped file
2.創(chuàng)建一個(gè)file mapping kernel object 告訴系統(tǒng)文件的大小,以及你準(zhǔn)備如何訪問(wè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)核對(duì)象
CreateFile
失敗返回 INVALID_HANDLE_VALUE = –1 一般來(lái)說(shuō)windows func 失敗return null這個(gè)比較特殊
createfile dwdesiredAccess 需要設(shè)置為 GENERIC_READ 或者 GENERIC_WRITE
--2. 創(chuàng)建file-mapping 內(nèi)核對(duì)象
CreatefileMapping(HANDLE,PSECURITY_ATTRIBUTES,DWORD fdwProtect,DWORD dwMaximumsizeHigh,DWORD dwMaximumSizeLow,PCTSTR pszName);
第一個(gè)參數(shù)使用createfile 返回的handle。psa一般使用默認(rèn)null。當(dāng)創(chuàng)建一個(gè)file mapping object 時(shí)候,系統(tǒng)并不會(huì) 馬上保留一個(gè)地址空間,然后將file映射到這個(gè)區(qū)域。但很i,當(dāng)系統(tǒng)map時(shí)候,系統(tǒng)必須知道為physical storage分配什么樣的保護(hù)屬性,第三個(gè)參數(shù)指明了這些。
后面兩個(gè)參數(shù)指明file的大小,ensure enouth physical storage is available for the file-mapping object.
high指明高32位,low指明低32位。如果想創(chuàng)建一個(gè)反應(yīng)現(xiàn)在文件大小的map,均傳0.
pszName 用于與其它進(jìn)程共享內(nèi)存映射文件
--3.將文件數(shù)據(jù)map to process address space
使用這個(gè)函數(shù)
PVOID MapViewOfFile(HANDLE hfileMappingObject,dwDesireaccess,dwFileOffsetHigh,dwFileOffsetLow,dwNumberOfbytestomap)
文件沒(méi)必要一次全映射,一次映射一部分,這一部分成為一個(gè)view
首先通過(guò)high和low 指定開(kāi)始映射的字節(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)
第一個(gè)地址是想要開(kāi)始flush的地址
--5.關(guān)閉filemapping object 以及file object
-----------------------------------------------------------------------------------
使用filemap 在進(jìn)程之間共享數(shù)據(jù)
例子:
app開(kāi)始時(shí)候,系統(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 再啟動(dòng)同一個(gè)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)核對(duì)象一樣,有三種方法共享他,繼承,命名對(duì)象以及賦值handle。
···頁(yè)文件支持的內(nèi)存映射文件
許多應(yīng)用程序運(yùn)行是產(chǎn)生數(shù)據(jù)需要和其他進(jìn)程共享如果必須在硬盤(pán)建立文件才可以共享,那么效率很低。因此微軟提供了由system paging file 支持的 file mapping。不需要createfile ,只需要在調(diào)用createFilemapping 的時(shí)候傳進(jìn)一個(gè) INVALID_HANDLE_VALUE 作為hFile 參數(shù)即可。 映射文件大小同樣是由high 和low 參數(shù)決定的。
`````稀疏提交的內(nèi)存映射文件
--看來(lái)需要把虛擬內(nèi)存那章一起看看了~~~~