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

运行结果

相关文章
|
Web App开发 Linux 程序员
获取和理解Linux进程以及其PID的基础知识。
总的来说,理解Linux进程及其PID需要我们明白,进程就如同汽车,负责执行任务,而PID则是独特的车牌号,为我们提供了管理的便利。知道这个,我们就可以更好地理解和操作Linux系统,甚至通过对进程的有效管理,让系统运行得更加顺畅。
512 16
|
监控 Shell Linux
Linux进程控制(详细讲解)
进程等待是系统通过调用特定的接口(如waitwaitpid)来实现的。来进行对子进程状态检测与回收的功能。
390 0
|
存储 负载均衡 算法
Linux2.6内核进程调度队列
本篇文章是Linux进程系列中的最后一篇文章,本来是想放在上一篇文章的结尾的,但是想了想还是单独写一篇文章吧,虽然说这部分内容是比较难的,所有一般来说是简单的提及带过的,但是为了让大家对进程有更深的理解与认识,还是看了一些别人的文章,然后学习了学习,然后对此做了总结,尽可能详细的介绍明白。最后推荐一篇文章Linux的进程优先级 NI 和 PR - 简书。
441 0
|
存储 Linux Shell
Linux进程概念-详细版(二)
在Linux进程概念-详细版(一)中我们解释了什么是进程,以及进程的各种状态,已经对进程有了一定的认识,那么这篇文章将会继续补全上篇文章剩余没有说到的,进程优先级,环境变量,程序地址空间,进程地址空间,以及调度队列。
325 0
|
Linux 调度 C语言
Linux进程概念-详细版(一)
子进程与父进程代码共享,其子进程直接用父进程的代码,其自己本身无代码,所以子进程无法改动代码,平时所说的修改是修改的数据。为什么要创建子进程:为了让其父子进程执行不同的代码块。子进程的数据相对于父进程是会进行写时拷贝(COW)。
406 0
|
Linux Shell 数据安全/隐私保护
|
Linux 应用服务中间件 Shell
二、Linux文本处理与文件操作核心命令
熟悉了Linux的基本“行走”后,就该拿起真正的“工具”干活了。用grep这个“放大镜”在文件里搜索内容,用find这个“探测器”在系统中寻找文件,再用tar把东西打包带走。最关键的是要学会使用管道符|,它像一条流水线,能把这些命令串联起来,让简单工具组合出强大的功能,比如 ps -ef | grep 'nginx' 就能快速找出nginx进程。
1113 1
二、Linux文本处理与文件操作核心命令
linux命令—stat
`stat` 是 Linux 系统中用于查看文件或文件系统详细状态信息的命令。相比 `ls -l`,它提供更全面的信息,包括文件大小、权限、所有者、时间戳(最后访问、修改、状态变更时间)、inode 号、设备信息等。其常用选项包括 `-f` 查看文件系统状态、`-t` 以简洁格式输出、`-L` 跟踪符号链接,以及 `-c` 或 `--format` 自定义输出格式。通过这些选项,用户可以灵活获取所需信息,适用于系统调试、权限检查、磁盘管理等场景。
813 137
|
安全 Ubuntu Unix
一、初识 Linux 与基本命令
玩转Linux命令行,就像探索一座新城市。首先要熟悉它的“地图”,也就是/根目录下/etc(放配置)、/home(住家)这些核心区域。然后掌握几个“生存口令”:用ls看周围,cd去别处,mkdir建新房,cp/mv搬东西,再用cat或tail看文件内容。最后,别忘了随时按Tab键,它能帮你自动补全命令和路径,是提高效率的第一神器。
1771 58
|
11月前
|
存储 安全 Linux
Linux卡在emergency mode怎么办?xfs_repair 命令轻松解决
Linux虚拟机遇紧急模式?别慌!多因磁盘挂载失败。本文教你通过日志定位问题,用`xfs_repair`等工具修复文件系统,三步快速恢复。掌握查日志、修磁盘、验重启,轻松应对紧急模式,保障系统稳定运行。
1715 2