I'm wondering is it possible to create linked list in shared memory (C, Linux).
Suppose I have library which creates a shared memory and returns a pointer to this memory. Example:
// in lib header
typedef struct _SHM_STR_ {
int i;
char c;
} SHM_STR_t;
// in libomg.so
void lib_ret_shmem(SHM_STR_t** shm_pt)
{
int shm_fd;
SHM_STR_t *shm_map;
if ((shm_fd = shm_open(SHM_FILE, (O_CREAT | O_EXCL | O_RDWR), (S_IREAD | S_IWRITE))) > 0) {
//first time created; init
...
} else if ((shm_fd = shm_open(SHM_FILE, (O_CREAT | O_RDWR), (S_IREAD | S_IWRITE))) < 0) {
return 1;
}
ftruncate(shm_fd, 20*sizeof(SHM_STR_t));
shm_map = (SHM_STR_t *)mmap(0, 20*sizeof(SHM_STR_t), (PROT_READ | PROT_WRITE), MAP_SHARED, shm_fd, 0)
...
// add new member
// linked list or work with the offset in the shared mem?
// increment pointer with offset and return in:
*shm_pt = shm_map;
}
// in proc1.c something like this
int main(int argc, char *argv[])
{
SHM_STR_t *ppp = NULL;
lib_ret_shmem(&ppp);
printf("%d %c\n", ppp->a, ppp->b);
return 0;
}
So in the lib I have allocated shared memory enough for 20 structs SHM_STR_t.
What is the best way to add a new member each time I call lib_ret_shmem()?
Should I work with the offset of the base address of the memory (or with arrays)? Like for member [3] I'll return something like
*shm_pt = shm_map + 3;
OR it's possible to create linked list in this memory? I have the feeling that *next wont point to the correct memory.
Sorry for the terrible explanation :/