/* Orion Lawlor's Short UNIX Examples, olawlor@acm.org 2003/8/18

Shows how to use the basic "mmap" syscall to gain access to 
some private memory.  The same technique can be used to 
map a file into memory.

WARNING: This uses MAP_ANONYMOUS, which works in Linux and OS X,
but not on some other weirder UNIXes.  mmap doesn't exist on Windows.
On some machines, you have to open("/dev/zero") to map in some zeros.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>

int main() {
	int len=1024*1024;
	int fd=-1; /* no file required with MAP_ANONYMOUS flag */
	void *addr=mmap((void *)0,len,PROT_READ+PROT_WRITE,MAP_ANONYMOUS+MAP_SHARED,fd,0);
	int *buf=(int *)addr;
	if (addr==MAP_FAILED) {perror("mmap"); exit(1);}
	buf[3]=7;
	buf[2]=buf[3];
	printf("mmap returned %p, which seems readable and writable\n",addr);
	munmap(addr,len);
	
	return 0;
}
/*<@>
<@> ******** Program output: ********
<@> mmap returned 0x2aff8c0f1000, which seems readable and writable
<@> */
