Hello everyone,
How to connect hash table with shared memory?.I need to update and retrieve data from the shared memory through hash table.
Thanks in advance
Hello everyone,
How to connect hash table with shared memory?.I need to update and retrieve data from the shared memory through hash table.
Thanks in advance
You put a hash table in shared memory the same way you put a hash table in other memory -- you write to the memory. What does your existing code look like? Maybe it can be adapted.
Of course, you'll also need a mutex of some sort to prevent writers from messing up the table while readers are trying to use it.
I am using linux operating system.I need to create one hash table and need to point that hash table to shared memory.The database data that we are entering from the server program.
So the hash table isn't in shared memory, just points to it? What do your current data structures look like?
The hash table should consist of one primary key of database(key) and the value is the address of the corresponding shared memory.................
So the table isn't shared, but the data it holds is shared?
Using the mmap system call you can map a file into shared memory. After you map it, the file effectively "is" memory; reads from the memory read from the file, writes to the memory write to the file. The OS only loads the parts you use. When you map it, it gives you a pointer; the pointer plus an offset gives you a particular location in that file.
The hashit library is a generic hash table that stores pointers. Pointers to shared memory aren't special, so it'll hold those too.
If you need more help you're going to have to stop the monosyllabic nonanswers and explain your data structures and modes of access in detail.
hai,
The hash table which consist of one key and data.The data it should pointing to shared memory.In shared memory(shmid) we are storing the database.(eg:- employee details).So by specifying the key i need to access the "data base" details from the shared memory.I wrote hash table program for storing the key and data.The key i am specifying the database name(there are n number of databases),but i am confused with the shared memory accessing?????
I've given you concrete examples and working library implementations. I can't give you any further detail until you give me anything to work with. You're going to have to explain your data structures and modes of access.
This is what an answer in detail might look like:
char *key="databasename";
struct database
{
members;
members;
more_members;
stuff_of_potentially_varying_size;
};
struct hash_table
{
struct
{
struct database *d;
char *key;
} table[1024];
};
my_variable = get_hash_entry(my_table, my_key);
add_hash_entry(my_table, my_key, my_database);
This is what a monosyllabic nonanswer might look like:
Please, I'm trying to help you.
Please post some code. As corona says, 'my crystal ball is broken today'. Otherwise you are wasting your time.
Databse
Since you refuse to tell us anything about your code, I can't help you any more than I have. Sorry. It's like trying to find the right part for a car when you don't know what the car is, and whenever I ask, you tell me is "it's green!"
Hi,
how to implement a hashtable whose KEYs are strings and VLAUEs are
again hashtables.
ie key - is a string and value -is another hashtable .????
Thanks in advance...
Hi crona,,
Here i am attaching my code that handle single hashtable operation.I need to modify this program that should handle nested hashtable.The hashtable whose KEYs are strings and Values are again hashtables.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
typedef size_t hash_size;
struct hashnode_s {
char *key;
void *data;
struct hashnode_s *next;
};
typedef struct hashtbl {
hash_size size;
struct hashnode_s **nodes;
hash_size (*hashfunc)(const char *);
} HASHTBL;
static char *mystrdup(const char *s)
{
char *b;
if(!(b=malloc(strlen(s)+1))) return NULL;
strcpy(b, s);
return b;
}
static hash_size def_hashfunc(const char *key)
{
hash_size hash=0;
while(*key) hash+=(unsigned char)*key++;
return hash;
}
HASHTBL *hashtbl_create(hash_size size, hash_size(*hashfunc)(const char *))
{
HASHTBL *hashtbl;
if(!(hashtbl=malloc(sizeof(HASHTBL)))) return NULL; if(!(hashtbl->nodes=calloc(size, sizeof(struct hashnode_s*)))) {
free(hashtbl);
return NULL;
}
hashtbl->size=size;
if(hashfunc) hashtbl->hashfunc=hashfunc;
else hashtbl->hashfunc=def_hashfunc;
return hashtbl;
}
int hashtbl_insert(HASHTBL *hashtbl, const char *key, void *data)
{
struct hashnode_s *node;
hash_size hash=hashtbl->hashfunc(key)%hashtbl->size;
node=hashtbl->nodes[hash];
while(node) {
if(!strcmp(node->key, key)) {
node->data=data;
return 0;
}
node=node->next;
}
if(!(node=malloc(sizeof(struct hashnode_s)))) return -1;
if(!(node->key=mystrdup(key))) {
free(node);
return -1;
}
node->data=data; /*this data should point to the another hash table*/
node->next=hashtbl->nodes[hash];
hashtbl->nodes[hash]=node;
return 0;
}
void *hashtbl_get(HASHTBL *hashtbl, const char *key)
{
struct hashnode_s *node;
hash_size hash=hashtbl->hashfunc(key)%hashtbl->size;
node=hashtbl->nodes[hash];
while(node) {
if(!strcmp(node->key, key)) return node->data;
node=node->next;
}
return NULL;
}
int main()
{
HASHTBL *hashtbl;
if(!(hashtbl=hashtbl_create(16, NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() failed\n");
exit(EXIT_FAILURE);
}
hashtbl_insert(hashtbl, "Sports", "cricket");
printf("After insert:%s\n",hashtbl_get(hashtbl, "Sports"));
return 0;
}
Now i am trying to implement h(sports)=>hashtable,ie it should contain hash table that should contain records of cricket,football,tennis etc.....
H=>
key data
Sports H1
Animals H2
H1=>
cricket ....
football ....
H2=>
cat ......
dog ......
Thanks in advance
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
typedef size_t hash_size;
struct hashnode_s {
char *key;
void *data;
struct hashnode_s *next;
};
typedef struct hashtbl {
hash_size size;
struct hashnode_s **nodes;
hash_size (*hashfunc)(const char *);
} HASHTBL;
static char *mystrdup(const char *s)
{
char *b;
if(!(b=malloc(strlen(s)+1))) return NULL;
strcpy(b, s);
return b;
}
static hash_size def_hashfunc(const char *key)
{
hash_size hash=0;
while(*key) hash+=(unsigned char)*key++;
return hash;
}
HASHTBL *hashtbl_create(hash_size size, hash_size(*hashfunc)(const char *))
{
HASHTBL *hashtbl;
if(!(hashtbl=malloc(sizeof(HASHTBL)))) return NULL; if(!(hashtbl->nodes=calloc(size, sizeof(struct hashnode_s*)))) {
free(hashtbl);
return NULL;
}
hashtbl->size=size;
if(hashfunc) hashtbl->hashfunc=hashfunc;
else hashtbl->hashfunc=def_hashfunc;
return hashtbl;
}
int hashtbl_insert(HASHTBL *hashtbl, const char *key, void *data)
{
struct hashnode_s *node;
hash_size hash=hashtbl->hashfunc(key)%hashtbl->size;
node=hashtbl->nodes[hash];
while(node) {
if(!strcmp(node->key, key)) {
node->data=data;
return 0;
}
node=node->next;
}
if(!(node=malloc(sizeof(struct hashnode_s)))) return -1;
if(!(node->key=mystrdup(key))) {
free(node);
return -1;
}
node->data=data; /*this data should point to the another hash table*/
node->next=hashtbl->nodes[hash];
hashtbl->nodes[hash]=node;
return 0;
}
void *hashtbl_get(HASHTBL *hashtbl, const char *key)
{
struct hashnode_s *node;
hash_size hash=hashtbl->hashfunc(key)%hashtbl->size;
node=hashtbl->nodes[hash];
while(node) {
if(!strcmp(node->key, key)) return node->data;
node=node->next;
}
return NULL;
}
int main()
{
HASHTBL *hashtbl;
if(!(hashtbl=hashtbl_create(16, NULL))) {
fprintf(stderr, "ERROR: hashtbl_create() failed\n");
exit(EXIT_FAILURE);
}
hashtbl_insert(hashtbl, "Sports", "cricket");
printf("After insert:%s\n",hashtbl_get(hashtbl, "Sports"));
return 0;
}
I've put it in code tags for you to make it legible.
You don't need to modify your hash table at all to make it store hash tables. It's already capable of storing any kind of pointer.
int main()
{
HASHTBL *outer=hashtbl_create(16, NULL);
HASHTBL *inner=hashtbl_create(16, NULL);
hashtbl_insert(inner, "sports", "cricket");
hashtbl_insert(outer, "sports_table", inner);
{
HASHTBL *inner=hashtbl_get(outer, "sports_table");
const char *str=hashtbl_get(inner, "sports");
printf("got %s\n", str);
}
}