linux中限制进程的cpu使用率
2023-09-15 本文已影响0人
一路向后
1.编写测试程序
#include <stdio.h>
int main()
{
while(1)
{
int i = 0;
for(i=0; i<100000000; i++)
{
}
}
return 0;
}
2.编译并运行程序, 使用top命令,可看到cpu使用率几乎100%
3.使用cgroup限制cpu使用率
# cd /sys/fs/cgroup/cpu,cpuacct
# mkdir tmp-cgroup
# cd tmp-cgroup
# echo 100000 > cpu.cfs_period_us
# echo 5000 > cpu.cfs_quota_us
// cpu.cfs_period_us为时间窗口, cpu.cfs_quota_us为限制时间,单位为微秒,cpu.cfs_quota_us 大于 cpu.cfs_period_us表示多核
# echo 1494 > tasks
// 1494为程序进程号