当前位置: 首页 > news >正文

乡镇信息公开网站建设制度做网站和彩票的同步开奖怎么做

乡镇信息公开网站建设制度,做网站和彩票的同步开奖怎么做,wordpress 一小时建站,株洲市建设局官方网站工作中我们经常会遇到这样的问题#xff0c;需要模拟cpu的负载程序#xff0c;例如模拟cpu占有率抬升10%、20%、50%、70%等#xff0c;那这样的程序应该如何实现呢#xff1f;它的原理是什么样的呢#xff1f; 思想 创建一个应用程序#xff0c;该应用程序的作用可以根…工作中我们经常会遇到这样的问题需要模拟cpu的负载程序例如模拟cpu占有率抬升10%、20%、50%、70%等那这样的程序应该如何实现呢它的原理是什么样的呢 思想 创建一个应用程序该应用程序的作用可以根据用户的设置占用指定的cpu占有率。例如用户指定占用10%则该应用程序占用cpu占有率为10%若设置cpu占有率为50%则应用程序程序的cpu占有率为50%。 占用固定cpu占有率的程序 #include iostream #include pthread.h #include time.h #include math.h #include unistd.husing namespace std;typedef long long int int64; const int NUM_THREADS 8; //CPU core nums int INTERVAL 100; int cpuinfo 70; //CPU utilization rate// time unit is ms int64 GetTickCount() {timespec now;int64 sec, nsec;clock_gettime(CLOCK_MONOTONIC, now);sec now.tv_sec;nsec now.tv_nsec;return sec * 1000 nsec / 1000000; }void* CPUCost(void *args) {int busyTime INTERVAL * cpuinfo / 100;int idleTime INTERVAL - busyTime;int64 startTime 0;std::cout XXXX CPUCost std::endl;std::cout XXXX cpuinfo cpuinfo std::endl;/** within INTERVAL ms, INTERVAL busyTime idleTime,* spend busyTime ms to let cpu busy,* spend idleTime ms top let cpu idle*/while (true) {startTime GetTickCount();while((GetTickCount() - startTime) busyTime);usleep(idleTime * 1000);} }int main(int argc, char **argv) {pthread_t t[NUM_THREADS];int ret;std::cout please input cpu utilization rate std::endl;std::cin cpuinfo;for(int i 0; i NUM_THREADS; i) {ret pthread_create(t[i], NULL, CPUCost, NULL);if(ret)std::cout XXXX create err std::endl;std::coutpthread_create i istd::endl;}pthread_exit(NULL);return 0; }上文代码中NUM_THREADS变量的含义是cpu有几个核该变量修改为cpu的核数INTERVAL值默认为100无需修改cpuinfo全局变量用于保存应用程序占用的cpu占有率GetTickCount函数的作用是获取毫秒时间CPUCost函数是线程函数核心逻辑 /** within INTERVAL ms, INTERVAL busyTime idleTime,* spend busyTime ms to let cpu busy,* spend idleTime ms top let cpu idle*/while (true) {startTime GetTickCount(); //获取一个开始时间单位为ms//busyTime和idleTime的总和为100ms即在100ms的时间间隔内程序运行时间为//busyTime程序空闲时间为idleTime通过这样的方式来控制cpu占用率如下的是循环是控制程序运行busyTime时间while((GetTickCount() - startTime) busyTime); //usleep控制程序睡眠idleTime时间让出cpuusleep(idleTime * 1000);} 注意事项 由于我的环境cpu有8个核若指定cpu占有率的为70%则每个核的cpu占有率为70%总的cpu占有率为70%所有的cpu核占有率综合为560%左右70%*8。 运行结果如下所示 可以看到cpu各个核的cpu占有率均在70%以上综合的cpu占有率也是79%各个核的cpu占有率总计为520.9基本与预期相符达到预期目的。 cpu占有率动态变化程序按照正弦函数规律控制cpu占有率 代码如下所示 #include iostream #include pthread.h #include time.h #include math.h #include unistd.h #include math.husing namespace std;#define PI acos(-1) #define DISCRETEVALUE 100 typedef long long int int64; const int NUM_THREADS 8; //CPU core nums int INTERVAL 100; int cpuinfo 70; //CPU utilization rate// time unit is ms int64 GetTickCount() {timespec now;int64 sec, nsec;clock_gettime(CLOCK_MONOTONIC, now);sec now.tv_sec;nsec now.tv_nsec;return sec * 1000 nsec / 1000000; }void* CPUCost(void *args) { // int busyTime INTERVAL * cpuinfo / 100; // int idleTime INTERVAL - busyTime; // int64 startTime 0;int busyTime 50;int idleTime 50;int64 startTime 0;//每次递增间隔float value 2*PI/DISCRETEVALUE;int index 0;coutvalue value PI sin(PI)endl;std::cout XXXX CPUCost std::endl;std::cout XXXX cpuinfo cpuinfo std::endl;/** within INTERVAL ms, INTERVAL busyTime idleTime,* spend busyTime ms to let cpu busy,* spend idleTime ms top let cpu idle*/while (true) {startTime GetTickCount();while((GetTickCount() - startTime) busyTime);usleep(idleTime * 1000);//添加正弦曲线if(index DISCRETEVALUE)index 0;busyTime 50 sin(index*value)*50;idleTime 100 - busyTime;coutbusyTime busyTime idleTime idleTime index*value index*value sin(index*value)*50 sin(index*value)*50endl;index;} }int main(int argc, char **argv) {pthread_t t[NUM_THREADS];int ret;std::cout please input cpu utilization rate std::endl;std::cin cpuinfo;for(int i 0; i NUM_THREADS; i) {ret pthread_create(t[i], NULL, CPUCost, NULL);if(ret)std::cout XXXX create err std::endl;std::coutpthread_create i istd::endl;}pthread_exit(NULL);return 0; }结果显示 完美实现cpu占有率动态控制。 总结 核心思想是在100ms内动态的分配应用程序运行时间和空闲时间的比例从而实现达到控制cpu占有率的目的。
http://www.w-s-a.com/news/860161/

