記憶體檔案對映 程序間通訊

2021-05-26 19:18:18 字數 1183 閱讀 4061

記憶體對映檔案可以分為兩種:

一種是普通的檔案,它需要乙個普通的檔案控制代碼,用於快速的讀寫檔案,這種檔案的資料在程序退出後會儲存在硬碟上,所以程序在下次執行時可以得到之前的資料;

另一種是頁檔案,當建立記憶體對映檔案的時候傳入無效的控制代碼,這時會把頁檔案當作臨時的共享儲存空間,當程序退出後這些資料是不會儲存下來的。因為共享記憶體通常只關注執行期間的資料共享,所以一般是使用這種記憶體對映檔案。

先開啟程序1,然後在程序2中就可以獲取程序1中的資料了, msdn中的例子如下:

// process1.cpp

#include #include #include #include #define buf_size 256

tchar szmsg = text("message from first process.");

int _tmain()

pbuf = (lptstr)mapviewoffile(hmapfile,

file_map_all_access,

0,0,

buf_size);

if (pbuf == null)

copymemory((void*)pbuf, szmsg, (_tcslen(szmsg) * sizeof(tchar)));

_getch();

unmapviewoffile(pbuf);

closehandle(hmapfile);

return 0;

}

#include #include #include #include #pragma comment(lib, "user32.lib")

#define buf_size 256

int _tmain()

pbuf = (lptstr)mapviewoffile(hmapfile,

file_map_all_access,

0,0,

buf_size);

if (pbuf == null)

messagebox(null, pbuf, text("process2"), mb_ok);

unmapviewoffile(pbuf);

closehandle(hmapfile);

return 0;

}

C 通過記憶體對映 程序間通訊

1.為了提速,沒用序列化,改自己寫對映。2.為了方便改動變數型別,都是分配8位元組。3.我這裡的body結構體一半是a端寫b端都,另一半是b端寫a端讀,看起來比較亂。charts using system using system.io using system.collections.generi...

程序間通訊 記憶體對映

程序1的資料有時需要傳送給程序b,這可以通過記憶體對映實現。主要用到的函式 寫方式開啟對映,page writecopy 為寫操作留下備份 dword dwmaximumsizehigh,檔案對映的最大長度的高32位,一般設為0,用低32位一般就夠用了 dword dwmaximumsizelow,...

程序間通訊之記憶體對映 mmap記憶體對映

讀資料端程序 mmanp r.c include include include include include include define len 0x1000 int main close fd while 1 close fd munmap addr,len return 0 寫資料端程序 ...