2012-04-11

Linux Thread Scheduling Policy and Priority

Introduction

Linux 2.6.x is pre-emptive kernel. It has 100 level priorities: 0 to 99. 0 is the lowest priority and 99 is the highest.

There're three scheduling policies:

  • SCHED_OTHER
This is default policy. It is a time-slice based policy. All threads of this policy have the same priority 0. The system manage their priorities automatically. User can't set priority of this kind of thread.
  • SCHED_FIFO
This is a real-time scheduling. The high priority thread always pre-empt the lower priority threads. The threads with the same priority run in a FIFO mode, which means the thread has to give up the CPU otherwise other threads can't run.
  • SCHED_RR
This is the same as SCHED_FIFO with difference about the scheduling of the threads with the same priority. The threads with the same priority are scheduled in a round-robin mode.

Linux pthread library bug

There's a bug in pthread library regarding setting the schedule policy and priority. See below picture. The right side is the correct one. The setting of the policy and priority MUST come after 'pthread_create()'.

Check the thread policy and priority

With 'chrt' command, the thread schedule policy and priority can be checked from command line.

The threads' id of a process can be found with command: ls /proc/<process id>/task

The first thread in the list is the main thread.

To check a thread's policy and priority:

   chrt -p <thread_id>