相关文章:

  • 维修网站怎么做跨境电商发展现状如何
  • 手机网站设计公司皆选亿企邦桐乡市建设局官方网站
  • 企业培训 电子商务网站建设 图片山东省住房和城乡建设厅网站主页
  • 做酒招代理的网站赣icp南昌网站建设
  • 怎样做网站內链大连市建设工程信息网官网
  • 网站软件免费下载安装泰安网站建设收费标准
  • 部署iis网站校园网站设计毕业设计
  • 网站快慢由什么决定塘沽手机网站建设
  • 苏州那家公司做网站比较好装修队做网站
  • 外贸网站推广中山网站流量团队
  • 网站前端设计培训做一份网站的步zou
  • 网站备案拍照茶叶网页设计素材
  • wordpress 手机商城模板关键词优化软件有哪些
  • 网站301做排名python做的网站如何部署
  • 昆山做企业网站工信部网站 备案
  • 做英文的小说网站有哪些网站做qq登录
  • 湖州建设局招投标网站深圳广告公司集中在哪里
  • 重庆主城推广网站建设商城网站建设预算
  • 宁波品牌网站推广优化公司开发公司工程部工作总结
  • 长沙建站模板微信网站建设方案
  • 不让网站在手机怎么做门户网站 模板之家
  • 网站建设及推广图片wordpress文章摘要调用
  • 手机版网站案例全国信息企业公示系统
  • 模仿别人网站建设银行广州招聘网站
  • 沧州网站建设沧州内页优化
  • 代加工网站有哪些专门做网站关键词排名
  • 郑州做景区网站建设公司软件开发者模式怎么打开
  • 长沙企业网站建设哪家好做app一般多少钱
  • 南宁一站网网络技术有限公司网站开发技术应用领域
  • 公司网站建设方案ppt专业构建网站的公司