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 ...
随机推荐
- NodeJs笔记 : express框架创建工程 ----- 路由设计
一.搭建工程 1 .安装 express-generator $ npm install -g express-generator 2 .本地创建express项目 $ express -e blog ...
- JAVA8函数式接口
java8 中内置的四大核心函数接口** Consumer<T> :消费型接口* void accept(T t)* Supplier<T> :供给型接口* T get()** ...
- Mod制作第一个物品
在通过Mod制作第一个物品时需要 一下几个步骤: 创建一个物品 通常在创建这个物品类的时候会继承Item或者是Block父类(因为Mod中的物品都是这个两个类的子类),在给类中使用了this.setU ...
- tensorflow tensor 索引
问题: self.q_eval4next: (100,2) ix=[0,1,0,1---0,1](100,1) 我想取q_eval4next[:,idx] #use_doubleQ 切片用!!!! s ...
- php核心纪要 整理
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 10.for
要遍历一个范围(如数组或表),使用 for 循环.在数组上迭代时,当前数组元素存储在循环变量中.在遍历字典时, index 存储在循环变量中. (in 内容测试) for x in [5, 7, 11 ...
- 7.线程id,优先级讲解
1.线程id可以通过Thread对象的getId()方法得到,在线程出了问题,为什么CPU占用这么高的时候,查的时候我们可以在堆栈信息中找到对应线程,然后干掉该线程就好! 2.而线程对象的getNam ...
- keras如何求分类问题中的准确率和召回率
https://www.zhihu.com/question/53294625 由于要用keras做一个多分类的问题,评价标准采用precision,recall,和f1_score:但是keras中 ...
- sql server 中后缀为.mdf的文件是干什么用的??
在微软的SQL Server 2000 数据库有三种类型的文件: 1)主要数据文件(扩展名.mdf是 primary data file 的缩写) 主要数据文件包含数据库的启动信息,并指向数据库中的其 ...
- Linux基础命令---traceroute追踪路由
traceroute traceroute指令输出到目标主机的路由包.Traceroute跟踪从IP网络到给定主机的路由数据包.它利用IP协议的生存时间(TTL)字段,并试图在通往主机的路 ...