Linux Kernel - Part 3
In this post I would like to review the function for using mmap that is introduced in Why Linux Has This Syscall?! - Tsoding His repo can be found on Github, where he demos 4 type of usage: (1) allocating memroy (2) reading files (3) IPC (inter-processing cache) (4) Precise control over memory protection Allocating Memory #include <assert.h> #include <sys/mman.h> #define N 10 int main(int argc, char *argv[]) { int *xs = mmap(NULL, N * sizeof(int), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); assert(xs != MAP_FAILED); assert(N >= 2); xs[0] = 0; xs[1] = 1; for (int i = 2; i < N; ++i) { xs[i] = xs[i - 1] + xs[i - 2]; } int err = munmap(xs, N * sizeof(int)); assert(err >= 0); return 0; } The above code request N int size memory from linux kernel through mmap, and return the virtual memory in the end by munmap. ...