4 ms·
I recently recorded a screencast [1], about linux cgroups, and how you can restrict/shape various program resources. For one of the examples, I wrote the follow
by WestCoastJustin 13y ago
I recently recorded a screencast [1], about linux cgroups, and how you can restrict/shape various program resources. For one of the examples, I wrote the following C program to take 50MB of memory, at around the 15:45 mark in the screencast you can see it in action, but this could easy be modified, and add a sleep, to hold the memory for a while. You would most likely need to disable swap if you wanted to use 90% of the free memory, run "free -m; swapoff -a; free -m" as root, then use "swapon -a" to enable it again.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
int i;
char *p;
/* intro message */
printf("Starting ...\n");
/* loop 50 times, try and consume 50 MB of memory */
for (i = 0; i < 50; ++i) {
/* failure to allocate memory? */
if ((p = malloc(1<<20)) == NULL) {
printf("Malloc failed at %d MB\n", i);
return 0;
}
/* take memory and tell user where we are at */
memset(p, 0, (1<<20));
printf("Allocated %d to %d MB\n", i, i+1);
}
/* exit message and return */
printf("Done!\n");
return 0;
}
[1] http://sysadmincasts.com/episodes/14-introduction-to-linux-control-groups-cgroups http://sysadmincasts.com/episodes/14-introduction-to-linux-c...