转载自:http://honglus.blogspot.com/2011/03/understanding-cpu-scheduling-priority.html
Understanding Linux CPU scheduling priority
Scheduling priority depends on scheduling class.scheduling classes
- SCHED_FIFO: A First-In, First-Out real-time process
- SCHED_RR: A Round Robin real-time process
- SCHED_NORMAL: A conventional, time-shared process
Most processes are SCHED_NORMAL
How to find out the scheduling class of a process.
# ps command with “class” flag
# TS SCHED_OTHER (SCHED_NORMAL)
# FF SCHED_FIFO
# RR SCHED_RR
$ ps -e -o class,cmd | grep sshd
TS /usr/sbin/sshd#chrt command
$ chrt -p 1836
pid 1836's current scheduling policy: SCHED_OTHERpid 1836's current scheduling priority: 0
Scheduling priorities
- Real-time process (SCHED_FIFO/SCHED_RR) real-time priority , ranging from 1 (lowest priority) to 99 (higest priority).
- Conventional process static priority(SCHED_NORMAL ), ranging from 100 (highest priority) to 139 (lowest priority).
Nice value and static priority
Conventional process's static priority = (120 + Nice value)
So user can use nice/renice command to change nice value in order to change conventional process's priority.
By default, conventional process starts with nice value of 0 which equals static priority 120
Checking Real-time/Conventional process priority.
$ ps -e -o class ,rtprio,pri,nice,cmd
Watchdog is a real time process (CLASS=SCHED_FIFO), whose real time priority is 99 (I think the PRI column is irrelevant for it)CLS RTPRIO PRI NI CMDTS - 21 0 init [3]FF 99 139 - [watchdog/0]
init is a conventional process (CLASS=SCHED_OTHER), whose nice is 0 and dynamic priority is 121 (100+21)(I think the RTPRIO column is irrelevant for it )
Why init's priority is 121 not 120? Please noted I used the term: dynamic priority not static priority.
dynamic priority = max (100, min ( static priority - bonus + 5, 139))
bonus is ranging from 0 to 10, which is set by scheduler depends on the past history of the process; more precisely, it is related to the average sleep time of the process.
Changing Real-time/Conventional process priority.
#Real-time process
$chrt 80 ps -e -o class,rtprio,pri,nice,cmd
..FF 80 120 - ps -e -o class,rtprio,pri,nice,cmd
# Conventional process
$nice -n 10 ps -e -o class,rtprio,pri,nice,cmd
...TS - 12 10 ps -e -o class,rtprio,pri,nice,cmd