
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

int
main()
{
    const char *dbname = "/root/mmap_test";
    int fd = open(dbname, O_RDWR|O_TRUNC|O_CREAT, 0777);
    if (fd < 0) {
	perror("open");
	return -1;
    }

    int len = 1024;
    if (ftruncate(fd, len) < 0) {
	perror("ftruncate");
	return -1;
    }

    void *mmap_ptr = mmap(0, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    if (mmap_ptr == MAP_FAILED) {
	perror("mmap");
	close(fd);
	return -1;
    }

    memset(mmap_ptr, '1', len);
    munmap(mmap_ptr, len);
    close(fd);
    return 0;
}
