Question
In C, write a monitor to implement a shared hash table. The hash table will be a mapping from keys (strings of at most 120
In C, write a monitor to implement a shared hash table. The hash table will be a mapping from keys (strings of at most 120 chars) to values (arbitrary binary data). It should do this in shared memory and use a semaphore to coordinate access to the data structure.
This will be implemented in two files hashtable_shared.h and hashtable_shared.c
It needs to implement the interface: void *make_hashtable(int num_elements, int max_element_size);
This will construct a hash table in an anonymous shared memory region (so it can be shared with child processes).
This shared memory must include the following:
-
Initialized binary semaphore for protecting the data structure
-
Integer containing max element size
-
Integer containing number of elements
-
Integer value that is the total size (in bytes) of the shared memory region
-
Space for num_elements elements. Each element must contain a name (up to 120 chars), a 1-byte integer representing if the element is free(0) or used(1), a value (containing up to element_size bytes), and an integer representing values current size in bytes
At first, all elements must be marked free. The address of the beginning of the shared memory must be returned or NULL should be returned for any failure.
The following will set the value of an element in the hash table: int hash_set(void *hashtable, char *name, void *data, int data_size);
If a used element exists with the given name, it will set the value of that element to the same as the data_size bytes pointed to by the data argument and set the elements values current size to data_size.
If no currently used element has the same name, then it finds the first free element, marks it as used, and sets its name, value, and values current size in bytes, per the given arguments.
Returns 0 on success. On errors, returns a value specific to the source of error, as follows:
-1 if the hashtable is NULL -2 if name too long or is NULL -3 if data_size > max allowed element size -4 if no space exists for the element in hash table -99 if an error other than the above occurs
int hash_delete(void *hashtable, char *name);
Finds an element with the given name in the hash table and marks as free. Returns 0 on success. On errors, returns a value specific to the source of error:
-1 if the hashtable is NULL -2 if name too long or is NULL -5 if element not found -99 if an error other than the above occurs
int hash_get(void *hashtable, char *name, void **buffer, int *size);
Finds a used element with the given name in the hash table. Sets *buffer to a newly allocated piece of memory containing the contents of the value for the element in the hash table. Sets *size to the size in bytes of the elements value.
Returns 0 on success. On errors, returns a value specific to the source of error:
-1 if the hashtable is NULL -2 if name too long or is NULL -5 if element not found -6 if memory to contain a copy of the element could not be allocated -7 if size is NULL -99 if an error other than the above occurs
void hash_detach(void *hashtable);
Detaches the shared hashtable from the current process memory.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started