// 使用Memory Mapping file共享数据,使用一个Event作为并发控制。
// 仅为练习使用。
#define BUFFER_SIZE (100*1024)
#define BUFSIZE 1024
struct BufferStruct
{
BOOL hasRead;
TCHAR buf[512];
};
void CEDSInstallMain1Dlg::OnBnClickedButton1()
{
TCHAR szMapFileName[] = _T("Local\\TestHelloWorld");
// Create the file mapping object
HANDLE hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // Use paging file instead of existing file. // Pass file handle to share in a file.
NULL, // Default security
PAGE_READWRITE, // Read/write access
0, // Max. object size
BUFFER_SIZE, // Buffer size
szMapFileName // Name of mapping object
);
if (hMapFile == NULL)
return;
// Create file view from the file mapping object.
BufferStruct* pBuf = (BufferStruct*) MapViewOfFile(
hMapFile, // Handle of the map object
FILE_MAP_ALL_ACCESS, // Read/write permission
0, // A high-order DWORD of the file offset where the view begins.
0, // A low-order DWORD of the file offset where the view is to begin.
BUFFER_SIZE // The number of bytes of a file mapping to map to the view.
);
if (pBuf == NULL)
{
CloseHandle(hMapFile);
return;
}
pBuf->hasRead = TRUE;
PROCESS_INFORMATION pi[3];
for (int i = 0; i< 3; i++)
{
STARTUPINFO si;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
//si.dwFlags = STARTF_USESHOWWINDOW;
//si.wShowWindow = SW_HIDE;
ZeroMemory(&pi, sizeof(pi));
CString cmdstr;
cmdstr.Format("C:\\Work\\EDSInstall\\Release\\EDSInstall.exe");
// Start the child process.
if(!CreateProcess(NULL, // No module name (use command line)
cmdstr.GetBuffer(0), // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi[i] ) // Pointer to PROCESS_INFORMATION structure
)
{
AfxMessageBox("CreateProcess failed (%d).");
return;
}
}
HANDLE globalEvent = CreateEvent(0, TRUE, FALSE, TEXT("GlobalTestEvent"));
SetEvent(globalEvent);
Sleep(2000);
do
{
DWORD dwRet = WaitForSingleObject(globalEvent, 100);
if (dwRet == WAIT_OBJECT_0)
{
if (pBuf && !pBuf->hasRead)
{
CString msgFromGlobal = pBuf->buf;
// *********************william test*************12/23/2009 1:40:44 PM
TCHAR bufferWill[512];
_stprintf(bufferWill, TEXT(" [%s] BEGIN read: %s ##[%p]L%d\n"), __FUNCTION__,
msgFromGlobal, this, __LINE__);
::OutputDebugString(bufferWill);
// **********************************************/
memset(pBuf->buf, 0, sizeof(TCHAR) * 512);
pBuf->hasRead = TRUE;
SetEvent(globalEvent);
// it is equal to do something.
Sleep(500);
}
else
{
SetEvent(globalEvent);
// wait the data written.
Sleep(500);
}
}
else
{
Sleep(500);
}
} while (1);
AfxMessageBox("finished");
for (int i = 0; i< 3; i++)
{
// Close process and thread handles.
CloseHandle( pi[i].hProcess );
CloseHandle( pi[i].hThread );
}
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
CloseHandle(globalEvent);
}
In the client side:
// *********************william test*************12/23/2009 2:24:20 PM
TCHAR bufferWill[512];
_stprintf(bufferWill, TEXT(" [%s] BEGIN ##L%d\n"), __FUNCTION__,
__LINE__);
::OutputDebugString(bufferWill);
// **********************************************/
TCHAR szMapFileName[] = _T("Local\\TestHelloWorld");
// Create the file mapping object
HANDLE hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // Use paging file instead of existing file. // Pass file handle to share in a file.
NULL, // Default security
PAGE_READWRITE, // Read/write access
0, // Max. object size
BUFFER_SIZE, // Buffer size
szMapFileName // Name of mapping object
);
if (hMapFile == NULL)
return nRetCode;
// Create file view from the file mapping object.
BufferStruct* pBuf = (BufferStruct*) MapViewOfFile(
hMapFile, // Handle of the map object
FILE_MAP_ALL_ACCESS, // Read/write permission
0, // A high-order DWORD of the file offset where the view begins.
0, // A low-order DWORD of the file offset where the view is to begin.
BUFFER_SIZE // The number of bytes of a file mapping to map to the view.
);
if (pBuf == NULL)
{
CloseHandle(hMapFile);
return nRetCode;
}
// *********************william test*************12/23/2009 2:24:20 PM
_stprintf(bufferWill, TEXT(" [%s] step1 ##L%d\n"), __FUNCTION__,
__LINE__);
::OutputDebugString(bufferWill);
// **********************************************/
HANDLE globalEvent = CreateEvent(0, TRUE, FALSE, TEXT("GlobalTestEvent"));
// *********************william test*************12/23/2009 2:24:20 PM
_stprintf(bufferWill, TEXT(" [%s] step2 %X %X ##L%d\n"), __FUNCTION__,
GetCurrentProcessId(), globalEvent, __LINE__);
::OutputDebugString(bufferWill);
// **********************************************/
do
{
DWORD dwRet = WaitForSingleObject(globalEvent, 200);
if (dwRet == WAIT_OBJECT_0)
{
if (pBuf && pBuf->hasRead)
{
CString str;
str.Format("String from sub %X", GetCurrentProcessId());
// Write the message to the file view.
int cbMessageBytes = (str.GetLength() + 1 /* NULL */) * sizeof(TCHAR);
CopyMemory((PVOID)pBuf->buf, str.GetBuffer(0), cbMessageBytes);
// *********************william test*************12/23/2009 2:24:20 PM
_stprintf(bufferWill, TEXT(" [%s] step-loop2 Write: %X ##L%d\n"), __FUNCTION__,
GetCurrentProcessId(), __LINE__);
::OutputDebugString(bufferWill);
// **********************************************/
pBuf->hasRead = FALSE;
SetEvent(globalEvent);
// it is equal to do something.
Sleep(800);
}
else
{
SetEvent(globalEvent);
// wait the data written.
Sleep(500);
}
}
else
{
Sleep(500);
}
} while (1);
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
CloseHandle(globalEvent);
分享到:
相关推荐
本DEMO是以Delphi7为开发工具,展示了如何实现进程间的通信,包括数据的传递和同步。 在Delphi7中,进程间通信可以通过多种方式实现,如管道(Pipes)、消息队列(Message Queues)、共享内存(Shared Memory)、套...
本示例"android 进程间通信demo"专注于讲解如何利用Android Interface Definition Language (AIDL) 实现这一功能。 AIDL是Android提供的一个接口定义工具,它允许我们定义服务间的接口,这样不同的进程可以通过 ...
套接字包含进行网络通信必须的五种信息:连接使用的协议,本地主机的IP地址,本地进程的协议端口,远地主机的IP地址,远地进程的协议端口。为了满足不同程序对通信质量和性能的要求,一般的网络系统都提供了流式、...
在本"Android进程间通信Demo"中,我们将重点探讨两种主要的IPC方式:普通的进程间通信和通过AIDL(Android Interface Definition Language)实现的进程间对象传递。 首先,让我们来看看“16”部分,即Android进程间...
在这个"android_aidl_进程间通讯demo"中,我们将探讨如何通过AIDL来构建服务并进行跨进程通信。 首先,我们来看标题"android_aidl_进程间通讯demo",它明确指出这个示例项目是关于AIDL在处理进程间通信中的应用。...
在这个“进程间通信demo”中,我们重点关注的是通过AIDL(Android Interface Definition Language)实现的IPC方式。 AIDL是Android提供的一种接口定义语言,它的主要作用是定义进程间通信的接口协议。通过AIDL,...
在这个“CEF:进程间通信 Demo(VS2013)”中,我们将探讨如何在Visual Studio 2013环境下利用CEF实现跨进程通信。 首先,让我们了解什么是进程间通信(IPC,Inter-Process Communication)。在多线程或多进程的...
本示例将详细介绍如何使用文件映射进行进程间通信,并通过具体的“进程间通信demo”来展示其实现过程。 首先,我们要理解什么是文件映射。在操作系统中,文件映射是将一个文件的内容映射到进程的虚拟地址空间中,...
通过COM组件封存共享内存的方法,并添加到...任何一个进程只要使用这个服务,就可以访问到共享内存。方便不同进程间通信,增加了通信效率。但是慎用这种方法,该方法已经在WINDOWS7下通过测试,XP下有时候会弹错。
AIDL进程间通讯demo,A [android] I [Interface] D [Definition] L [Language],Android接口定义语言。 作用:方便系统为我们生成代码从而实现跨进程通讯,仅此而已。(玉刚老师如是说也),也就是说这个AIDL就只是一...
通过这个AILD进程间通信Demo,我们可以了解到Android中实现跨进程通信的基本步骤和注意事项,这对于开发大型、复杂的应用尤其重要。在实际项目中,理解并熟练掌握AIDL将有助于构建稳定、高效的应用架构。
本示例"AIDL进程间通信DEMO"将深入讲解如何使用AIDL来建立两个应用程序组件之间的桥梁。 首先,我们需要了解AIDL的基本概念。AIDL允许开发者定义一个接口,这个接口将在两个进程间被实现和使用。通过AIDL定义的接口...
android_IPC进程间通信Demo 移动开发 - Android包含两个eclipse工程,客户端和服务端,演示了android在使用Messenger对象和AIDL实现进程间通信.zip
本教程将通过“android_IPC进程间通信Demo”中的客户端和服务端示例,深入讲解如何利用Messenger对象和AIDL(Android Interface Definition Language)来实现这一目标。 首先,我们来看一下Messenger的使用。...
**进程间通信(IPC,Inter-Process Communication)是Android系统中一种重要的机制,它使得不同应用程序的进程可以相互通信,共享数据。AIDL(Android Interface Definition Language)是Android提供的一种用于实现...
android_IPC进程间通信Demo 移动开发 - Android包含两个eclipse工程,客户端和服务端,演示了android在使用Messenger对象和AIDL实现进程间通信.zip
Qt Remote Object简称QtRO,这是Qt5.9以后官方推出来的新模块,专门用于进程间通信(IPC)。在这之前,要实现进程间通信有多种方式,这里就不做介绍了,而Qt官方推出的这个新模块是基于Socket来封装的,使用起来非常...
在IT领域,进程间通信(IPC,Inter-Process Communication)是一种关键的技术,使得不同进程能够交换数据和协调工作。在Windows系统中,File Mapping(文件映射)是实现共享内存的一种方式,它允许多个进程访问同一...
在本文中,我们将深入探讨如何使用Qt Remote Objects(QtRO)框架实现动态Replica进行进程间通信(IPC)。...通过学习和实践这个Demo,你可以掌握如何在Qt应用程序中有效地使用QtRO进行进程间通信。