Linux sched

man page

sched

每个线程对应一种调度策略和一个静态优先级
对于SCHED_OTHER, SCHED_IDLE, SCHED_BATCH调度策略的线程,静态优先级固定为0
对于SCHED_FIFO, SCHED_RR调度策略的线程,静态优先级可设置范围是1-99

对于每一个静态优先级,系统维护一个拥有此优先级的可执行线程列表

调度的时候,按照静态优先级从大到小的顺序,从队列中取线程进行调度。
因此,拥有SCHED_FIFO, SCHED_RR调度策略的线程总是会比SCHED_OTHER, SCHED_IDLE, SCHED_BATCH调度策略的线程优先,
并且会抢占(当SCHED_FIFO, SCHED_RR调度策略的线程需要调度时,会抢占SCHED_OTHER, SCHED_IDLE, SCHED_BATCH调度策略的线程)

调度策略只在同一个优先级的线程列表中生效。

SCHED_FIFO, SCHED_RR调度策略的线程优先

还有一种SCHED_DEADLINE调度策略,比SCHED_FIFO, SCHED_RR调度策略更优先。

NICE:
nice只对SCHED_OTHER and SCHED_BATCH有效。
越小越优先。

修改进程nice值
# nice
# renice

修改进程调度策略
# chrt

dynamic priority

SCHED_OTHER: Default Linux time-sharing scheduling
       SCHED_OTHER can be used at only static priority 0 (i.e., threads
       under real-time policies always have priority over SCHED_OTHER
       processes).  SCHED_OTHER is the standard Linux time-sharing
       scheduler that is intended for all threads that do not require
       the special real-time mechanisms.

       The thread to run is chosen from the static priority 0 list based
       on a dynamic priority that is determined only inside this list.
       The dynamic priority is based on the nice value (see below) and
       is increased for each time quantum the thread is ready to run,
       but denied to run by the scheduler.  This ensures fair progress
       among all SCHED_OTHER threads.

       In the Linux kernel source code, the SCHED_OTHER policy is
       actually named SCHED_NORMAL.

SCHED_FIFO and SCHED_RR

SCHED_FIFO and SCHED_RR are so called "real-time" policies. They implement the fixed-priority real-time scheduling specified by the POSIX standard. 
Tasks with these policies preempt every other task, which can thus easily go into starvation (if they don't release the CPU).

The difference between SCHED_FIFO and SCHED_RR is that among tasks with the same priority, SCHED_RR performs a round-robin with a certain timeslice; 
SCHED_FIFO, instead, needs the task to explicitly yield the processor.

SCHED_OTHER is the common round-robin time-sharing scheduling policy that schedules a task for a certain timeslice depending on the other tasks running in the system.

Update: since Linux 3.14, there is an additional policy called SCHED_DEADLINE. 
This policy implements the Constant Bandwidth Server (CBS) algorithm on top of Earliest Deadline First queues. 
Each task under this policy is assigned a deadline, and the earliest-deadline task is executed. 
The best resource describing this algorithm is Deadline scheduling in the Linux kernel.