better mmap example
[mirrors/Programs.git] / c / mmap / mmap.c
1 //#define _GNU_SOURCE
2
3 #include <stdio.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <sys/mman.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <string.h>
10
11 #define MEM_PATH "hello.bin"
12 #define MEM_SIZE 1048576
13
14 int main() {
15 int fd;
16 fd = open(MEM_PATH, O_RDWR | O_SYNC | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR);
17 ftruncate(fd, MEM_SIZE);
18
19 void *mem;
20 mem = mmap(NULL, MEM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED , fd, 0);
21 close(fd);
22
23 char hello[] = "Hello!";
24 memcpy(mem, hello, sizeof(hello));
25 msync(mem, sizeof(hello), MS_SYNC);
26 }
This page took 0.247167 seconds and 4 git commands to generate.