Skip to content

Instantly share code, notes, and snippets.

@pja237
Created February 24, 2021 12:57
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 pja237/b0e9a49be64a20ad1af905305487d41a to your computer and use it in GitHub Desktop.
Save pja237/b0e9a49be64a20ad1af905305487d41a to your computer and use it in GitHub Desktop.
mempoc for singularity-cgroups issue
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/wait.h>
#define G 1073741824L
int main(int argc, char *argv[])
{
int j;
if(argc!=3) {
printf("Syntax ./mempoc SIZE_IN_GB NUM_CHILDREN\n");
exit(1);
}
for(j=0; j<atoi(argv[2]); j++) {
if(fork()==0) {
long size;
char *res;
size=atoi(argv[1])*G;
printf("Calling mmap %s from child %d with %ld bytes...\n", argv[1], j, size);
res=(char *) mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_SHARED, -1, 0);
if(res == MAP_FAILED) {
perror("mmap failed");
return 1;
}
printf("Waiting 10 seconds...\n");
sleep(10);
while(1) {
printf("Filling pages with memset...\n");
memset(res, 1, size);
// printf("Filling pages neanderthal way...\n");
// for(long i=0; i<size-1; i++) {
// res[i]='\0';
// }
sleep(5);
}
munmap(res, size);
exit(0);
}
}
while(wait(NULL)>0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment