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

Linux源码阅读笔记10-进程NICE案例分析2

set_user_nice

set_user_nice函数功能:设置某一进程的NICE值,其NICE值的计算是根据进程的静态优先级(task_struct->static_prio),直接通过set_user_nice函数更改进程的静态优先级。

内核源码

void set_user_nice(struct task_struct *p, long nice)
{bool queued, running;int old_prio;struct rq_flags rf;struct rq *rq;if (task_nice(p) == nice || nice < MIN_NICE || nice > MAX_NICE)return;/** We have to be careful, if called from sys_setpriority(),* the task might be in the middle of scheduling on another CPU.*/rq = task_rq_lock(p, &rf);update_rq_clock(rq);/** The RT priorities are set via sched_setscheduler(), but we still* allow the 'normal' nice value to be set - but as expected* it wont have any effect on scheduling until the task is* SCHED_DEADLINE, SCHED_FIFO or SCHED_RR:*/if (task_has_dl_policy(p) || task_has_rt_policy(p)) {p->static_prio = NICE_TO_PRIO(nice);goto out_unlock;}queued = task_on_rq_queued(p);running = task_current(rq, p);if (queued)dequeue_task(rq, p, DEQUEUE_SAVE | DEQUEUE_NOCLOCK);if (running)put_prev_task(rq, p);p->static_prio = NICE_TO_PRIO(nice);set_load_weight(p, true);old_prio = p->prio;p->prio = effective_prio(p);if (queued)enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK);if (running)set_next_task(rq, p);/** If the task increased its priority or is running and* lowered its priority, then reschedule its CPU:*/p->sched_class->prio_changed(rq, p, old_prio);out_unlock:task_rq_unlock(rq, p, &rf);
}
EXPORT_SYMBOL(set_user_nice);

使用示例

#include <linux/module.h>
#include <linux/pid.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/kthread.h>static int MyThreadFunc(void* argc) {printk("Prompt:kernel thread PID : %d.\n", current->pid);printk("Prompt:kernel thread static_prio : %d.\n", current->static_prio);printk("Prompt:kernel thread nice : %d.\n", task_nice(current));return 0;
}static int __init SetUserNiceInit(void) {struct task_struct* new_task = NULL;new_task = kthread_create_on_node(MyThreadFunc, NULL, -1, "setusernice.c");printk("Prompt:new thread nice : %d.\n", task_nice(new_task));printk("Prompt:new thread static_prio : %d.\n", new_task->static_prio);printk("Prompt:new thread prio : %d.\n", new_task->prio);set_user_nice(new_task, 16);printk("Prompt:new thread nice : %d.\n", task_nice(new_task));printk("Prompt:new thread static_prio : %d.\n", new_task->static_prio);printk("Prompt:new thread prio : %d.\n", new_task->prio);return 0;
}static void __exit SetUserNiceExit(void) {printk("Prompt:exit kernel.\n");
}module_init(SetUserNiceInit);
module_exit(SetUserNiceExit);

运行结果

http://www.lryc.cn/news/389963.html

相关文章:

  • Elasticsearch实战教程: 如何在海量级数据中进行快速搜索
  • Python学习笔记24:进阶篇(十三)常见标准库使用之数据压缩功能模块zlib,gzip,bz2,lzma的学习使用
  • 【笔记】Android Settings 应用设置菜单的界面代码介绍
  • Symfony配置管理深度解析:构建可维护项目的秘诀
  • 视频的宣传片二维码怎么做?扫码播放视频的制作教程
  • 实用的网站
  • Monorepo(单体仓库)与 MultiRepo(多仓库): Monorepo 单体仓库开发策略与实践指南
  • 使用 PyTorch 创建的多步时间序列预测的 Encoder-Decoder 模型
  • 开启IT世界的第一步:高考新生的暑期学习指南
  • 软考系统架构师高效备考方法论
  • 【neo4j图数据库】入门实践篇
  • 【TS】TypeScript 原始数据类型深度解析
  • 怎么样调整分类的阈值
  • java+mysql教师管理系统
  • PDF文档如何统计字数,统计PDF文档字数的方法有哪些?
  • 在Python asyncio中如何识别协程是否被block了
  • Hyper-V虚拟机固定IP地址(手把手教设置)
  • 以 Vue 3 项目为例,多个请求下如何全局封装 Loading 的展示与关闭?其中大有学问!
  • Node.js学习(一)
  • Spring Data JPA使用及实现原理总结
  • 【C语言】extern 关键字
  • Linux--V4L2应用程序开发(二)改变亮度
  • [Gstreamer] 消息处理handler的设置
  • 线性代数笔记
  • 未公开 GeoServer开源服务器wfs远程命令执行漏洞 已复现(CVE-2024-36401)
  • 【WebGIS干货分享】Webgis 面试题-浙江中海达
  • ES 修改索引字段类型
  • 恢复的实现技术-日志和数据转储
  • 全网最全最细的jmeter接口测试教程,建议收藏
  • Raspbian命令行连接WiFi网络