Posts

Showing posts from 2015

Linux mmap Based File Write

Breif on mmap: mmap() creates a new mapping in the virtual address space of the calling process. The starting address for the new mapping is specified in addr.  The length argument specifies the length ofthe mapping. You can find more info about mmap [1] There are lots of examples on web which do a entire file copy and its quite easy. One of my colleague wanted a faster file writes, he tried fwrite/write but it was too slow for him and I suggested to use mmaping the file and writing the contents to the file. Unfortunately google didn’t help him out, so just pasting the code so that someone else might get benefit from it. OK here goes the code: #include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <sys/mman.h> #include <errno.h> struct buffer { #define MAX_BUFFER_SIZE 1000  char ptr[MAX_BUFFER_SIZE]; /* array */  int size; /* size of data in buffer */ }; ...