Created
July 28, 2022 05:11
-
-
Save msymt/cfb32560e5c385bbe69cd145dd6aeac2 to your computer and use it in GitHub Desktop.
Reading Memory Mapped File written by C# in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <sys/mman.h> | |
#define MAP_SIZE_POW2 16 | |
#define MAP_SIZE (1 << MAP_SIZE_POW2) | |
int main(void) { | |
char *mem; | |
int mmf_fd; | |
const char *mmf_filepath = "/tmp/mmf"; | |
if ((mmf_fd = open(mmf_filepath, O_CREAT|O_RDWR, (mode_t)00700)) == -1) { | |
perror("fail to open mmf file, please create mmf like, dd if=/dev/zero of=/tmp/mmf bs=MAP_SIZE count=1"); | |
exit(EXIT_FAILURE); | |
} | |
mem = mmap(NULL, MAP_SIZE, PROT_WRITE|PROT_READ, MAP_SHARED, mmf_fd, 0); | |
if (mem == MAP_FAILED) { | |
perror("mmap"); | |
exit(EXIT_FAILURE); | |
} | |
int sum = 0; | |
for(int i = 0; i < MAP_SIZE; i++) { | |
sum += mem[i]; | |
if(mem[i] != 0) { | |
printf("mem[%d] = %d\n",i, mem[i]); | |
} | |
} | |
printf("sum = %d\n",sum); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment