Skip to content

Instantly share code, notes, and snippets.

@cemeyer
Created June 12, 2014 22:26
Show Gist options
  • Save cemeyer/ebaf569a59280d1d2a94 to your computer and use it in GitHub Desktop.
Save cemeyer/ebaf569a59280d1d2a94 to your computer and use it in GitHub Desktop.
basic mmap test
#include <sys/mman.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
int
main(int argc, char **argv)
{
struct stat st;
int fd, err;
void *p;
char a;
if (argc < 2)
return EX_USAGE;
fd = open(argv[1], O_RDONLY);
if (fd < 0)
return EX_OSERR;
err = fstat(fd, &st);
if (err < 0)
return EX_OSERR;
if (st.st_size == 0)
return EX_DATAERR;
p = mmap(NULL, (st.st_size / PAGE_SIZE) * PAGE_SIZE, PROT_READ, 0, fd,
0);
if (p == MAP_FAILED) {
printf("mmap(2) Fail: %s\n", strerror(errno));
return EX_SOFTWARE;
} else
printf("mmap(2): Success\n");
a = *(char *)p;
printf("trap: Success\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment