我正在尝试使用 Unix Shared Memory 创建与此 Windows 代码等效的代码,但是当尚未创建共享内存时,它不会-1
像文档所说的那样返回并设置值,而是返回 65537。errno
我只是想让它循环,直到在另一个进程中创建了内存,然后返回 id 并“连接到它”。
窗口代码:
HANDLE hClientSlot = NULL;
unsigned long long* clientSlot;
while(hClientSlot == NULL)
{
hClientSlot = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, CLIENTSLOTNAME);
if(hClientSlot == NULL)
{
std::cout << "Could not open Client Slot file mapping object:" << GetLastError() << std::endl;
}
}
clientSlot = reinterpret_cast<unsigned long long*>( MapViewOfFile(hClientSlot, FILE_MAP_ALL_ACCESS, 0, 0, 1 * sizeof(unsigned long long)) );
if(!clientSlot)
{
std::cout << "Error: Failed to map Client Slot!" << std::endl;
}
Unix 代码:编辑:根据@Zack 的反馈添加了 Key_T 内容
unsigned long long* clientSlot;
int clientSlotID = -1;
key_t slotKey;
slotKey = ftok(CLIENTSLOTNAME, ID);
while(clientSlotID == -1)
{
clientSlotID = shmget(slotKey, sizeof(unsigned long long), 0666);
}
clientSlot = (unsigned long long*) shmat(clientSlotID, NULL, 0);
if(!clientSlot)
{
std::cout << "Error: Failed to map client slot!" << std::endl;
}