task_nice
task_nice
函数功能:获取某个进程的nice值,其中nice值为进程的优先级,与静态优先级有关(nice=static_prio-120)。
- nice的取值范围:-20 ~ 19
内核源码
- 根据内核的注释可以知道:
task_nice
返回一个整形,就是进程(任务)的优先级。前面几次的笔记也说过,nice
的值越小则进程优先级越高。
使用示例
#include <linux/sched.h>
#include <linux/module.h>
#include <linux/pid.h>
#include <linux/kthread.h>
int MyThreadFunc(void* argc) {
printk("Prompt:kernel thread function.\n");
printk("Prompt:kernel thread function static_prio : %d.\n", current->static_prio);
printk("Prompt:kernel thread function PID : %d.\n", current->pid);
printk("Prompt:kernel thread function nice : %d.\n", task_nice(current));
return 0;
}
static int __init TaskNiceFuncInit(void) {
struct task_struct* new_task;
int priority;
new_task = kthread_create_on_node(MyThreadFunc, NULL, -1, "task_nice.c");
wake_up_process(new_task);
priority = task_nice(new_task);
printk("Prompt:child thread PID : %d.\n", new_task->pid);
printk("Prompt:child thread static_prio : %d.\n", priority);
printk("Prompt:current thread PID : %d.\n", current->pid);
printk("Prompt:current thread static_prio : %d.\n", current->static_prio);
return 0;
}
static void __exit TaskNiceFuncExit(void) {
printk("Prompt:exit kernel.\n");
}
module_init(TaskNiceFuncInit);
module_exit(TaskNiceFuncExit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("lenn louis");