Cogs and Levers A blog full of technical stuff

Unix IPC: Memory Mapped Files

This snippet will show you how to open a file and map it into memory.

int fd, pagesize;
char *data;

/* open the file */
fd = open("somefile", O_RDONLY);

/* get the current page size */
pagesize = getpagesize();

/* map the 2nd page into memory */
data = mmap((caddr_t)0, pagesize, PROT_READ, MAP_SHARED, fd, pagesize);

/* start using the data pointer */

Further reading

The mmap system call