Created
May 12, 2015 17:21
-
-
Save chrisvest/da3c23231e9fc21f84d9 to your computer and use it in GitHub Desktop.
This program uses memory in page units. As much as you like. Have fun.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <sys/types.h> | |
#include <sys/mman.h> | |
void err(char** argv) { | |
printf("Usage: %s <page count>\n", argv[0]); | |
exit(1); | |
} | |
int main(int argc, char** argv) { | |
if (argc != 2) { | |
err(argv); | |
} | |
long long count = strtoll(argv[1], NULL, 10); | |
if (!count) { | |
err(argv); | |
} | |
int psize = sysconf(_SC_PAGESIZE); | |
size_t len = count * psize; | |
int fd = open("/dev/zero", O_RDWR); | |
void* pointer = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); | |
if (pointer == MAP_FAILED) { | |
perror("mmap"); | |
exit(2); | |
} | |
if (mlock(pointer, len)) { | |
perror("mlock"); | |
exit(3); | |
} | |
for (size_t i = 0; i < count; i++) { | |
((char*)pointer)[i * psize] = 1; | |
} | |
printf("Locked %lld pages of %d bytes\n", count, psize); | |
printf("Press return to stop..."); | |
getchar(); | |
if (munmap(pointer, len)) { | |
perror("munmap"); | |
exit(4); | |
} | |
close(fd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment