Skip to content

Instantly share code, notes, and snippets.

@ajkr
Created August 7, 2019 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajkr/f7501a1177647e9a45cb636002c76e39 to your computer and use it in GitHub Desktop.
Save ajkr/f7501a1177647e9a45cb636002c76e39 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
int main(int argc, char** argv) {
if (argc != 3) {
fprintf(stderr, "usage: %s <num blocks> <block size>\n", argv[0]);
exit(1);
}
int num_blocks = atoi(argv[1]);
int block_size = atoi(argv[2]);
for (int i = 0; i < num_blocks; ++i) {
void* alloc = malloc(block_size);
if (alloc == NULL) {
fprintf(stderr, "malloc failed\n");
exit(1);
}
int ret = mlock(alloc, block_size);
if (ret == -1) {
perror("mlock");
exit(1);
}
}
getchar();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment