C++进程间通信之共享内存
转载:http://blog.csdn.net/taily_duan/article/details/51692999
转载:http://blog.csdn.net/fengrx/article/details/4069088
转载:http://www.cnblogs.com/xuandi/p/5673917.html
// ServerCom.cpp : Defines the entry point for the console application.
// #include "stdafx.h" #include <stdio.h>
#include <windows.h>
#pragma endregion
#define MAP_PREFIX L"Local\\"
#define MAP_NAME L"SampleMap"
#define FULL_MAP_NAME MAP_PREFIX MAP_NAME // Max size of the file mapping object.
#define MAP_SIZE 65536 // File offset where the view is to begin.
#define VIEW_OFFSET 0 // The number of bytes of a file mapping to map to the view. All bytes of the
// view must be within the maximum size of the file mapping object (MAP_SIZE).
// If VIEW_SIZE is 0, the mapping extends from the offset (VIEW_OFFSET) to
// the end of the file mapping.
#define VIEW_SIZE 1024 // Unicode string message to be written to the mapped view. Its size in byte
// must be less than the view size (VIEW_SIZE).
#define MESSAGE L"Message from the first process." int _tmain(int argc, _TCHAR* argv[])
{
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
, // 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
, // 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"); // Prepare a message to be written to the view.
PWSTR pszMessage = MESSAGE;
DWORD cbMessage = (wcslen(pszMessage) + ) * sizeof(*pszMessage); // 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); // Wait to clean up resources and stop the process.
wprintf(L"Press ENTER to clean up resources and quit");
getchar(); Cleanup: if (hMapFile)
{
if (pView)
{
// Unmap the file view.
UnmapViewOfFile(pView);
pView = NULL;
}
// Close the file mapping object.
CloseHandle(hMapFile);
hMapFile = NULL;
} return ;
}
// ClientCom.cpp : Defines the entry point for the console application.
// #include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#pragma endregion
#define MAP_PREFIX L"Local\\"
#define MAP_NAME L"SampleMap"
#define FULL_MAP_NAME MAP_PREFIX MAP_NAME // File offset where the view is to begin.
#define VIEW_OFFSET 0 // The number of bytes of a file mapping to map to the view. All bytes of the
// view must be within the maximum size of the file mapping object. If
// VIEW_SIZE is 0, the mapping extends from the offset (VIEW_OFFSET) to the
// end of the file mapping.
#define VIEW_SIZE 1024 int _tmain(int argc, _TCHAR* argv[])
{
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
, // 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); // Wait to clean up resources and stop the process.
wprintf(L"Press ENTER to clean up resources and quit");
getchar(); Cleanup: if (hMapFile)
{
if (pView)
{
// Unmap the file view.
UnmapViewOfFile(pView);
pView = NULL;
}
// Close the file mapping object.
CloseHandle(hMapFile);
hMapFile = NULL;
} return ;
}
注:运行的时候先运行写入的进程,再运行读出的进程

