博客
关于我
进程间通信(2) 内存映射FileMap
阅读量:81 次
发布时间:2019-02-25

本文共 3220 字,大约阅读时间需要 10 分钟。

1.文件映射对象。

  • 文件映射对象是内核对象,它从系统的分页文件中获得一段内存。
  • 所有内核对象都共享同一个名字空间,所以名称不能重复。
  • 其他内核对象还有:事件,信号,互斥对象

2. 创建一个文件映射对象。

  • 为文件映射指明一个大小和名称。
  • windows api:CreateFileMapping

3. 对内核对象进行读写操作

windows api:MapViewOfFile

4. 示例

FULL_MAP_NAME 用来标记文件映射对象

server

// Prepare a message to be written to the view.      PWSTR pszMessage = (PWSTR)MESSAGE;    DWORD cbMessage = (wcslen(pszMessage) + 1) * sizeof(*pszMessage);    HANDLE hMapFile = NULL;    PVOID pView = NULL;    // Create the file mapping object.      hMapFile = CreateFileMapping(        INVALID_HANDLE_VALUE,   // Use paging file - shared memory          NULL,                   // Default security attributes          PAGE_READWRITE,         // Allow read and write access          0,                      // High-order DWORD of file mapping max size          MAP_SIZE,               // Low-order DWORD of file mapping max size          FULL_MAP_NAME           // Name of the file mapping object      );    if (hMapFile == NULL)    {           wprintf(L"CreateFileMapping failed w/err 0x%08lx\n", GetLastError());        goto Cleanup;    }    wprintf(L"The file mapping (%s) is created\n", FULL_MAP_NAME);    // Map a view of the file mapping into the address space of the current       // process.      pView = MapViewOfFile(        hMapFile,               // Handle of the map object          FILE_MAP_ALL_ACCESS,    // Read and write access          0,                      // High-order DWORD of the file offset           VIEW_OFFSET,            // Low-order DWORD of the file offset           VIEW_SIZE               // The number of bytes to map to view      );    if (pView == NULL)    {           wprintf(L"MapViewOfFile failed w/err 0x%08lx\n", GetLastError());        goto Cleanup;    }    wprintf(L"The file view is mapped\n");    // Write the message to the view.      memcpy_s(pView, VIEW_SIZE, pszMessage, cbMessage);    wprintf(L"This message is written to the view:\n\"%s\"\n",        pszMessage);

client

HANDLE hMapFile = NULL;    PVOID pView = NULL;    // Try to open the named file mapping identified by the map name.      hMapFile = OpenFileMapping(        FILE_MAP_READ,          // Read access          FALSE,                  // Do not inherit the name          FULL_MAP_NAME           // File mapping name       );    if (hMapFile == NULL)    {           wprintf(L"OpenFileMapping failed w/err 0x%08lx\n", GetLastError());        goto Cleanup;    }    wprintf(L"The file mapping (%s) is opened\n", FULL_MAP_NAME);    // Map a view of the file mapping into the address space of the current       // process.      pView = MapViewOfFile(        hMapFile,               // Handle of the map object          FILE_MAP_READ,          // Read access          0,                      // High-order DWORD of the file offset           VIEW_OFFSET,            // Low-order DWORD of the file offset          VIEW_SIZE               // The number of bytes to map to view      );    if (pView == NULL)    {           wprintf(L"MapViewOfFile failed w/err 0x%08lx\n", GetLastError());        goto Cleanup;    }    wprintf(L"The file view is mapped\n");    // Read and display the content in view.      wprintf(L"Read from the file mapping:\n\"%s\"\n", (PWSTR)pView);

结果:

在这里插入图片描述
在这里插入图片描述

【引用】

[1]: 代码地址

转载地址:http://dlm.baihongyu.com/

你可能感兴趣的文章
MySQL 中的外键检查设置:SET FOREIGN_KEY_CHECKS = 1
查看>>
Mysql 中的日期时间字符串查询
查看>>
mysql 中索引的问题
查看>>
MySQL 中锁的面试题总结
查看>>
MySQL 中随机抽样:order by rand limit 的替代方案
查看>>
MySQL 为什么需要两阶段提交?
查看>>
mysql 为某个字段的值加前缀、去掉前缀
查看>>
mysql 主从
查看>>
mysql 主从 lock_mysql 主从同步权限mysql 行锁的实现
查看>>
mysql 主从互备份_mysql互为主从实战设置详解及自动化备份(Centos7.2)
查看>>
mysql 主从关系切换
查看>>
MYSQL 主从同步文档的大坑
查看>>
mysql 主键重复则覆盖_数据库主键不能重复
查看>>
Mysql 事务知识点与优化建议
查看>>
Mysql 优化 or
查看>>
mysql 优化器 key_mysql – 选择*和查询优化器
查看>>
MySQL 优化:Explain 执行计划详解
查看>>
Mysql 会导致锁表的语法
查看>>
mysql 使用sql文件恢复数据库
查看>>
mysql 修改默认字符集为utf8
查看>>