Hi all,
I am trying to implement a queue which contains a structure as its member
node. Also to make the queue persistent we are using the mmap unix system
call to map the contenets of memory to a file. so that if the process shuts
down abruptly then we can retriev the contents of the queue by loading the
memory mapped file. In this manner we are trying to build persistence of
objects without using flat files or a database, as this should result in
better performance than normal file I/O. Does anybody see any problems in
the above approach.
In that there are a few issues:-
1. The structure which is to mmapped contains fields which are pointers as
its data members. So the size of the struct is not known before hand. Is
there a way around to allocate exact size of memory depending on the size of
structure instance.
for example the fileds of struct are:
struct QueueNode {
char * event_name;
char * description;
then if we use mmap() to allocate memory for five such nodes usingQuote:};
mmap(addr, 5*sizeof(struct QueueNode), PROT_READ|PROT_WRITE, MAP_SHARED,
mtfd, 0);
then the mmap function just allocates memory for the pointers i.e. 4 bytes
for each char*.
When such instances of structs are populated in the mmapped files, this
creates problems. As the actual contents of event_name and description are
not written to the mmapped file. The work around is to make the fileds as
arrays instead of pointers.
Is there a better solution so that we can allocate memory using mmap while
still keeping the fields of the struct as pointers.
2) Second query is related to the deletion of nodes from such mmapped queue.
For deletion of nodes do we need to use munmap() or free system call is
sufficient.
3) Thirdly do we need to explicitly close the file once the insertion of
nodes is over. In case of abrupt shutdown of the process is there any
guarnatee that the mmapped file would not contain any garbage.
Any clues to the above queries??
Thanks
Satender