Skip to content

Instantly share code, notes, and snippets.

@ammarfaizi2
Created June 22, 2022 14:15
Show Gist options
  • Save ammarfaizi2/44ad5a9843c43c6461b69cd618a1d511 to your computer and use it in GitHub Desktop.
Save ammarfaizi2/44ad5a9843c43c6461b69cd618a1d511 to your computer and use it in GitHub Desktop.
#include <errno.h>
#include <string.h>
#include <sys/mman.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
size_t len;
void *p;
int ret;
if (argc != 2) {
printf("Usage: %s [size in bytes]\n", argv[0]);
return 0;
}
len = (size_t) strtoull(argv[1], NULL, 10);
printf("mmap'ing %zu bytes...\n", len);
p = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
-1, 0);
if (p == MAP_FAILED) {
ret = errno;
perror("mmap");
return ret;
}
printf("Locking...\n");
ret = mlock(p, len);
if (ret < 0) {
ret = errno;
perror("mlock");
goto out;
}
printf("Faulting...\n");
memset(p, 'x', len);
printf("Sleeping for 10 seconds...\n");
sleep(10);
printf("Unlocking...\n");
munlock(p, len);
out:
printf("munmap'ing...\n");
munmap(p, len);
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment