我正在尝试使用Unix共享内存创建与此Windows代码等效的内容,但是当文档说并设置-1
值时,而不是返回errno
,而不是共享内存已创建,它返回65537。
我只是想让它循环,直到在另一个进程中创建了内存,然后返回id并'连接到它'。
Windows代码:
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;
}