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

运行结果

相关文章
|
Linux
linux常用命令详细说明以及案例
本文介绍了Linux中几个常用的命令及其用法,包括:`ls`(列出目录内容)、`cd`(切换目录)、`mkdir`(创建目录)、`rm -p`(删除目录及内容)和`mv`(移动或重命名文件/目录)。每个命令都配有详细说明、语法格式、常见选项及实用案例,帮助用户更好地理解和使用这些基础命令。内容源自[linux常用命令详细说明以及案例](https://linux.ciilii.com/show/news-285.html)。
644 159
|
Linux
linux命令详细说明以及案例
本文介绍了常用的 Linux 命令及其详细说明和示例,包括:`ls`(列出目录内容)、`cd`(更改目录)、`rm` 和 `mv`(删除与移动文件)、`grep`(搜索文本)、`cat`(显示文件内容)以及 `chmod`(更改文件权限)。每个命令均配有功能描述、选项说明及实际案例,帮助用户更好地掌握 Linux 命令的使用方法。
985 157
|
Ubuntu Linux Python
Tkinter错误笔记(一):tkinter.Button在linux下出现乱码
在Linux系统中,使用Tkinter库时可能会遇到中文显示乱码的问题,这通常是由于字体支持问题导致的,可以通过更换支持中文的字体来解决。
1471 0
Tkinter错误笔记(一):tkinter.Button在linux下出现乱码
|
运维 监控 Java
Linux常用命令行大全:14个核心指令详解+实战案例
在服务器管理与开发运维领域,Linux 指令是构建技术能力体系的基石。无论是日常的系统监控、文件操作,还是复杂的服务部署与故障排查,熟练掌握指令的使用逻辑都是提升工作效率的核心前提。然而,对于初学者而言,Linux 指令体系往往呈现出“参数繁多易混淆”“组合使用门槛高”“实际场景适配难”等痛点——例如 ls 命令的 -l 与 -a 参数如何搭配查看隐藏文件详情,grep 与管道符结合时如何精准过滤日志内容,这些问题常常成为技术进阶的阻碍。
|
数据挖掘 Linux 数据库
服务器数据恢复—Linux系统服务器数据恢复案例
服务器数据恢复环境: linux操作系统服务器中有一组由4块SAS接口硬盘组建的raid5阵列。 服务器故障: 服务器工作过程中突然崩溃。管理员将服务器操作系统进行了重装。 用户方需要恢复服务器中的数据库、办公文档、代码文件等。
|
运维 监控 中间件
Linux运维笔记 - 如何使用WGCLOUD监控交换机的流量
WGCLOUD是一款开源免费的通用主机监控工具,安装使用都非常简单,它可以监控主机、服务器的cpu、内存、磁盘、流量等数据,也可以监控数据库、中间件、网络设备
|
Linux API 开发工具
FFmpeg开发笔记(五十九)Linux编译ijkplayer的Android平台so库
ijkplayer是由B站研发的移动端播放器,基于FFmpeg 3.4,支持Android和iOS。其源码托管于GitHub,截至2024年9月15日,获得了3.24万星标和0.81万分支,尽管已停止更新6年。本文档介绍了如何在Linux环境下编译ijkplayer的so库,以便在较新的开发环境中使用。首先需安装编译工具并调整/tmp分区大小,接着下载并安装Android SDK和NDK,最后下载ijkplayer源码并编译。详细步骤包括环境准备、工具安装及库编译等。更多FFmpeg开发知识可参考相关书籍。
1257 0
FFmpeg开发笔记(五十九)Linux编译ijkplayer的Android平台so库
|
Linux 数据库 Perl
【YashanDB 知识库】如何避免 yasdb 进程被 Linux OOM Killer 杀掉
本文来自YashanDB官网,探讨Linux系统中OOM Killer对数据库服务器的影响及解决方法。当内存接近耗尽时,OOM Killer会杀死占用最多内存的进程,这可能导致数据库主进程被误杀。为避免此问题,可采取两种方法:一是在OS层面关闭OOM Killer,通过修改`/etc/sysctl.conf`文件并重启生效;二是豁免数据库进程,由数据库实例用户借助`sudo`权限调整`oom_score_adj`值。这些措施有助于保护数据库进程免受系统内存管理机制的影响。
|
Linux Shell
Linux 进程前台后台切换与作业控制
进程前台/后台切换及作业控制简介: 在 Shell 中,启动的程序默认为前台进程,会占用终端直到执行完毕。例如,执行 `./shella.sh` 时,终端会被占用。为避免不便,可将命令放到后台运行,如 `./shella.sh &`,此时终端命令行立即返回,可继续输入其他命令。 常用作业控制命令: - `fg %1`:将后台作业切换到前台。 - `Ctrl + Z`:暂停前台作业并放到后台。 - `bg %1`:让暂停的后台作业继续执行。 - `kill %1`:终止后台作业。 优先级调整:
1989 5