曾经看过《编程之美》上提到说使 CPU的使用率固定在百分之多少。然后这次刚好要用到这个东西,下面是一个简单的实现。基于多线程:

Linux 版本:

 #include <iostream>
#include <pthread.h>
#include <time.h>
#include <math.h> using namespace std; typedef long long int int64;
const int NUM_THREADS = ; // CPU 核数
int eachTime = ;
int cpuinfo = ; // 占用率 int64 GetTickCount()
{
timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
int64 sec = now.tv_sec;
int64 nsec = now.tv_nsec;
return sec* + nsec/;
} void* CPUCost(void *args)
{
std::cout << "XXXX CPUCost" << std::endl;
int busyTime = eachTime * cpuinfo / ;
//std::cout << "XXXX cpuinfo = " << cpuinfo << std::endl;
int idleTime = eachTime - busyTime;
int64 startTime = ;
while (true)
{
startTime = GetTickCount();
while((GetTickCount() - startTime) <= busyTime)
{
;
} usleep();
}
} int main(int argc, char **argv)
{
std::cin >> cpuinfo;
pthread_t t[NUM_THREADS];
for(int i = ; i < NUM_THREADS; i++)
{
int ret = pthread_create(&t[i], NULL, CPUCost, NULL);
if(ret)
{
std::cout << "XXXX create err" << std::endl;
}
} pthread_exit(NULL); return ;
}

编译方式: g++ testCPU.cc -lpthread -lrt -o testCPU

注:因为只是用来测试,所以写的很粗糙。大致的原理是对的,细节部分请大家自行优化了。

Windows 版本:

 #include<iostream>
#include<Windows.h>
#include<cmath> using namespace std; int eachTime = ;
void CPU(int busy,int idle); int main()
{
int level;
cin >> level;
int busyTime = eachTime*level / ;
int idleTime = eachTime - busyTime;
CPU(busyTime,idleTime);
return ;
}
void CPU(int busy, int idle)
{
INT64 startTime = ;
while (true)
{
startTime = GetTickCount();
while ((GetTickCount()-startTime)<=busy)
{
;
}
Sleep(idle);
}
}

注: 没有实现多线程。

Linux 控制CPU使用率的更多相关文章

  1. Java获取Linux系统cpu使用率

    原文:http://www.open-open.com/code/view/1426152165201 import java.io.BufferedReader; import java.io.Fi ...

  2. 生成CPU使用率 sin 曲线 控制cpu使用率 编程之美

    入职Oracle 以后想着把之前写过的<编程之美>中控制CPU使用率曲线的程序再写一边, 可是总是由于入职须要学习的东西太多, 没有时间. 程序早就写好了. 最终有机会贴出来了.o(∩∩) ...

  3. 如何让Linux 机器CPU使用率变高

    如何让Linux 机器CPU使用率变高 一.实现 1.单行命令搞定 for i in `seq 1 $(cat /proc/cpuinfo |grep "physical id" ...

  4. Linux平台Cpu使用率的计算

    proc文件系统 /proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以文件系统的方式为内核与进程提供通信的接口.用户和应用程序可以通过/proc得到系统的信息,并可以改变内核的 ...

  5. Linux服务器CPU使用率较低但负载较高

    CPU使用率较低但负载较高 问题描述 Linux 系统没有业务程序运行,通过 top 观察,类似如下图所示,CPU 很空闲,但是 load average 却非常高,如下图所示. 处理办法 load ...

  6. Linux:CPU使用率100%排查方法

    Linux作为一个多任务操作系统,将每个CPU的时间划分为很短的时间片,再通过调度器轮流分配给各个任务使用,因此造成多任务同时运行的错觉. CPU使用率 Linux作为一个多任务操作系统,将每个CPU ...

  7. Linux 下 CPU 使用率与机器负载的关系与区别

    原文链接:  http://blog.chinaunix.net/uid-28541347-id-4926054.html 当我们使用top命令查看系统的资源使用情况时会看到load average, ...

  8. 查看Linux服务器CPU使用率、内存使用率、磁盘空间占用率、负载情况

    [root@server script]# vi monitor.py #!/usr/bin/env python # -*- coding:utf-8 -*- #Author: nulige imp ...

  9. python控制cpu使用率

    以下亲测可行. 使用方法:命令行模式 runing.py -c 2 -t 0.01 -c 指定cpu核数:不指定-c参数默认为所有核数. -t 数值越大,cpu使用率越低. runing.py &qu ...

随机推荐

  1. strtotime 获取之前,之后时间

    一年之前 <?php echo strtotime('-1 year'); ?> 一年之后 <?php echo strtotime('+1 year'); ?> 一月之前 & ...

  2. Android IPC 结篇

    一.概述 Android 的 IPC 方式有 Bundle .共享文件.AIDL .Messenger .ContentProvider .Socket ,我们在实现进程间通信时要选择哪一种方式来实现 ...

  3. x1c 2017 安装mint18的坑——grub2

    折腾一天,死活安装不上.用U盘安装,能进入pe,但是安装时提示无法将grub2安装到/target/ 不论如何分区.如何修改BIOS 安全启动和 启动模式.都是这个问题. ubuntu16.04.3 ...

  4. 类的调用1(被调用的MyFirstJava)

    package com.mec.MyFirstJavaLife.text; public class MyFirstJava { /**   * @param args   */ private in ...

  5. English trip V1 - B 2. May I Help You? 它是多少钱? Teacher:Lamb Key:

    In this lesson you will learn to ask for things in shops  这节课您将学习如何在商店中寻找东西 课上内容(Lesson) 你通常去哪里购物? W ...

  6. Spring Batch 背景

    在开源项目及其相关社区把大部分注意力集中在基于 web 和 SOA 基于消息机制的框架中时,基于 Java 的批处理框架却无人问津,尽管在企业 T 环境中一直都有这种批处理的需求.但因为缺乏一个标准的 ...

  7. P3721 [AH2017/HNOI2017]单旋

    题目:https://www.luogu.org/problemnew/show/P3721 手玩一下即可AC此题. 结论:插入x后,x要么会成为x的前驱的右儿子,要么成为x的后继的左儿子,这取决于它 ...

  8. MySQL的自动补全和语法高亮工具MyCli

    官方地址: RHEL, Centos: We don't have packages for RHEL or Centos, yet. Instead, use pip to install mycl ...

  9. ImportError: sys.meta_path is None, Python is likely shutting down

    python执行过错中,报错:ImportError: sys.meta_path is None, Python is likely shutting down 解决方法:在C:\Users\Adm ...

  10. Windows下如何使用Heroku

    1. 安装 进入https://devcenter.heroku.com/articles/heroku-cli#windows,选择对应版本安装 安装后使用heroku -v可检查版本号 2. 登陆 ...