Created
March 29, 2021 08:11
-
-
Save minhaj94/684f7d7e37d1a923bdd6571389fd5561 to your computer and use it in GitHub Desktop.
Script to test memory usage conditions using mlock and mmap.
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/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