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

简介: 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);

运行结果

相关文章
|
11月前
|
安全 Linux iOS开发
Binary Ninja 5.1.8104 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
Binary Ninja 5.1.8104 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
865 53
Binary Ninja 5.1.8104 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
|
11月前
|
Linux API iOS开发
Binary Ninja 4.2.6455 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
Binary Ninja 4.2.6455 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
779 14
Binary Ninja 4.2.6455 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
|
12月前
|
数据管理 Linux iOS开发
Splunk Enterprise 9.4.5 (macOS, Linux, Windows) - 机器数据管理和分析
Splunk Enterprise 9.4.5 (macOS, Linux, Windows) - 机器数据管理和分析
406 0
|
Unix Linux iOS开发
Splunk Enterprise 10.0.0 (macOS, Linux, Windows) - 搜索、分析和可视化,数据全面洞察平台
Splunk Enterprise 10.0.0 (macOS, Linux, Windows) - 搜索、分析和可视化,数据全面洞察平台
362 0
|
运维 监控 中间件
Linux运维笔记 - 如何使用WGCLOUD监控交换机的流量
WGCLOUD是一款开源免费的通用主机监控工具,安装使用都非常简单,它可以监控主机、服务器的cpu、内存、磁盘、流量等数据,也可以监控数据库、中间件、网络设备
|
监控 Linux
Linux基础:文件和目录类命令分析。
总的来说,这些基础命令,像是Linux中藏匿的小矮人,每一次我们使用他们,他们就把我们的指令准确的传递给Linux,让我们的指令变为现实。所以,现在就开始你的Linux之旅,挥动你的命令之剑,探索这个充满神秘而又奇妙的世界吧!
328 19
|
缓存 网络协议 Linux
PCIe 以太网芯片 RTL8125B 的 spec 和 Linux driver 分析备忘
本文详细介绍了 Realtek RTL8125B PCIe 以太网芯片的规格以及在 Linux 中的驱动安装和配置方法。通过深入分析驱动源码,可以更好地理解其工作原理和优化方法。在实际应用中,合理配置和优化驱动程序可以显著提升网络性能和稳定性。希望本文能帮助您更好地使用和管理 RTL8125B,以满足各种网络应用需求。
2255 33
|
弹性计算 运维 监控
基于进程热点分析与系统资源优化的智能运维实践
智能服务器管理平台提供直观的可视化界面,助力高效操作系统管理。核心功能包括运维监控、智能助手和扩展插件管理,支持系统健康监控、故障诊断等,确保集群稳定运行。首次使用需激活服务并安装管控组件。平台还提供进程热点追踪、性能观测与优化建议,帮助开发人员快速识别和解决性能瓶颈。定期分析和多维度监控可提前预警潜在问题,保障系统长期稳定运行。
705 17
|
数据管理 Linux iOS开发
Splunk Enterprise 9.4.1 (macOS, Linux, Windows) 发布 - 机器数据管理和分析
Splunk Enterprise 9.4.1 (macOS, Linux, Windows) 发布 - 机器数据管理和分析
441 0
Splunk Enterprise 9.4.1 (macOS, Linux, Windows) 发布 - 机器数据管理和分析
|
存储 运维 监控
Linux--深入理与解linux文件系统与日志文件分析
深入理解 Linux 文件系统和日志文件分析,对于系统管理员和运维工程师来说至关重要。文件系统管理涉及到文件的组织、存储和检索,而日志文件则记录了系统和应用的运行状态,是排查故障和维护系统的重要依据。通过掌握文件系统和日志文件的管理和分析技能,可以有效提升系统的稳定性和安全性。
496 7