美文网首页
linux中限制进程的cpu使用率

linux中限制进程的cpu使用率

作者: 一路向后 | 来源:发表于2023-09-15 13:06 被阅读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为程序进程号
    

    4.再次使用top命令,可以看到cpu使用率已限制在5%以内

    相关文章

      网友评论

          本文标题:linux中限制进程的cpu使用率

          本文链接:https://www.haomeiwen.com/subject/ibgvvdtx.html