C++进程间通信之共享内存的更多相关文章
- 浅析Linux下进程间通信:共享内存
浅析Linux下进程间通信:共享内存 共享内存允许两个或多个进程共享一给定的存储区.因为数据不需要在客户进程和服务器进程之间复制,所以它是最快的一种IPC.使用共享内存要注意的是,多个进程之间对一给定 ...
- Linux环境进程间通信(五): 共享内存(下)
linux下进程间通信的几种主要手段: 管道(Pipe)及有名管道(named pipe):管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允 ...
- Linux环境进程间通信(五): 共享内存(上)
linux下进程间通信的几种主要手段: 管道(Pipe)及有名管道(named pipe):管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允 ...
- Linux进程IPC浅析[进程间通信SystemV共享内存]
Linux进程IPC浅析[进程间通信SystemV共享内存] 共享内存概念,概述 共享内存的相关函数 共享内存概念,概述: 共享内存区域是被多个进程共享的一部分物理内存 多个进程都可把该共享内存映射到 ...
- Linux进程间通信—使用共享内存
Linux进程间通信-使用共享内存 转自: https://blog.csdn.net/ljianhui/article/details/10253345 下面将讲解进程间通信的另一种方式,使用共享内 ...
- Linux进程间通信——使用共享内存
一.什么是共享内存 顾名思义,共享内存就是允许两个不相关的进程访问同一个逻辑内存.共享内存是在两个正在运行的进程之间共享和传递数据的一种非常有效的方式.不同进程之间共享的内存通常安排为同一段物理内存. ...
- linux进程间通信之共享内存篇
本文是对http://www.cnblogs.com/andtt/articles/2136279.html中共享内存(上)的进一步阐释说说明 1 共享内存的实现原理 共享内存是linux进程间通讯的 ...
- linux内核剖析(十一)进程间通信之-共享内存Shared Memory
共享内存 共享内存是进程间通信中最简单的方式之一. 共享内存是系统出于多个进程之间通讯的考虑,而预留的的一块内存区. 共享内存允许两个或更多进程访问同一块内存,就如同 malloc() 函数向不同进程 ...
- Linux进程间通信——使用共享内存(转)
一.什么是共享内存 顾名思义,共享内存就是允许两个不相关的进程访问同一个逻辑内存.共享内存是在两个正在运行的进程之间共享和传递数据的一种非常有效的方式.不同进程之间共享的内存通常安排为同一段物理内存. ...
- Python进程间通信之共享内存
前一篇博客说了怎样通过命名管道实现进程间通信,但是要在windows是使用命名管道,需要使用python调研windows api,太麻烦,于是想到是不是可以通过共享内存的方式来实现.查了一下,Pyt ...
随机推荐
- Abp项目构建、swagger及代码生成器
前段时间在学习abp,在配置swagger时踩了不少坑,特此整理一下,方便同行参考.幸运的是又发现了神奇的代码生成器,分享下亲身经验. 觉得此博客非常有用的朋友可以在右侧赞助打赏下,非常感谢大家支持. ...
- Web界面进行用户管理
Web界面进行用户管理 添加用户 Tags:表示账号的角色 Admin:超级管理员 No access :表示没有可以访问的virtual host虚拟机(相当于数据库) ...
- 水题 K
输入4行全部由大写字母组成的文本,输出一个垂直直方图,给出每个字符出现的次数.注意:只用输出字符的出现次数,不用输出空白字符,数字或者标点符号的输出次数. Input输入包括4行由大写字母组成的文本, ...
- arc 092D Two Sequences
题意: 给出两个长度N相同的整数序列A和B,有N^2种方式从A中选择一个数Ai,从B中选择一个数Bj,让两个数相加,求这N^2个数的XOR,即异或. 思路: 暴力的求显然是会超时的,因为是异或,就考虑 ...
- python遍历目录os.walk(''d:\\test2",topdown=False)
os.walk(top, topdown=True, onerror=None, followlinks=False)遍历目录,topdown=false表示先返回目录,后返回文件 参数说明: top ...
- Class_third_实验报告
设计思路:声明一个父类Shape并声明一个getArea()计算面积的空方法让子类Circle,Echelon,Triangle,Rectangle 继承父类的方法并根据类的不同重写getArea() ...
- js多个异步请求
一,两个(或多个)js异步并发执行,怎么在两个AJax异步操作之后执行一个新的操作 原题来自 ES6 方法 1.Promise 包装异步ajax操作,2.定义async 函数,3.用await等待pr ...
- calendar 模块
calendar模块,即日历模块,提供了对日期的一些操作方法,和生成日历的方法 注:星期一是默认的每周第一天,星期天是默认的最后一天.更改设置需调用calendar.setfirstweekday() ...
- Prometheus监控学习笔记之Prometheus的架构及持久化
0x00 Prometheus是什么 Prometheus是一个开源的系统监控和报警工具,特点是 多维数据模型(时序列数据由metric名和一组key/value组成) 在多维度上灵活的查询语言(Pr ...
- 9i时候的块
Header (Common and Variable) The header contains general block information, such as the block addres ...