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);

运行结果

相关文章
|
1月前
|
NoSQL Linux Redis
linux安装单机版redis详细步骤,及python连接redis案例
这篇文章提供了在Linux系统中安装单机版Redis的详细步骤,并展示了如何配置Redis为systemctl启动,以及使用Python连接Redis进行数据操作的案例。
45 2
|
1月前
|
人工智能 监控 Shell
常用的 55 个 Linux Shell 脚本(包括基础案例、文件操作、实用工具、图形化、sed、gawk)
这篇文章提供了55个常用的Linux Shell脚本实例,涵盖基础案例、文件操作、实用工具、图形化界面及sed、gawk的使用。
87 2
|
1月前
|
Unix Linux 网络安全
python中连接linux好用的模块paramiko(附带案例)
该文章详细介绍了如何使用Python的Paramiko模块来连接Linux服务器,包括安装配置及通过密码或密钥进行身份验证的示例。
32 1
|
1月前
|
监控 Linux Shell
30 个实用的 Linux 命令贴与技巧,提升你的效率(附实战案例)
本文介绍了30个实用的Linux命令及其应用场景,帮助你提升命令行操作效率。涵盖返回目录、重新执行命令、查看磁盘使用情况、查找文件、进程管理、网络状态监控、定时任务设置等功能,适合各水平的Linux用户学习和参考。
|
17天前
|
NoSQL Linux 程序员
进程管理与运行分析
进程管理与运行分析
16 0
|
18天前
|
存储 Linux
服务器数据恢复—Linux操作系统网站服务器数据恢复案例
服务器数据恢复环境: 一台linux操作系统网站服务器,该服务器上部署了几十个网站,使用一块SATA硬盘。 服务器故障&原因: 服务器在工作过程中突然宕机。管理员尝试重新启动服务器失败,于是将服务器上的硬盘拆下检测,发现很多坏扇区。联系当地的一家数据恢复公司处理,但是没有成功。
|
1月前
|
存储 数据挖掘 Linux
服务器数据恢复—Linux操作系统网站服务器数据恢复案例
服务器数据恢复环境: 一台linux操作系统服务器上跑了几十个网站,服务器上只有一块SATA硬盘。 服务器故障: 服务器突然宕机,尝试再次启动失败。将硬盘拆下检测,发现存在坏扇区
|
1月前
|
Shell Linux Python
python执行linux系统命令的几种方法(python3经典编程案例)
文章介绍了多种使用Python执行Linux系统命令的方法,包括使用os模块的不同函数以及subprocess模块来调用shell命令并处理其输出。
19 0
|
1月前
|
并行计算 API 调度
探索Python中的并发编程:线程与进程的对比分析
【9月更文挑战第21天】本文深入探讨了Python中并发编程的核心概念,通过直观的代码示例和清晰的逻辑推理,引导读者理解线程与进程在解决并发问题时的不同应用场景。我们将从基础理论出发,逐步过渡到实际案例分析,旨在揭示Python并发模型的内在机制,并比较它们在执行效率、资源占用和适用场景方面的差异。文章不仅适合初学者构建并发编程的基础认识,同时也为有经验的开发者提供深度思考的视角。
|
1月前
|
存储 传感器 Linux
STM32微控制器为何不适合运行Linux系统的分析
总的来说,虽然技术上可能存在某些特殊情况下将Linux移植到高端STM32微控制器上的可能性,但从资源、性能、成本和应用场景等多个方面考虑,STM32微控制器不适合运行Linux系统。对于需要运行Linux的应用,更适合选择ARM Cortex-A系列处理器的开发平台。
163 0