共享内存附加进程计数。 [英] Shared memory attached process count.
问题描述
您好,
我想在我的应用程序中使用共享内存。我创建了共享内存并成功使用了两个进程。
我想知道多少(仅计数)进程附加到共享内存。因此,如果只有一个进程附加到共享内存,那么我想破坏共享内存。
用于创建和销毁shred内存的API - >
char key_name [50] = {0};
snprintf(key_name,50," Global \\%d",key);
mMemId = CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,size,key_name);
mMem = MapViewOfFile(mMemId, FILE_MAP_ALL_ACCESS, 0,0, size);
UnmapViewOfFile(mMem);
$
CloseHandle(mMemId);
如果在获得计数后立即附加新进程并且想要销毁共享内存之前该怎么办?
< p style ="margin:0px 0px 10.66px"> 根据文档,
"文件映射对象的映射视图维护内部引用到对象,文件映射对象不会关闭,直到所有对它的引用都被释放"。因此,您无需检查计数。
Hello,
I want to use shared memory in my application. I have created shared memory and successfully using between 2 processes.
I want to know how many (only count) processes attached to shared memory. So if only 1 process is attached to shared memory then I want to destroy shared memory.
API used to create and destroy shred Memory ->
char key_name [50] = {0};
snprintf(key_name, 50, "Global\\%d",key);
mMemId = CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,size,key_name);
mMem = MapViewOfFile(mMemId, FILE_MAP_ALL_ACCESS, 0, 0, size); UnmapViewOfFile(mMem);CloseHandle(mMemId);
What if a new process is attached just after you get the count and before you want to destroy the shared memory?
According to documentation, "mapped views of a file mapping object maintain internal references to the object, and a file mapping object does not close until all references to it are released". Therefore, you do not have to check the count.
这篇关于共享内存附加进程计数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!