The examples here are just showing basic usage. How they work in case with multiple processes or threads is tested in tests and also in some separate programs which are not listed in this repository.
You can source the script test_init to set library path.
So to test the examples fast:
For example.c:
source test_init make ./example.out
For example_with_ptr.c:
source test_init make example_with_ptr.out ./example_with_ptr.out
Line 15, we initialize the shared memory:
retval = shm_init(NULL, shm_file_name);
Line 26, get the user's shared memory base for the current process:
shm_base = get_shm_user_base();
Line 34, allocate space in shared memory and get the offset to that space:
str = shm_calloc(string_len, sizeof(char));
Line 41, to access the allocated area, offset is added to the shm base:
strcpy((char *)(shm_base + str), "My test string!");
This is just a copy of example.c. Here we define my_calloc() and my_free().
Both of them are exactly same as ptr_calloc() and
ptr_free(). This is done to show how it actually works.
Further, we just use ptr_calloc() and
ptr_free() instead of shm_calloc() and
shm_free().