简体   繁体   English

我是否需要在共享内存对象上使用shm_unlink?

[英]Do I need to use shm_unlink on a shared memory object?

I've written a server (GNU C++ / Linux) which runs continuously, and occasionally executes small stand-alone programs to do work. 我编写了一个连续运行的服务器(GNU C ++ / Linux),偶尔执行小型独立程序来完成工作。 In order to efficiently get data to the worker programs, the server creates and maps a shared memory object (code abbreviated for clarity): 为了有效地将数据提供给工作程序,服务器创建并映射共享内存对象(为清楚起见,代码缩写):

int fd = shm_open("/shm_file", O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR);
ftruncate(...);
data = mmap(...);
// etc...
launchWorker(...);   // Start the worker program

The worker program then opens this shared memory in a similar way (except read-only, without the O_CREAT and O_TRUNC, ie we assume it already exists). 然后,worker程序以类似的方式打开这个共享内存(除了只读,没有O_CREAT和O_TRUNC,即我们假设它已经存在)。

When the worker finishes, it closes the file descriptor, unmaps with munmap(...) and unlinks with shm_unlink(...). 当worker完成时,它会关闭文件描述符,使用munmap(...)取消映射并使用shm_unlink(...)取消链接。

At this point, there is a file "/dev/shm/shm_file" which I guess is the shared memory object. 此时,有一个文件“/ dev / shm / shm_file”,我猜是共享内存对象。 Unlinking in the worker doesn't remove it because the server still has it open. 在工作器中取消链接不会删除它,因为服务器仍然打开它。 When the server unlinks it, the file system object disappears. 当服务器取消链接时,文件系统对象消失。 This behavior is consistent with the man page for shm_open / shm_unlink, and works fine for my server/worker case. 此行为与shm_open / shm_unlink的手册页一致,并且适用于我的服务器/工作人员案例。

However, now I'd like the workers to be able to share certain data between themselves, and possibly (for testing) do this when the server is not running. 但是,现在我希望工作人员能够在他们之间共享某些数据,并且可能(用于测试)在服务器未运行时执行此操作。

If I create a shared memory object in one worker program and DO NOT use munmap(...) and shm_unlink(...) when it exits, I note that the shared memory object remains in /dev/shm, and I can open it again in another worker program. 如果我在一个工作程序中创建一个共享内存对象,并且在退出时不要使用munmap(...)和shm_unlink(...),我注意到共享内存对象保留在/ dev / shm中,我可以打开它又在另一个工人计划中。 This is handy. 这很方便。

However, is it safe to do this? 但是,这样做是否安全? (ie repeatedly run a program which maps shared memory, then doesn't unmap/unlink it)? (即重复运行映射共享内存的程序,然后不取消映射/取消链接)? I'm guessing that the unmap() doesn't matter as the memory mapping will vanish with the process, but what about the shm_unlink? 我猜测unmap()并不重要,因为内存映射会随着进程消失,但是shm_unlink呢? Given that the OS decided when to delete the object based on whether it is still being used, if I fail to call shm_unlink() every time, will this cause a leak of some kind? 鉴于操作系统决定何时根据对象是否仍在使用而删除对象,如果我每次都无法调用shm_unlink(),是否会导致某种泄漏?

The only leak is that the file will stay even after the last process that opened it exists. 唯一的泄漏是,即使在打开它的最后一个进程存在之后文件也会保留。

But since that was the intent in this case it is not really a leak as such. 但由于这是本案的意图,因此并非真正的泄密。

The files in /dev/shm behave just like regular files (because they are). /dev/shm文件就像常规文件一样(因为它们是)。

This means that the name can be removed (using unlink or shm_unlink ) but the file data will remain until the name is gone and the last process using it stops doing so (having the file open or it's content mmap :ed counts as using it). 这意味着可以删除名称(使用unlinkshm_unlink ),但文件数据将保留,直到名称消失,最后一个使用它的进程停止这样做(文件打开或内容mmap :ed计为使用它) 。

But there is only the one file, no matter how many times you open and/or mmap it. 但是只有一个文件,无论你打开多少次和/或mmap它。

And when a process exits all open file descriptors are closed, and all memory mappings are removed. 当进程退出时,所有打开的文件描述符都将关闭,并且所有内存映射都将被删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM