C placing a hash table in a shared memory segment

I hope my question makes sense: Programming in C, can I create a hash table in the shared memory segment, so does any process with the appropriate permissions have access to the keys / values โ€‹โ€‹in it? If so, how can I indicate when creating a hash table that I want it to fit in SHM? Is there a recommended hash table implementation that allows this? Thank you very much

+4
source share
3 answers

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

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

The base will be a pointer returned by the functions for creating / opening shared memory, and in each process it will be different . An offset replaces what would be a pointer. When you need to find a value using an offset, you put the base on char* , add an offset to it, and then translate it to your_type* . Something like (bucket_t*)((char*)base + offset) .

Assuming your hash implementation needs pointers in general. Some do not if they use only unambiguous buckets and there are no lists of values.

Another tricky thing is that you need to manage your memory yourself. Instead of calling malloc (), you create your own function, perhaps call it shm_hash_alloc (). A quick start is simply to keep the pointer and increase it when allocating memory and not bother to free it. Later you can use an array of pointers (offsets) for free lists of various sizes and sizes, or if you know your types of objects, a list for each type. In each free block, you save a pointer to the next free block, and you know the size, due to the list in which it is included. There are even more fancy methods, but they probably don't need you.

+5
source

I downloaded the linux shared memory hash table library for SF (libshmht), I designed it with a performance characteristic as the main function and write / write access for home. I think this is useful as a cache and IPC system. Also implements read / write locks for sharing between many processes.

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

+3
source

A hash table is just a data structure. As long as modules accessing shared memory know how the structure is built, they can access it. It doesnโ€™t matter which implementation you use if all the modules are involved in reading it.

Think of it like a newspaper. You create your own personal memory segment - this is a local newspaper. Then you want to share it with all cities - you can, as long as people speak the same language and can read it. There is no special language for exchange, it just has to be the one that everyone understands.

In your case, the same thing - you can use any hash table implementation if all threads use the same.

+1
source

Source: https://habr.com/ru/post/1344683/


All Articles