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

建英语网站好做网站一直不知道做什么网站

建英语网站好,做网站一直不知道做什么网站,奢侈品网站建设方案,百度推广产品1. Linux 进程中的 nice 值 nice 值通过调整进程的优先级,间接控制 CPU 时间片的分配权重,从而实现对进程调度行为的精细控制。它是 Linux 系统中平衡实时任务与后台任务资源占用的核心工具之一。 优先级调整 核心机制:nice 值(…

1. Linux 进程中的 nice 值

nice 值通过调整进程的优先级,间接控制 CPU 时间片的分配权重,从而实现对进程调度行为的精细控制。它是 Linux 系统中平衡实时任务与后台任务资源占用的核心工具之一。

优先级调整
核心机制:nice 值(范围 -20 到 19)直接控制进程的调度优先级。
低 nice 值(如 -20):表示高优先级,进程更容易被调度器选中,获得更多 CPU 时间片。
高 nice 值(如 19):表示低优先级,进程被调度的频率降低,CPU 时间片分配减少。

记忆:一个进程的 nice 值越高,说明对别的进程越 nice,别的进程运行得越好越快。

2. 代码

设计说明

  • 独立程序:高优先级和低优先级程序完全分离,便于独立运行和调试。
  • CPU 绑定:两个程序均绑定到 CPU 0,消除多核干扰。
  • 时间片观测:每秒打印计数器增量,反映时间片分配比例。

a. high_nice.cc

#include <iostream>
#include <unistd.h>
#include <sys/resource.h>
#include <sched.h>
#include <atomic>
#include <thread>// 绑定到指定 CPU 核
void bind_cpu(int cpu_id) {cpu_set_t mask;CPU_ZERO(&mask);CPU_SET(cpu_id, &mask);if (sched_setaffinity(0, sizeof(mask), &mask) < 0) {perror("sched_setaffinity");exit(1);}
}// 设置进程的 nice 值
void set_nice(int niceness) {if (setpriority(PRIO_PROCESS, 0, niceness) < 0) {perror("setpriority");exit(1);}
}std::atomic<long> counter(0);  // 全局计数器void worker() {while (true) {counter++;  // 高速递增计数器}
}void reporter() {long last = 0;while (true) {std::this_thread::sleep_for(std::chrono::seconds(1));long current = counter.load();std::cout << "[HIGH] Increment: " << current - last << std::endl;last = current;}
}int main() {bind_cpu(0);         // 绑定到 CPU 0set_nice(-5);       // 设置最高优先级(需 root 权限)std::thread t1(worker);std::thread t2(reporter);t1.join();t2.join();return 0;
}

b. low_nice.cc

#include <iostream>
#include <unistd.h>
#include <sys/resource.h>
#include <sched.h>
#include <atomic>
#include <thread>// 绑定到指定 CPU 核
void bind_cpu(int cpu_id) {cpu_set_t mask;CPU_ZERO(&mask);CPU_SET(cpu_id, &mask);if (sched_setaffinity(0, sizeof(mask), &mask) < 0) {perror("sched_setaffinity");exit(1);}
}// 设置进程的 nice 值
void set_nice(int niceness) {if (setpriority(PRIO_PROCESS, 0, niceness) < 0) {perror("setpriority");exit(1);}
}std::atomic<long> counter(0);  // 全局计数器void worker() {while (true) {counter++;  // 高速递增计数器}
}void reporter() {long last = 0;while (true) {std::this_thread::sleep_for(std::chrono::seconds(1));long current = counter.load();std::cout << "[LOW]  Increment: " << current - last << std::endl;last = current;}
}int main() {bind_cpu(0);         // 绑定到 CPU 0set_nice(10);        // 设置最低优先级std::thread t1(worker);std::thread t2(reporter);t1.join();t2.join();return 0;
}

c. Makefile

CPP = g++
CFLAGS = -std=c++11 -pthread -O2
TARGETS = high_nice low_niceall: $(TARGETS)high_nice: high_nice.cc$(CPP) $(CFLAGS) $< -o $@low_nice: low_nice.cc$(CPP) $(CFLAGS) $< -o $@clean:rm -f $(TARGETS)

运行:

make# bash1
sudo ./high_nice# bash2
sudo ./low_nice

3. 结果测试

nice 值为 -5 的占据 CPU 的 96%, nice 值为10 的占据 CPU 的3.7%。

在这里插入图片描述

http://www.dtcms.com/wzjs/828225.html

相关文章:

  • 创意网站 模板国外做鞋子的网站吗
  • 智库建设网站新公司网站设计注意事项
  • 单位网站建设制作wordpress 获取当前文章栏目链接
  • 中国建设网站官方网站莱州网络推广公司
  • 网站建设的主要特征河西做网站的公司
  • 北京企业做网站费用扬中零壹网站建设
  • php网站建设素材网站开发商外包
  • 淄博桓台学校网站建设方案如何搭建一个购物网站
  • 个人网站主页模板网站标题的设置方法
  • 湖南省百川电力建设有限公司网站一站式服务宣传语
  • 网站备案 地址一达通外贸综合服务平台
  • 想建立什么网站吗地方网 wordpress
  • 亚洲杯最新消息网站后端性能优化措施
  • 怎样让百度收录网站seo点击工具帮你火21星热情
  • 网站如何判断做的好不好网站集约化建设存在的困难
  • 网站开发程序员需要会的技能wordpress+悬浮按钮
  • 织梦网站面包屑导航怎么做电脑制作网页用什么软件
  • 幸运28网站代理怎么做做销售网站的公司哪家最好的
  • 建设网站的十个步骤小型网站建设
  • h5网站制作平台有哪些seo裤子的关键词首页排名有哪些
  • 视频网站代言人wordpress添加到主屏幕
  • 佛山做网站哪家公司最好网络广告策划的流程顺序为
  • 免费网站建设知识个人承接网站开发
  • 商丘网站优化mvc个人网站怎么做
  • 徐汇制作网站哪家好一流的营销型网站建设
  • 在国外做盗版网站吗wordpress 工具栏遮挡
  • 网站个人信息页面布局山东银汇建设集团网站
  • 洛阳做网站公司在哪nginx即代理又做网站
  • 学网站建设 去那里WordPress实现
  • 南昌的网站推广公司做网站ui