
#include <fcntl.h>
#include <io.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>

void *mmap(void *start, size_t length, int prot, int flags,           int
fd, off_t offset);
int munmap(void *start, size_t length);

void main( void)
{
    int i;
    int size = 100;
    int fd, fd2;
    char* buf, *buf2;
    char* buffer = "0123456789012345678901234567890123456789012345678901234567890123456789";

    fd = open( "map1", O_CREAT | O_RDWR | O_BINARY, S_IREAD|S_IWRITE);
    write( fd, buffer, strlen(buffer));
    close(fd);

    fd = open( "map1", O_CREAT | O_RDWR | O_BINARY, S_IREAD|S_IWRITE);
    buf  = mmap( NULL, strlen(buffer), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    close(fd);
    printf( "mapped %x\n", buf);
    printf( "char[0]=%c\n", buf[0]);
    printf( "char[1]=%c\n", buf[1]);
    buf[0] = 'a';

    fd2 = open( "map2", O_CREAT | O_RDONLY);
    buf2  = mmap( NULL, size, PROT_READ, MAP_PRIVATE, fd2, 0);
    printf( "mapped %x\n", buf2);

    // not needed for SHARED map: msync( buf, -1, 0);

    munmap( buf, size);
    munmap( buf2, size);

    buf = 0;
    buf  = mmap( NULL, size, PROT_READ, MAP_ANON, -1, 0);
    printf( "anon mapped %x\n", buf);
    munmap( buf, 0);


    for( i=0; i<200; i++) {
        buf2  = mmap( NULL, size, PROT_READ, MAP_PRIVATE, fd2, 0);
        printf( "#%d, mapped %x\n", i, buf2);
        munmap( buf2, 0);
    }

}

