惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 热门话题
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Project Zero
Project Zero
V
Vulnerabilities – Threatpost
Cisco Talos Blog
Cisco Talos Blog
P
Palo Alto Networks Blog
C
Cisco Blogs
A
Arctic Wolf
月光博客
月光博客
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
量子位
小众软件
小众软件
Latest news
Latest news
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
N
Netflix TechBlog - Medium
K
Kaspersky official blog
人人都是产品经理
人人都是产品经理
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
Y
Y Combinator Blog
P
Proofpoint News Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
S
Schneier on Security
D
Docker
Scott Helme
Scott Helme
MyScale Blog
MyScale Blog
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
GbyAI
GbyAI
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
H
Help Net Security
Simon Willison's Weblog
Simon Willison's Weblog
J
Java Code Geeks
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tenable Blog
B
Blog
Know Your Adversary
Know Your Adversary
IT之家
IT之家

博客园 - SPARON

[转] 在 Windows Server 2008 R2 下用 Visual Studio 2010 编译 Chrome 与 WebKit Chromium Port [转]JVM调优总结 6 个基于 HTML5 实现的多媒体播放器 35个五彩缤纷的网页设计作品欣赏 关于setInterval调用对象方法的问题 【收藏】27个jQuery表单插件分享 【收藏】开发人员看过来:11 个免费的开源 IDE Web开发人员应当知道的15个开源项目 终于还是走到了这步! 【转】27 款经典的CSS 框架 如果收购SUN的是Microsoft... XP安装 安全补丁KB905414 后本地连接丢失的解决方法 C、CPP const 详解 C语言运算符优先级 详细列表 typedef struct和struct定义结构体的区别 C语言学习之#define用法 [转]JDK里的设计模式 记CPU的一次复频 C语言字符串转数值
CreateFileMapping和MapViewOfFile函数
SPARON · 2010-12-08 · via 博客园 - SPARON

大家都是到PG是分布式网络事务处理数据库,与其他数据库优点之一就在于服务器与客户的交流是一对一的,所谓一对一是指,针对客户的每一连接服务器都会产生一个进程为其服务,那么问题就来了,这些进程间是如何交互、如何实现并发数据同步、保证数据正确性的问题呢?在PG中采用的就是共享内存+信号灯实现的,关于共享内存首先想到的就是CreateFileMapping和MapViewOfFile函数,下面是晚上搜索的一篇关于这两个函数的使用方法,原文如下:

在开发软件过程里,也经常碰到进程间共享数据的需求。比如A进程创建计算数据,B进程进行显示数据的图形。这样的开发方式可以把一个大程序分开成独立的小程序,提高软件的成功率,也可以更加适合团队一起开发,加快软件的开发速度。下面就来使用文件映射的方式进行共享数据。先要使用函数CreateFileMapping来创建一个想共享的文件数据句柄,然后使用MapViewOfFile来获取共享的内存地址,然后使用OpenFileMapping函数在另一个进程里打开共享文件的名称,这样就可以实现不同的进程共享数据。

函数CreateFileMapping、MapViewOfFile声明如下:

WINBASEAPI
__out
HANDLE
WINAPI
CreateFileMappingA(
    
__in     HANDLE hFile,
    
__in_opt LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
    
__in     DWORD flProtect,
    
__in     DWORD dwMaximumSizeHigh,
    
__in     DWORD dwMaximumSizeLow,
    
__in_opt LPCSTR lpName
    );
WINBASEAPI
__out
HANDLE
WINAPI
CreateFileMappingW(
    
__in     HANDLE hFile,
    
__in_opt LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
    
__in     DWORD flProtect,
    
__in     DWORD dwMaximumSizeHigh,
    
__in     DWORD dwMaximumSizeLow,
    
__in_opt LPCWSTR lpName
    );
#ifdef UNICODE
#define CreateFileMapping CreateFileMappingW
#else
#define CreateFileMapping CreateFileMappingA
#endif // !UNICODE
 
WINBASEAPI
__out
LPVOID
WINAPI
MapViewOfFile(
    
__in HANDLE hFileMappingObject,
    
__in DWORD dwDesiredAccess,
    
__in DWORD dwFileOffsetHigh,
    
__in DWORD dwFileOffsetLow,
    
__in SIZE_T dwNumberOfBytesToMap
    );

hFile是创建共享文件的句柄。

lpFileMappingAttributes是文件共享的属性。

flProtect是当文件映射时读写文件的属性。

dwMaximumSizeHigh是文件共享的大小高位字节。

dwMaximumSizeLow是文件共享的大小低位字节。

lpName是共享文件对象名称。

hFileMappingObject是共享文件对象。

dwDesiredAccess是文件共享属性。

dwFileOffsetHigh是文件共享区的偏移地址。

dwFileOffsetLow是文件共享区的偏移地址。

dwNumberOfBytesToMap是共享数据长度。

调用函数的例子如下:

   //文件共享。
   void FileMapping(void)
   {
          
//打开共享的文件对象。
          m_hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE,_T("TestFileMap"));
          
if (m_hMapFile)
          {
                
//显示共享的文件数据。
               LPTSTR lpMapAddr = (LPTSTR)MapViewOfFile(m_hMapFile,FILE_MAP_ALL_ACCESS,
                    
0,0,0
);
                OutputDebugString(lpMapAddr);
          }
          
else
          {
                
//创建共享文件。
                m_hMapFile = CreateFileMapping( (HANDLE)0xFFFFFFFF,NULL,
                    PAGE_READWRITE,
0,1024,_T("TestFileMap"
));
 
                
//拷贝数据到共享文件里。
                LPTSTR lpMapAddr = (LPTSTR)MapViewOfFile(m_hMapFile,FILE_MAP_ALL_ACCESS,
                    
0,0,0
);
                std::wstring strTest(_T(
"TestFileMap"));
                wcscpy(lpMapAddr,strTest.c_str());                
  
                FlushViewOfFile(lpMapAddr,strTest.length()
+1);
          }
   }