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 ...
随机推荐
- 排名前10的vue前端UI框架框架值得你掌握
参考:https://juejin.im/post/5b34faeef265da59645b188e muse-ui 框架: https://juejin.im/entry/582974eb8ac24 ...
- 移动端的rem适配
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- VS2010 运行时 出现cmd窗口的设置方法
项目 - 属性 -生成事件 --后期生成事件 ->命令行 (editbin /SUBSYSTEM:CONSOLE $(OUTDIR)\$(ProjectName).exe) 注: ...
- jar包的读取1
---恢复内容开始--- 昨天在做项目插件的时候,因为会用到jar包中的一个文件来初始化程序.并且以后还是会访问这个文件,所以就想到干脆吧文件拷贝到指定目录.在拷贝的时候也费了好一会时间,这里涉及到了 ...
- struts2启动时,出现的com.opensymphony.xwork2.util.finder.ClassFinder - Unable to read class 错误解决办法
在项目的struts.xml文件中第一行加入<constant name="struts.convention.package.locators" value="c ...
- 使用spring的特殊bean完成分散配置
1.在使用分散配置时,spring的配置文件applicationContext.xml中写法如下: <!-- 引入db.properties文件, --> <context:pro ...
- mysql 5.6 每天凌晨12:00 重置sequence表中的某个值
#.创建evevt要调用的存储过程update_current_value_procedure delimiter // drop procedure if exists update_current ...
- django admin 处理文本换行空格
使用 linebreaks filter <p>{{ blogpassage.content|linebreaksbr }}<p>
- 标准I/O流
一.标准输入流 标准输入流对象cin,重点掌握的函数 cin.get() //一次只能读取一个字符 cin.get(一个参数) //读一个字符 cin.get(三个参数) //可以读字符串 cin.g ...
- linux ~/ 和 /
/是目录层的分隔.表示符.只有一个/表明是root,/etc/表明是根目录下面的etc目录(当然目录最后不需要/,但有/直接表明他是目录,没有末尾的/,那么/etc需要检测一下确定是目录还是文件,虽然 ...