我正在尝试使用 cgroups 来停止实际使用过多内存的程序,同时让它们运行,如果它们只是分配而不实际使用大量内存。但是,它似乎不起作用;有什么建议么?在 Ubuntu 机器上,我按如下方式设置我的 cgroup [1]:
#Changing $USER to a system user running the process
sudo apt-get install cgroup-bin
sudo cgcreate -a $USER -g memory:$USER
echo 100M > /cgroup/memory/$USER/memory.limit_in_bytes
#Running the program using cgroups
cgexec -g memory:$USER program
我正在使用以下程序(回答“否”应该有效,应该停止回答“是”)。
#include <stdlib.h>
#include <stdio.h>
#define SIZE (long int) 1*1024*1024*1024*sizeof(char)/*1 GB*/
int main(void)
{
char *str = (char*) malloc(SIZE);
char *ans = (char*) malloc(100);
printf("Write random values to memory? (yes/no): ");
scanf("%s", ans);
if (strcmp(ans,"yes") == 0) {
FILE *f = fopen("/dev/urandom","r");
fread(str,1,SIZE,f);
printf("Success!!\n");
fclose(f);
free(str);
}
else {
printf("Have a good day!\n");
}
return(0);
}