内存映射文件实现多进程通信
2009-02-20 09:36
519 查看
send.cpp
recv.cpp
]#include <stdio.h> #include <iostream> #include <windows.h> using namespace std; int main(int argc,char **argv){ HANDLE hMapFile = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4*1024, "ShareFile" ); if(hMapFile == NULL){ cout << "分配内存空间出错" << endl; return 0; } LPVOID lpMapAddress = MapViewOfFile( hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0 ); if(lpMapAddress == NULL){ cout << "申请内存失败" << endl; return 0; } char buf[4096]; cin >> buf; lstrcpy((char*)lpMapAddress,buf); int i = 0; here: cin >> i; if(i == 0){ goto here; } UnmapViewOfFile(lpMapAddress); return 0; }
recv.cpp
]#include <stdio.h> #include <iostream> #include <windows.h> using namespace std; int main(int argc,char **argv){ HANDLE hMapFile = OpenFileMapping( FILE_MAP_ALL_ACCESS, false, "ShareFile" ); if(hMapFile == NULL){ cout << "获取内存映射文件失败" << endl; return 0; } LPVOID lpMapAddress = MapViewOfFile( hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0 ); if(lpMapAddress == NULL){ cout << "内存映射文件申请失败" << endl; return 0; } cout << (char *)lpMapAddress << endl; UnmapViewOfFile(lpMapAddress); return 0; }
相关文章推荐
- UNIX环境高级编程学习之第十五章进程间通信 - 两个进程通过映射普通文件实现共享内存通信
- Windows进程通信--使用内存映射文件
- C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped
- 通过内存映射文件实现进程间数据共享
- 用文件映射(File Mapping)实现进程间内存共享
- C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped 转
- C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped
- 进程通信之内存地址映射与共享,同时如何在Linux0.11下实现共享内存
- 使用文件映射实现进程通信
- 用文件映射(File Mapping)实现进程间内存共享
- C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped 转
- C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped
- C# .Net 多进程同步 通信 共享内存 内存映射文件
- C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped
- Windows进程通信之内存映射文件
- 用文件映射(File Mapping)实现进程间内存共享
- C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped
- 用文件映射(File Mapping)实现进程间内存共享
- 用文件映射来实现进程通信
- 用文件映射(File Mapping)实现进程间内存共享