-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexample.c
More file actions
55 lines (40 loc) · 996 Bytes
/
example.c
File metadata and controls
55 lines (40 loc) · 996 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "shm_alloc.h"
#include <stdlib.h>
#include <string.h>
int main()
{
char *shm_file_name;
bool retval;
shm_file_name = "shm_temp_file";
/*
* Initialize shm
*/
retval = shm_init(NULL, shm_file_name);
if (retval == false) {
fprintf(stderr, "shm_init() failed!");
exit(EXIT_FAILURE);
}
shm_offt str;
/* needs to be uint8_t */
uint8_t *shm_base;
shm_base = get_shm_user_base();
size_t string_len = 100;
/*
* shm calloc returns an offset which needs to be added to
* the shared memory base to access the memory
*/
str = shm_calloc(string_len, sizeof(char));
if (str == SHM_NULL) {
fprintf(stderr, "Out of memory\n");
exit(EXIT_FAILURE);
}
strcpy((char *)(shm_base + str), "My test string!");
printf("%s\n", (char *)(shm_base + str));
/* free from the shared memory */
shm_free(str);
/* release resources held by shared memory */
shm_deinit();
/* User's responsibilty to delete the file after usage */
remove(shm_file_name);
return 0;
}