Created
October 30, 2021 07:18
-
-
Save JackyYin/66e0c198e9f9cdf7314d8e276dc86990 to your computer and use it in GitHub Desktop.
Test RLIMIT_MEMLOCK and mlock behavior.
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 <sys/time.h> | |
#include <sys/resource.h> | |
#include <sys/mman.h> | |
#include <unistd.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <errno.h> | |
#include <string.h> | |
int main() { | |
long page_sz = sysconf(_SC_PAGESIZE); | |
printf("page size: %lu\n", page_sz); | |
const struct rlimit mlock_rlimit = { | |
.rlim_cur = page_sz, | |
.rlim_max = page_sz, | |
}; | |
if ((setrlimit(RLIMIT_MEMLOCK, &mlock_rlimit))) { | |
printf("setrlimit error: %s\n", strerror(errno)); | |
} | |
const long mem_sz = page_sz * 2; | |
void *ptr; | |
if ((posix_memalign(&ptr, page_sz, mem_sz))) { | |
printf("posix_memalign error: %s\n", strerror(errno)); | |
} | |
if (mlock(ptr, mem_sz)) { | |
printf("mlock error: %s\n", strerror(errno)); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
expected result:
explaination:
mlock
will fail to lock a block of memory space larger than the resouce limit.