Skip to content

Instantly share code, notes, and snippets.

@SaveTheRbtz
Created May 15, 2013 22:15
Show Gist options
  • Save SaveTheRbtz/5587869 to your computer and use it in GitHub Desktop.
Save SaveTheRbtz/5587869 to your computer and use it in GitHub Desktop.
#include <sys/mman.h>
#include <err.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define MAP_NAM "/var/tmp/test_map_0"
#define MAP_SZ (1024*1024*1024)
static void
mmap_sigint()
{
return 0;
}
static void
mmap_pause()
{
warnx("paused - press Ctrl+C");
(void)pause();
}
int
main(void)
{
int fd;
unsigned char *arr, *c, acc = 0;
if (signal(SIGINT, mmap_sigint) == SIG_ERR)
err(1, "signal");
warnx("open");
fd = open(MAP_NAM, O_RDWR|O_CREAT|O_TRUNC, 0644);
if (fd == -1)
err(1, "open");
warnx("truncate");
if (ftruncate(fd, MAP_SZ) == -1)
err(1, "ftruncate");
warnx("map");
arr = mmap(NULL, MAP_SZ, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (arr == NULL)
err(1, "mmap");
mmap_pause();
warnx("access\n");
for (c = arr; c < arr + MAP_SZ; ++c)
acc += *c;
warnx("finished: %c\n", acc);
mmap_pause();
warnx("munmap\n");
if (munmap(arr, MAP_SZ) == -1)
err(1, "munmap");
mmap_pause();
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment