crafcat7 opened a new pull request, #13511:
URL: https://github.com/apache/nuttx/pull/13511

   ## Summary
   Refer to https://github.com/apache/nuttx/pull/5997 and implement an msync 
based on the current mmap
   Follow https://man7.org/linux/man-pages/man2/msync.2.html
   
   ## Impact
   Support msync
   
   ## Testing
   Based on anonymous mapping and rammap demo is OK
   ```
   #include <stdio.h>
   #include <stdlib.h>
   #include <string.h>
   #include <sys/mman.h>
   #include <unistd.h>
   
   int main() {
       const char *text = "This is a demo of anonymous mmap!";
       size_t length = strlen(text) + 1; 
   
       char *map = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | 
MAP_PRIVATE, -1, 0);
       if (map == MAP_FAILED) {
           perror("mmap");
           return 1;
       }
   
       memcpy(map, text, length);
   
       printf("Mapped memory contains: %s\n", map);
   
       if (munmap(map, length) == -1) {
           perror("munmap");
           return 1;
       }
   
       return 0;
   }
   ```
   
   ```
   #include <stdio.h>
   #include <stdlib.h>
   #include <fcntl.h>
   #include <unistd.h>
   #include <sys/mman.h>
   #include <sys/stat.h>
   #include <string.h>
   
   int main(int argc, char *argv[]) {
       const char *filepath = "example.txt";
       const char *text = "This is a demo of mmap!";
       
       int fd = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
       if (fd == -1) {
           perror("open");
           return 1;
       }
   
       size_t filesize = strlen(text);
       if (ftruncate(fd, filesize) == -1) {
           perror("ftruncate");
           close(fd);
           return 1;
       }
   
       char *map = mmap(NULL, filesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 
0);
       if (map == MAP_FAILED) {
           perror("mmap");
           close(fd);
           return 1;
       }
   
       memcpy(map, text, filesize);
   
       if (msync(map, filesize, MS_SYNC) == -1) {
           perror("msync");
       }
   
       if (munmap(map, filesize) == -1) {
           perror("munmap");
       }
   
       close(fd);
       
       printf("File mapped and modified successfully.\n");
   
       return 0;
   }
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to