我不想限制我的进程的 CPU 使用率。相反,我想以消耗更少cpu的方式编写它。我正在处理的系统不允许进行任何系统调用。
所以我不能让它睡觉,我不能做任何 I/O。我唯一剩下的就是让它占用大量内存,我认为这很容易。
我制作了一个大型全局数组并编写了一个 for 循环来写入它。这个过程最终是 cpu 密集型的(在 top/htop 上显示 98-100%)。
所以我想也许for循环计算正在使用cpu,我删除了for循环。但它仍然使用 98-100% cpu,这对我来说没有意义。谁能帮我把它变成记忆密集型的?如果有帮助,我将附上下面的代码。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ARRAY_SIZE 1024*1024*1024 // 4 GB large_array
char large_array[ARRAY_SIZE];
int main(){
while(1){
strncpy(large_array, "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", 256);
strncpy(large_array+256, "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", 256);
strncpy(large_array+512, "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", 256);
strncpy(large_array+768, "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", 256);
strncpy(large_array+1024, "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", 256);
strncpy(large_array+1280, "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", 256);
strncpy(large_array+1536, "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", 256);
strncpy(large_array+1792, "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", 256);
strncpy(large_array+2048, "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", 256);
strncpy(large_array+2304, "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234", 256);
}
}