220
9 reader() {
10
if (var1_valid == True) {
11
print (var1);
12
}
14 }
The reader() will read the data in var1 if the var1_valid flag is set to True (line 10),
and var1_valid can only be True if var1 has been flushed (lines 4 and 5).
We can now modify the code from Listing 12-7 to introduce this “valid” flag. In
Listing 12-14, we separate the code into writer and reader programs and map two
integers instead of one (to accommodate for the flag). Listing 12-15 shows the reading to
persistent memory example.
Listing 12-14. Example of writing to persistent memory with a write
dependency; the code does not flush
33 #include
34 #include
35 #include
36 #include
37
38 int main(int argc, char *argv[]) {
39
int fd, *ptr, *data, *flag;
40
41
fd = open("/mnt/pmem/file", O_CREAT|O_RDWR, 0666);
42
posix_fallocate(fd, 0, sizeof(int)*2);
43
44
ptr = (int *) mmap(NULL, sizeof(int)*2,
45
PROT_READ | PROT_WRITE,
46
MAP_SHARED_VALIDATE | MAP_SYNC,
47
fd, 0);
48
49
data = &(ptr[1]);
50
flag = &(ptr[0]);
51
*data = 1234;
52
*flag = 1;
53
Chapter 12 Debugging persistent MeMory appliCations
9 reader() {
10
if (var1_valid == True) {
11
print (var1);
12
}
14 }
The reader() will read the data in var1 if the var1_valid flag is set to True (line 10),
and var1_valid can only be True if var1 has been flushed (lines 4 and 5).
We can now modify the code from Listing 12-7 to introduce this “valid” flag. In
Listing 12-14, we separate the code into writer and reader programs and map two
integers instead of one (to accommodate for the flag). Listing 12-15 shows the reading to
persistent memory example.
Listing 12-14. Example of writing to persistent memory with a write
dependency; the code does not flush
33 #include
34 #include
35 #include
36 #include
37
38 int main(int argc, char *argv[]) {
39
int fd, *ptr, *data, *flag;
40
41
fd = open("/mnt/pmem/file", O_CREAT|O_RDWR, 0666);
42
posix_fallocate(fd, 0, sizeof(int)*2);
43
44
ptr = (int *) mmap(NULL, sizeof(int)*2,
45
PROT_READ | PROT_WRITE,
46
MAP_SHARED_VALIDATE | MAP_SYNC,
47
fd, 0);
48
49
data = &(ptr[1]);
50
flag = &(ptr[0]);
51
*data = 1234;
52
*flag = 1;
53
Chapter 12 Debugging persistent MeMory appliCations
