#include
#include <windows.h>
#include<tchar.h>
#define MAX_NUMBER_PROCESS 10
#define MAX_NUMBER_SOCKET 10
struct BoardSetting
{
int pad = 0;
int sad = 0;
int autopoll = 0;
uint8_t TermCharEnable = 0;
uint8_t EOSChar = 0;
int ist = 0;
char ipAddress[50] = { 0 };
bool bSIC = false;
bool bInit = false;
};
struct SharedData
{
HANDLE hMutex;
SOCKET clientSocket[MAX_NUMBER_PROCESS];
const char name[20];// = "NoName";
DWORD ProcessID[MAX_NUMBER_PROCESS] = { 0 };
int BoardID;
int ud;
BoardSetting bs_Board;
//int nCount;
};
struct SharedSockets
{
SharedData ss_sockets[MAX_NUMBER_SOCKET];
int nCount;
};
SharedSockets* pShd_Sockets;
//SharedData* shd_Sockets;
HANDLE hMutexFirstProcess;
bool isFirstProcess = false;
void Cleanup()
{
// 清理资源
printf("process count: %d\n", pShd_Sockets->nCount);
//UnmapViewOfFile(shd_Sockets);
if (pShd_Sockets->nCount == 1)
{
UnmapViewOfFile(pShd_Sockets);
CloseHandle(hMutexFirstProcess);
}
else
pShd_Sockets->nCount--;
}
//bool isLastProcess()
//{
// for (int i = 0; i < MAX_NUMBER_SOCKET; i++)
// {
//
// }
// return false;
//}
int main()
{
// 创建互斥体,用于判断是否为第一个进程
hMutexFirstProcess = CreateMutex(nullptr, TRUE, _T("FirstProcessMutex"));
if (hMutexFirstProcess == nullptr)
{
std::cout << "创建互斥体失败" << std::endl;
return 1;
}
code
// 检查是否为第一个进程
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
printf("not first process\n");
isFirstProcess = false;
}
else
{
printf("first process\n");
isFirstProcess = true;
}
/* --- */
//const int sharedMemorySize = 1024 * 1024 * 100; // 分配100MB的共享内存
//HANDLE hFileMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sharedMemorySize, L"MySharedMemory");
//if (hFileMapping == NULL)
//{
// // 创建文件映射对象失败
// return -1;
//}
///*SIZE_T dwSharedMemorySize = 0;
//GetFileSizeEx(hFileMapping, (PLARGE_INTEGER)&dwSharedMemorySize);
//printf("共享内存大小:%u MB\n", (unsigned int)(dwSharedMemorySize / (1024 * 1024)));*/
//LPVOID lpSharedMemory = MapViewOfFile(hFileMapping, FILE_MAP_ALL_ACCESS, 0, 0, sharedMemorySize);
//if (lpSharedMemory == NULL)
//{
// // 映射共享内存失败
// CloseHandle(hFileMapping);
// return -1;
//}
//MEMORY_BASIC_INFORMATION tmbi;
//VirtualQuery(lpSharedMemory, &tmbi, sizeof(tmbi));
//SIZE_T dwSharedMemorySize = tmbi.RegionSize;
//printf("共享内存大小:%u MB\n", (unsigned int)(dwSharedMemorySize / (1024 * 1024)));
//UnmapViewOfFile(lpSharedMemory);
//CloseHandle(hFileMapping);
//system("pause");
//return 0;
/* --- */
printf("type int size is: %d, type uint8_t size is: %d, type char[50] size is: %d, type bool size is: %d\n",sizeof(int),sizeof(uint8_t),sizeof(char[50]), sizeof(bool));
printf("struct BoardSetting size is: %d\n", sizeof(BoardSetting));
printf("struct sharedData size is: %d\n", sizeof(SharedData));
printf("struct SharedSockets size is: %d\n", sizeof(SharedSockets));
// 创建共享内存
HANDLE hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, sizeof(SharedSockets), _T("SharedMemory"));
if (hMapping == nullptr)
{
std::cout << "创建共享内存失败" << std::endl;
Cleanup();
return 1;
}
//HANDLE hMapFile = OpenFileMapping(FILE_MAP_READ, FALSE, _T("SharedMemory"));
//LARGE_INTEGER fileSize;
/*if (GetFileSizeEx(hMapping, &fileSize))
{
printf("共享内存的大小: %d\n", fileSize.QuadPart);
}
else
printf("得到共享内存大小失败.\n");*/
// 映射到进程地址空间
pShd_Sockets = (SharedSockets*)MapViewOfFile(hMapping, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(SharedSockets));
if (pShd_Sockets == nullptr)
{
std::cout << "映射共享内存失败" << std::endl;
Cleanup();
return 1;
}
MEMORY_BASIC_INFORMATION mbi;
if (VirtualQuery(pShd_Sockets, &mbi, sizeof(mbi)))
{
// The size is stored in the mbi.RegionSize variable.
printf("共享内存的大小: %d\n", mbi.RegionSize);
}
// 初始化共享内存数据(只在第一个进程执行)
if (isFirstProcess)
{
for (int i = 0; i < MAX_NUMBER_SOCKET; i++)
{
//shd_Sockets[i].hMutex = CreateMutex(nullptr, FALSE, nullptr);
//shd_Sockets[i].clientSocket = INVALID_SOCKET;
////strcpy_s(shd_Sockets[i].name, sizeof(shd_Sockets[i].name), "");
//memset(shd_Sockets[i].ProcessID, 0, sizeof(shd_Sockets[i].ProcessID));
char m_name[20];
sprintf_s(m_name, sizeof(m_name), "gpib%d", i + 1);
if(i == 0)
pShd_Sockets->ss_sockets[i].hMutex = CreateMutex(nullptr, FALSE, _T("GPIB0"));
else
pShd_Sockets->ss_sockets[i].hMutex = CreateMutex(nullptr, FALSE, NULL);
for(int j = 0; j<MAX_NUMBER_PROCESS;j++)
pShd_Sockets->ss_sockets[i].clientSocket[j] = INVALID_SOCKET;
}
}
/*DWORD dwSize = MAX_PATH;
wchar_t szMutexName[MAX_PATH];
GetMutexInformation(pShd_Sockets->ss_sockets[0].hMutex, MUTEX_ALL_ACCESS, szMutexName, dwSize, &dwSize))*/
// 使用共享内存数据
if (isFirstProcess)
{
// 第一个进程执行特定逻辑
pShd_Sockets->nCount = 1;
//WaitForSingleObject(pShd_Sockets->ss_sockets[0].hMutex, INFINITE);
WaitForSingleObject(hMutexFirstProcess, INFINITE);
int value;
printf("pleas input value: ");
scanf_s("%d", &value);
pShd_Sockets->ss_sockets[0].BoardID = value;
//Sleep(5000);
//ReleaseMutex(pShd_Sockets->ss_sockets[0].hMutex);
ReleaseMutex(hMutexFirstProcess);
}
else
{
// 后续进程执行特定逻辑
pShd_Sockets->nCount++;
//WaitForSingleObject(pShd_Sockets->ss_sockets[0].hMutex, INFINITE);
//int value;
WaitForSingleObject(hMutexFirstProcess, INFINITE);
printf("Get BoardID = %d\n", pShd_Sockets->ss_sockets[0].BoardID);
//scanf("%d", &value);
//pShd_Sockets->ss_sockets[0].BoardID = value;
ReleaseMutex(hMutexFirstProcess);
}
printf("there are %d processes.\n", pShd_Sockets->nCount);
system("pause");
// 清理资源
Cleanup();
system("pause");
return 0;
}
上面的代码为什么无法实现两个进程的阻塞