37
80
* Map the file into our address space for read
81
* & write. Use MAP_SHARED so stores are visible
82
* to other programs.
83
*/
84
if ((pmaddr = mmap(NULL, stbuf.st_size,
85
PROT_READ|PROT_WRITE,
86
MAP_SHARED, fd, 0)) == MAP_FAILED)
87
err(1, "mmap %s", argv[1]);
88
89
/* Don't need the fd anymore because the mapping
90
* stays around */
91
close(fd);
92
93
/* store a string to the Persistent Memory */
94
strcpy(pmaddr, "This is new data written to the
95
file");
96
97
/*
98
* Simplest way to flush is to call msync().
99
* The length needs to be rounded up to a 4k page.
100
*/
101
if (msync((void *)pmaddr, 4096, MS_SYNC) < 0)
102
err(1, "msync");
103
104
printf("Done.\n");
105
exit(0);
106 }
• Lines 67-74: We verify the caller passed a file name that can be
opened. The open call will create the file if it does not already exist.
• Line 76: We retrieve the file statistics to use the length when we
memory map the file.
Chapter 3 Operating SyStem SuppOrt fOr perSiStent memOry
Précédent

- 66/457

Suivant