How to change scheduling algorithm and priority used by a process?

Solution Verified - Updated -

Environment

  • Red Hat Enterprise Linux 7

Issue

  • Unable to set the priority of the process as default scheduling algorithm taken by process is SCHED_OTHER.

Resolution

  • RHEL 7 introduces the tuna tool to assist with simplifying the tasks of isolating CPU cores for specific applications, assigning those CPU cores, pinning IRQs, and other formerly complicated processes .

  • Using the tuna command, the process were allocated round robin algorithm, and the same command can be used to change process priority
    too.

Root Cause

  • The Linux scheduler is modular, enabling different algorithms to schedule different types of processes.This modularity is called scheduler
    classes. Scheduler classes enable different,pluggable algorithms to coexist, scheduling their own types of processes. The Linux Scheduling
    Algorithm class has a priority.The base scheduler code, which is defined in kernel/sched.c , iterates over each scheduler class in order of
    priority.The highest priority scheduler class that has a runnable process wins, selecting who runs next.

  • The Completely Fair Scheduler (CFS) is the registered scheduler class for normal processes, called SCHED_NORMAL in Linux (and
    SCHED_OTHER in POSIX). CFS is defined in kernel/sched_fair.c .

  • Real-time policies are managed not by the Completely Fair Scheduler, but by a special real-time scheduler, defined in kernel/sched_rt.c .

  • SCHED_RR can run only until it exhausts a predetermined time slice. When a SCHED_RR task exhausts its time-slice, any other real-time
    processes at its priority are scheduled round-robin.

Diagnostic Steps

  • Below is one sample program. When started , it will automatically take sched_fifo scheduler as its defined in the program.
# cat setscheduler.c
 #include <sched.h>
 main ()
 {
   struct sched_param param;
   int pid_num = 0;

   param.sched_priority = 99;
   sched_setscheduler(pid_num, SCHED_FIFO, &param);
   while (1) 
      ;
 }
  • To check the scheduler used by this program use chrt -p pid command.

  • Below example will guide how to change the scheduler & priority of a process using TUNA:

# tuna --threads 7861 --priority=RR:40    <<-- sets a policy of RR (round-robin) and a priority of 40 for PID 7861. 

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments