c 将哈希表放入共享内存段

发布于 2024-10-25 10:22:01 字数 126 浏览 2 评论 0原文

希望我的问题有意义: 用 C 编程,我可以在共享内存段中创建哈希表,以便任何具有适当权限的进程都可以访问其中的键/值吗? 如果是这样,我如何在创建哈希表时指定我希望将其放入 SHM 中? 是否有任何推荐的哈希表实现可以实现这一点? 多谢

Hope my question makes sense:
Programming in C, can I create a hash table in a shared memory segment, so any process with proper permissions has access to the keys/values in it?
If so, how can I specify at hash table creation that I want it put in the SHM?
Is there any recommended hash table implementation that allows this?
Thanks a lot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情场扛把子 2024-11-01 10:22:01

从我的头脑中,我不知道有任何库可以做到这一点。

我确实知道,如果您编写自己的库或修改现有的库,您需要做的棘手事情是跟踪并修复任何使用指针的代码,并将其替换为使用基址+偏移量的代码。

基址将是共享内存创建/打开函数返回的指针,并且在每个进程中它都会不同。偏移量取代了指针。当您需要通过偏移量查找值时,可以将基数转换为 char*,添加偏移量,然后将其转换为 your_type*。类似(bucket_t*)((char*)base + offset)

假设您的哈希实现完全需要指针。有些人如果只使用单值存储桶而不使用值列表,则不会这样做。

另一个棘手的事情是您需要自己管理内存。您可以创建自己的函数,或者将其命名为 shm_hash_alloc(),而不是调用 malloc()。快速开始是只保留一个指针并在分配内存时递增它,而不需要释放它。稍后,您可以使用指针数组(偏移量)来释放两个大小的各种幂的列表,或者如果您知道对象类型,则可以使用每种类型的列表。在每个空闲块中,您存储指向下一个空闲块的指针,并且由于它所在的列表而知道其大小。甚至还有更奇特的方法,但您可能不需要它们。

Off the top of my head I don't know any libraries that do this.

I do know that if you write your own or modify an existing library, the tricky thing that you need to do is track down and fix any code that uses pointers and replace it with code that uses base + offset.

The base would be the pointer returned by the shared memory create/open functions and it will be different in each process. The offset replaces what would be a pointer. When you need to locate a value via offset, you cast base to char*, add the offset to that and then cast it to your_type*. Something like (bucket_t*)((char*)base + offset).

That's assuming your hash implementation needs pointers at all. Some don't if they only use single-value buckets and no value lists.

The other tricky thing is that you need to manage the memory yourself. Instead of calling malloc(), you make your own function, maybe name it shm_hash_alloc(). A quick start is to just keep a pointer and increment it when allocating memory and don't bother with freeing it. Later you can use an array of pointers(offsets) to free lists of various power of two sizes or if you know your object types, a list for each type. In each free block you store the pointer to the next free block and you know the size because of what list it's on. There are even fancier methods but you probably don't need them.

Oo萌小芽oO 2024-11-01 10:22:01

我将Linux的共享内存哈希表库上传到SF(libshmht),我以性能为主要特征和读/写访问同类访问时间来开发它。我认为它作为缓存和 IPC 系统很有用。
还实现了在多个进程之间共享的读/写锁。

http://sourceforge.net/projects/libshmht/

I uploaded a shared memory hash table library for linux to SF (libshmht), I developed it with the performance as main feature and read / write access homegeneous access time. I think it's usefull as cache and as IPC system.
Also implements read/write locks for sharing between many processes.

http://sourceforge.net/projects/libshmht/

︶葆Ⅱㄣ 2024-11-01 10:22:01

哈希表只是一种数据结构。只要访问共享内存的模块知道该结构是如何构建的,它们就可以访问它。使用哪种实现并不重要,只要涉及的所有模块都知道如何读取它即可。

把它想象成一份报纸。你创建你自己的私人记忆片段——那是一份镇上的当地报纸。然后你想与周围所有的城镇分享——你可以,只要人们说同一种语言并且能读懂它。分享没有特殊的语言,只要是每个人都能理解的语言即可。

在你的情况下也是如此 - 你可以使用任何哈希表实现,只要所有线程使用相同的。

Hash table is just a data structure. As long as the modules accessing the shared memory know how the structure is built, they can access it. It doesn't matter which implementation you use, as long as all the modules involved know how to read it.

Think of it as a newspaper. You create your own private memory segment - that's a town local paper. Then you want to share it with all the towns around - you can, as long as the people speak the same language and can read it. There's no special language for sharing, it just has to be the one everyone understands.

Same in your case - you can use any hash table implementation, as long as all the threads use the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文