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

cpp 复制代码
#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 0
    set_nice(-5);       // 设置最高优先级(需 root 权限)
    std::thread t1(worker);
    std::thread t2(reporter);
    t1.join();
    t2.join();
    return 0;
}

b. low_nice.cc

cpp 复制代码
#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 0
    set_nice(10);        // 设置最低优先级
    std::thread t1(worker);
    std::thread t2(reporter);
    t1.join();
    t2.join();
    return 0;
}

c. Makefile

bash 复制代码
CPP = g++
CFLAGS = -std=c++11 -pthread -O2
TARGETS = high_nice low_nice

all: $(TARGETS)

high_nice: high_nice.cc
	$(CPP) $(CFLAGS) $< -o $@

low_nice: low_nice.cc
	$(CPP) $(CFLAGS) $< -o $@

clean:
	rm -f $(TARGETS)

运行:

bash 复制代码
make

# bash1
sudo ./high_nice

# bash2
sudo ./low_nice

3. 结果测试

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

相关推荐
漫步企鹅21 分钟前
【蓝牙】Linux Qt4查看已经配对的蓝牙信息
linux·qt·蓝牙·配对
cui_win31 分钟前
【网络】Linux 内核优化实战 - net.core.flow_limit_table_len
linux·运维·网络
梦在深巷、32 分钟前
MySQL/MariaDB数据库主从复制之基于二进制日志的方式
linux·数据库·mysql·mariadb
风清再凯37 分钟前
自动化工具ansible,以及playbook剧本
运维·自动化·ansible
深圳安锐科技有限公司40 分钟前
深圳安锐科技发布国内首款4G 索力仪!让斜拉桥索力自动化监测更精准高效
运维·安全·自动化·自动化监测·人工监测·桥梁监测·索力监测
猫头虎44 分钟前
猫头虎 AI工具分享:一个网页抓取、结构化数据提取、网页爬取、浏览器自动化操作工具:Hyperbrowser MCP
运维·人工智能·gpt·开源·自动化·文心一言·ai编程
冰橙子id1 小时前
linux系统安全
linux·安全·系统安全
stark张宇1 小时前
VMware 虚拟机装 Linux Centos 7.9 保姆级教程(附资源包)
linux·后端
Johny_Zhao1 小时前
Ubuntu系统安装部署Pandawiki智能知识库
linux·mysql·网络安全·信息安全·云计算·shell·yum源·系统运维·itsm·pandawiki
悲伤小伞2 小时前
linux_git的使用
linux·c语言·c++·git