Created
August 25, 2011 17:23
-
-
Save Bystroushaak/1171204 to your computer and use it in GitHub Desktop.
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 <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#define MEMINFO "/proc/meminfo" | |
#define MEM "/dev/mem" | |
#define MB 1024 * 1024 | |
// Returns free memory in kB | |
int get_free_memory(){ | |
FILE *f; | |
char buff[80]; | |
f = fopen(MEMINFO, "r"); | |
fscanf(f, "%*s%*s%*s%*s%s", buff); // MemFree: %s | |
fclose(f); | |
return atoi(buff); | |
} | |
int main(int argc, char *argv[]){ | |
// check permissions | |
if (geteuid() != 0){ | |
fputs("This program needs to run under the root (euid == 0)!\n", stderr); | |
return 1; | |
} | |
// check procfs | |
if(access(MEMINFO, R_OK) == -1){ | |
fputs("This program needs procfs (http://en.wikipedia.org/wiki/proc)!\n", stderr); | |
return 2; | |
} | |
// check swapoff | |
if (system("which swapoff > /dev/null") != 0){ | |
fputs("This program needs 'swapoff' command!\n", stderr); | |
return 3; | |
} | |
// turn off swap | |
printf("Turning off swap .. "); | |
if (system("swapoff -a") != 0){ | |
fputs("Oucha, cant turn off swap! :(\n", stderr); | |
return 4; | |
} | |
puts("done."); | |
// eat free memory | |
char *v; | |
uint i, amount; | |
puts("Eating memory:"); | |
while((amount = get_free_memory()) > 0){ | |
printf("\tFree memory: %dkB\n", amount); | |
if ((v = malloc(MB)) != 0) | |
for(i = 0; i < MB; i++) | |
v[i] = 0xFF; | |
else | |
break; | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment