Skip to content

Instantly share code, notes, and snippets.

@minhaj94
Created March 29, 2021 08:11
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 minhaj94/684f7d7e37d1a923bdd6571389fd5561 to your computer and use it in GitHub Desktop.
Save minhaj94/684f7d7e37d1a923bdd6571389fd5561 to your computer and use it in GitHub Desktop.
Script to test memory usage conditions using mlock and mmap.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
void main(int argc, char *argv[])
{
if( argc != 2 ) {
printf("Pass correct number of arguments");
return;
}
if ( mlockall(MCL_CURRENT|MCL_FUTURE) == -1 )
perror("mlockall failed");
const char *filepath = "file";
int fd = open(filepath, O_RDONLY);
if (fd == -1)
{
perror("Error opening file");
return;
}
struct stat info = {0};
if (fstat(fd, &info) == -1)
{
perror("Error getting file size");
return;
}
char *map = mmap(0, info.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (map == MAP_FAILED)
{
perror("Error mmapping the file");
return;
}
char *data;
long int i=0;
long int one_giga_byte = (1024*1024*1024);
int gb_multipler = atoi(argv[1]) ;
data = (char *) malloc(one_giga_byte*gb_multipler);
for(;i<one_giga_byte*gb_multipler;i++){
data[i] = (char) rand();
}
sleep(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment