# SCHED(7) - man - phpMan

[SCHED(7)](https://www.chedong.com/phpMan.php/man/SCHED/7/markdown)                              Linux Programmer's Manual                             [SCHED(7)](https://www.chedong.com/phpMan.php/man/SCHED/7/markdown)



## NAME
       sched - overview of CPU scheduling

## DESCRIPTION
       Since  Linux  2.6.23, the default scheduler is CFS, the "Completely Fair Scheduler".  The CFS
       scheduler replaced the earlier "[O(1)](https://www.chedong.com/phpMan.php/man/O/1/markdown)" scheduler.

### API summary
       Linux provides the following system calls for controlling the CPU scheduling  behavior,  pol‐
       icy, and priority of processes (or, more precisely, threads).

       [**nice**(2)](https://www.chedong.com/phpMan.php/man/nice/2/markdown)
              Set a new nice value for the calling thread, and return the new nice value.

       [**getpriority**(2)](https://www.chedong.com/phpMan.php/man/getpriority/2/markdown)
              Return  the  nice value of a thread, a process group, or the set of threads owned by a
              specified user.

       [**setpriority**(2)](https://www.chedong.com/phpMan.php/man/setpriority/2/markdown)
              Set the nice value of a thread, a process group, or the set  of  threads  owned  by  a
              specified user.

       **sched**___**[setscheduler**(2)](https://www.chedong.com/phpMan.php/man/setscheduler/2/markdown)
              Set the scheduling policy and parameters of a specified thread.

       **sched**___**[getscheduler**(2)](https://www.chedong.com/phpMan.php/man/getscheduler/2/markdown)
              Return the scheduling policy of a specified thread.

       **sched**___**[setparam**(2)](https://www.chedong.com/phpMan.php/man/setparam/2/markdown)
              Set the scheduling parameters of a specified thread.

       **sched**___**[getparam**(2)](https://www.chedong.com/phpMan.php/man/getparam/2/markdown)
              Fetch the scheduling parameters of a specified thread.

       **sched**___**get**___**priority**___**[max**(2)](https://www.chedong.com/phpMan.php/man/max/2/markdown)
              Return the maximum priority available in a specified scheduling policy.

       **sched**___**get**___**priority**___**[min**(2)](https://www.chedong.com/phpMan.php/man/min/2/markdown)
              Return the minimum priority available in a specified scheduling policy.

       **sched**___**rr**___**get**___**[interval**(2)](https://www.chedong.com/phpMan.php/man/interval/2/markdown)
              Fetch the quantum used for threads that are scheduled under the "round-robin" schedul‐
              ing policy.

       **sched**___**[yield**(2)](https://www.chedong.com/phpMan.php/man/yield/2/markdown)
              Cause the caller to relinquish the CPU, so that some other thread be executed.

       **sched**___**[setaffinity**(2)](https://www.chedong.com/phpMan.php/man/setaffinity/2/markdown)
              (Linux-specific) Set the CPU affinity of a specified thread.

       **sched**___**[getaffinity**(2)](https://www.chedong.com/phpMan.php/man/getaffinity/2/markdown)
              (Linux-specific) Get the CPU affinity of a specified thread.

       **sched**___**[setattr**(2)](https://www.chedong.com/phpMan.php/man/setattr/2/markdown)
              Set the scheduling policy and parameters of a specified thread.  This (Linux-specific)
              system  call  provides  a  superset  of the functionality of **sched**___**[setscheduler**(2)](https://www.chedong.com/phpMan.php/man/setscheduler/2/markdown) and
              **sched**___**[setparam**(2)](https://www.chedong.com/phpMan.php/man/setparam/2/markdown).

       **sched**___**[getattr**(2)](https://www.chedong.com/phpMan.php/man/getattr/2/markdown)
              Fetch the scheduling policy and parameters of a specified  thread.   This  (Linux-spe‐
              cific)  system  call provides a superset of the functionality of **sched**___**[getscheduler**(2)](https://www.chedong.com/phpMan.php/man/getscheduler/2/markdown)
              and **sched**___**[getparam**(2)](https://www.chedong.com/phpMan.php/man/getparam/2/markdown).

### Scheduling policies
       The scheduler is the kernel component that decides which runnable thread will be executed  by
       the CPU next.  Each thread has an associated scheduling policy and a _static_ scheduling prior‐
       ity, _sched_priority_.  The scheduler makes its decisions based on knowledge of the  scheduling
       policy and static priority of all threads on the system.

       For  threads  scheduled under one of the normal scheduling policies (**SCHED**___**OTHER**, **SCHED**___**IDLE**,
       **SCHED**___**BATCH**), _sched_priority_ is not used in scheduling decisions (it must be specified as 0).

       Processes scheduled under one  of  the  real-time  policies  (**SCHED**___**FIFO**,  **SCHED**___**RR**)  have  a
       _sched_priority_  value  in  the  range 1 (low) to 99 (high).  (As the numbers imply, real-time
       threads always have higher priority than normal threads.)  Note well: POSIX.1 requires an im‐
       plementation  to  support  only a minimum 32 distinct priority levels for the real-time poli‐
       cies, and some systems supply just this minimum.  Portable programs should use **sched**___**get**___**pri**‐‐
       **ority**___**[min**(2)](https://www.chedong.com/phpMan.php/man/min/2/markdown)  and  **sched**___**get**___**priority**___**[max**(2)](https://www.chedong.com/phpMan.php/man/max/2/markdown)  to find the range of priorities supported for a
       particular policy.

       Conceptually, the scheduler maintains a list of runnable threads for each possible _sched_pri__‐
       _ority_  value.   In  order  to  determine  which thread runs next, the scheduler looks for the
       nonempty list with the highest static priority and selects the thread at  the  head  of  this
       list.

       A  thread's  scheduling  policy determines where it will be inserted into the list of threads
       with equal static priority and how it will move inside this list.

       All scheduling is preemptive: if a thread with a higher static priority becomes ready to run,
       the  currently  running thread will be preempted and returned to the wait list for its static
       priority level.  The scheduling policy determines  the  ordering  only  within  the  list  of
       runnable threads with equal static priority.

   **SCHED**___**FIFO:** **First** **in-first** **out** **scheduling**
       **SCHED**___**FIFO**  can  be  used  only with static priorities higher than 0, which means that when a
       **SCHED**___**FIFO** thread becomes runnable, it will always immediately preempt any currently  running
       **SCHED**___**OTHER**,  **SCHED**___**BATCH**, or **SCHED**___**IDLE** thread.  **SCHED**___**FIFO** is a simple scheduling algorithm
       without time slicing.  For threads scheduled under the **SCHED**___**FIFO** policy, the following rules
       apply:

       1) A  running  **SCHED**___**FIFO** thread that has been preempted by another thread of higher priority
          will stay at the head of the list for its priority and will resume execution  as  soon  as
          all threads of higher priority are blocked again.

       2) When  a  blocked **SCHED**___**FIFO** thread becomes runnable, it will be inserted at the end of the
          list for its priority.

       3) If a call to **sched**___**[setscheduler**(2)](https://www.chedong.com/phpMan.php/man/setscheduler/2/markdown), **sched**___**[setparam**(2)](https://www.chedong.com/phpMan.php/man/setparam/2/markdown), **sched**___**[setattr**(2)](https://www.chedong.com/phpMan.php/man/setattr/2/markdown), **pthread**___**setsched**‐‐
          [**param**(3)](https://www.chedong.com/phpMan.php/man/param/3/markdown),  or  **pthread**___**[setschedprio**(3)](https://www.chedong.com/phpMan.php/man/setschedprio/3/markdown)  changes  the  priority  of the running or runnable
          **SCHED**___**FIFO** thread identified by _pid_ the effect on the thread's position in  the  list  de‐
          pends on the direction of the change to threads priority:

          •  If  the  thread's  priority  is raised, it is placed at the end of the list for its new
             priority.  As a consequence, it may preempt a currently running thread  with  the  same
             priority.

          •  If the thread's priority is unchanged, its position in the run list is unchanged.

          •  If  the thread's priority is lowered, it is placed at the front of the list for its new
             priority.

          According to POSIX.1-2008, changes to a thread's priority (or policy) using any  mechanism
          other  than **pthread**___**[setschedprio**(3)](https://www.chedong.com/phpMan.php/man/setschedprio/3/markdown) should result in the thread being placed at the end of
          the list for its priority.

       4) A thread calling **sched**___**[yield**(2)](https://www.chedong.com/phpMan.php/man/yield/2/markdown) will be put at the end of the list.

       No other events will move a thread scheduled under the **SCHED**___**FIFO** policy in the wait list  of
       runnable threads with equal static priority.

       A  **SCHED**___**FIFO**  thread runs until either it is blocked by an I/O request, it is preempted by a
       higher priority thread, or it calls **sched**___**[yield**(2)](https://www.chedong.com/phpMan.php/man/yield/2/markdown).

   **SCHED**___**RR:** **Round-robin** **scheduling**
       **SCHED**___**RR** is a simple enhancement of **SCHED**___**FIFO**.  Everything described  above  for  **SCHED**___**FIFO**
       also  applies  to **SCHED**___**RR**, except that each thread is allowed to run only for a maximum time
       quantum.  If a **SCHED**___**RR** thread has been running for a time period equal to or longer than the
       time quantum, it will be put at the end of the list for its priority.  A **SCHED**___**RR** thread that
       has been preempted by a higher priority thread and subsequently resumes execution as  a  run‐
       ning  thread will complete the unexpired portion of its round-robin time quantum.  The length
       of the time quantum can be retrieved using **sched**___**rr**___**get**___**[interval**(2)](https://www.chedong.com/phpMan.php/man/interval/2/markdown).

   **SCHED**___**DEADLINE:** **Sporadic** **task** **model** **deadline** **scheduling**
       Since version 3.14, Linux provides a deadline scheduling policy (**SCHED**___**DEADLINE**).  This  pol‐
       icy  is currently implemented using GEDF (Global Earliest Deadline First) in conjunction with
       CBS (Constant Bandwidth Server).  To set and fetch this policy and associated attributes, one
       must use the Linux-specific **sched**___**[setattr**(2)](https://www.chedong.com/phpMan.php/man/setattr/2/markdown) and **sched**___**[getattr**(2)](https://www.chedong.com/phpMan.php/man/getattr/2/markdown) system calls.

       A  sporadic task is one that has a sequence of jobs, where each job is activated at most once
       per period.  Each job also has a _relative_ _deadline_, before which it should finish  execution,
       and  a  _computation_  _time_, which is the CPU time necessary for executing the job.  The moment
       when a task wakes up because a new job has to be executed is called the  _arrival_  _time_  (also
       referred to as the request time or release time).  The _start_ _time_ is the time at which a task
       starts its execution.  The _absolute_ _deadline_ is thus obtained by adding the relative deadline
       to the arrival time.

       The following diagram clarifies these terms:

           arrival/wakeup                    absolute deadline
                |    start time                    |
                |        |                         |
                v        v                         v
           -----x--------xooooooooooooooooo--------x--------x---
                         |<- comp. time ->|
                |<------- relative deadline ------>|
                |<-------------- period ------------------->|

       When  setting  a  **SCHED**___**DEADLINE**  policy for a thread using **sched**___**[setattr**(2)](https://www.chedong.com/phpMan.php/man/setattr/2/markdown), one can specify
       three parameters: _Runtime_, _Deadline_, and _Period_.  These parameters do not necessarily  corre‐
       spond  to the aforementioned terms: usual practice is to set Runtime to something bigger than
       the average computation time (or worst-case execution time for hard real-time  tasks),  Dead‐
       line  to  the relative deadline, and Period to the period of the task.  Thus, for **SCHED**___**DEAD**‐‐
       **LINE** scheduling, we have:

           arrival/wakeup                    absolute deadline
                |    start time                    |
                |        |                         |
                v        v                         v
           -----x--------xooooooooooooooooo--------x--------x---
                         |<-- Runtime ------->|
                |<----------- Deadline ----------->|
                |<-------------- Period ------------------->|

       The three deadline-scheduling parameters correspond to the _sched_runtime_, _sched_deadline_, and
       _sched_period_  fields of the _sched_attr_ structure; see **sched**___**[setattr**(2)](https://www.chedong.com/phpMan.php/man/setattr/2/markdown).  These fields express
       values in nanoseconds.  If _sched_period_ is specified as 0,  then  it  is  made  the  same  as
       _sched_deadline_.

       The kernel requires that:

           sched_runtime <= sched_deadline <= sched_period

       In  addition,  under the current implementation, all of the parameter values must be at least
       1024 (i.e., just over one microsecond, which is the resolution of  the  implementation),  and
       less than 2^63.  If any of these checks fails, **sched**___**[setattr**(2)](https://www.chedong.com/phpMan.php/man/setattr/2/markdown) fails with the error **EINVAL**.

       The  CBS  guarantees  non-interference  between  tasks, by throttling threads that attempt to
       over-run their specified Runtime.

       To ensure deadline scheduling guarantees, the kernel must prevent situations where the set of
       **SCHED**___**DEADLINE**  threads is not feasible (schedulable) within the given constraints.  The ker‐
       nel thus performs an admittance test when setting or changing **SCHED**___**DEADLINE** policy  and  at‐
       tributes.   This  admission  test  calculates  whether  the change is feasible; if it is not,
       **sched**___**[setattr**(2)](https://www.chedong.com/phpMan.php/man/setattr/2/markdown) fails with the error **EBUSY**.

       For example, it is required (but not necessarily sufficient) for the total utilization to  be
       less  than or equal to the total number of CPUs available, where, since each thread can maxi‐
       mally run for Runtime per Period, that thread's utilization is its Runtime divided by its Pe‐
       riod.

       In order to fulfill the guarantees that are made when a thread is admitted to the **SCHED**___**DEAD**‐‐
       **LINE** policy, **SCHED**___**DEADLINE** threads are the highest priority (user controllable)  threads  in
       the  system;  if  any **SCHED**___**DEADLINE** thread is runnable, it will preempt any thread scheduled
       under one of the other policies.

       A call to [**fork**(2)](https://www.chedong.com/phpMan.php/man/fork/2/markdown) by a thread scheduled under the **SCHED**___**DEADLINE** policy fails with the  error
       **EAGAIN**, unless the thread has its reset-on-fork flag set (see below).

       A  **SCHED**___**DEADLINE**  thread that calls **sched**___**[yield**(2)](https://www.chedong.com/phpMan.php/man/yield/2/markdown) will yield the current job and wait for a
       new period to begin.

   **SCHED**___**OTHER:** **Default** **Linux** **time-sharing** **scheduling**
       **SCHED**___**OTHER** can be used at only static priority 0 (i.e., threads under real-time policies al‐
       ways have priority over **SCHED**___**OTHER** processes).  **SCHED**___**OTHER** is the standard Linux time-shar‐
       ing 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**.

### The nice value
       The nice value is an attribute that can be used to influence the CPU scheduler  to  favor  or
       disfavor  a  process  in  scheduling decisions.  It affects the scheduling of **SCHED**___**OTHER** and
       **SCHED**___**BATCH** (see below) processes.  The nice value can be modified using  [**nice**(2)](https://www.chedong.com/phpMan.php/man/nice/2/markdown),  **setprior**‐‐
       [**ity**(2)](https://www.chedong.com/phpMan.php/man/ity/2/markdown), or **sched**___**[setattr**(2)](https://www.chedong.com/phpMan.php/man/setattr/2/markdown).

       According  to  POSIX.1,  the nice value is a per-process attribute; that is, the threads in a
       process should share a nice value.  However, on Linux, the nice value is a per-thread  attri‐
       bute: different threads in the same process may have different nice values.

       The  range  of  the nice value varies across UNIX systems.  On modern Linux, the range is -20
       (high priority) to +19 (low priority).  On some other systems, the range  is  -20..20.   Very
       early Linux kernels (Before Linux 2.0) had the range -infinity..15.

       The  degree  to which the nice value affects the relative scheduling of **SCHED**___**OTHER** processes
       likewise varies across UNIX systems and across Linux kernel versions.

       With the advent of the CFS scheduler in kernel 2.6.23, Linux adopted an algorithm that causes
       relative differences in nice values to have a much stronger effect.  In the current implemen‐
       tation, each unit of difference in the nice values of two processes results in  a  factor  of
       1.25  in  the  degree to which the scheduler favors the higher priority process.  This causes
       very low nice values (+19) to truly provide little CPU to a process  whenever  there  is  any
       other  higher  priority  load on the system, and makes high nice values (-20) deliver most of
       the CPU to applications that require it (e.g., some audio applications).

       On Linux, the **RLIMIT**___**NICE** resource limit can be used to define a limit to which  an  unprivi‐
       leged process's nice value can be raised; see [**setrlimit**(2)](https://www.chedong.com/phpMan.php/man/setrlimit/2/markdown) for details.

       For further details on the nice value, see the subsections on the autogroup feature and group
       scheduling, below.

   **SCHED**___**BATCH:** **Scheduling** **batch** **processes**
       (Since Linux 2.6.16.)  **SCHED**___**BATCH** can be used only at static priority  0.   This  policy  is
       similar  to  **SCHED**___**OTHER**  in  that  it schedules the thread according to its dynamic priority
       (based on the nice value).  The difference is that this policy will cause  the  scheduler  to
       always  assume  that  the  thread is CPU-intensive.  Consequently, the scheduler will apply a
       small scheduling penalty with respect to wakeup behavior, so that this thread is mildly  dis‐
       favored in scheduling decisions.

       This  policy  is useful for workloads that are noninteractive, but do not want to lower their
       nice value, and for workloads that want a deterministic scheduling policy without interactiv‐
       ity causing extra preemptions (between the workload's tasks).

   **SCHED**___**IDLE:** **Scheduling** **very** **low** **priority** **jobs**
       (Since  Linux  2.6.23.)   **SCHED**___**IDLE**  can be used only at static priority 0; the process nice
       value has no influence for this policy.

       This policy is intended for running jobs at extremely low priority (lower  even  than  a  +19
       nice value with the **SCHED**___**OTHER** or **SCHED**___**BATCH** policies).

### Resetting scheduling policy for child processes
       Each  thread has a reset-on-fork scheduling flag.  When this flag is set, children created by
       [**fork**(2)](https://www.chedong.com/phpMan.php/man/fork/2/markdown) do not inherit privileged scheduling policies.  The reset-on-fork flag can be set  by
       either:

       *  ORing  the  **SCHED**___**RESET**___**ON**___**FORK** flag into the _policy_ argument when calling **sched**___**setsched**‐‐
          [**uler**(2)](https://www.chedong.com/phpMan.php/man/uler/2/markdown) (since Linux 2.6.32); or

       *  specifying the **SCHED**___**FLAG**___**RESET**___**ON**___**FORK** flag in _attr.sched_flags_  when  calling  **sched**___**se**‐‐
          [**tattr**(2)](https://www.chedong.com/phpMan.php/man/tattr/2/markdown).

       Note  that the constants used with these two APIs have different names.  The state of the re‐
       set-on-fork   flag   can   analogously   be   retrieved   using   **sched**___**[getscheduler**(2)](https://www.chedong.com/phpMan.php/man/getscheduler/2/markdown)   and
       **sched**___**[getattr**(2)](https://www.chedong.com/phpMan.php/man/getattr/2/markdown).

       The  reset-on-fork  feature  is  intended for media-playback applications, and can be used to
       prevent applications evading the **RLIMIT**___**RTTIME** resource limit (see [**getrlimit**(2)](https://www.chedong.com/phpMan.php/man/getrlimit/2/markdown)) by  creating
       multiple child processes.

       More  precisely, if the reset-on-fork flag is set, the following rules apply for subsequently
       created children:

       *  If the calling thread has a scheduling policy of **SCHED**___**FIFO** or **SCHED**___**RR**, the policy is re‐
          set to **SCHED**___**OTHER** in child processes.

       *  If the calling process has a negative nice value, the nice value is reset to zero in child
          processes.

       After the reset-on-fork flag has been enabled, it can be reset only if  the  thread  has  the
       **CAP**___**SYS**___**NICE** capability.  This flag is disabled in child processes created by [**fork**(2)](https://www.chedong.com/phpMan.php/man/fork/2/markdown).

### Privileges and resource limits
       In  Linux  kernels  before  2.6.12,  only privileged (**CAP**___**SYS**___**NICE**) threads can set a nonzero
       static priority (i.e., set a real-time scheduling policy).  The only change that an  unprivi‐
       leged  thread can make is to set the **SCHED**___**OTHER** policy, and this can be done only if the ef‐
       fective user ID of the caller matches the real or effective user  ID  of  the  target  thread
       (i.e., the thread specified by _pid_) whose policy is being changed.

       A thread must be privileged (**CAP**___**SYS**___**NICE**) in order to set or modify a **SCHED**___**DEADLINE** policy.

       Since  Linux  2.6.12,  the  **RLIMIT**___**RTPRIO** resource limit defines a ceiling on an unprivileged
       thread's static priority for the **SCHED**___**RR** and **SCHED**___**FIFO** policies.  The  rules  for  changing
       scheduling policy and priority are as follows:

       *  If  an  unprivileged thread has a nonzero **RLIMIT**___**RTPRIO** soft limit, then it can change its
          scheduling policy and priority, subject to the restriction that the priority cannot be set
          to  a  value  higher  than  the maximum of its current priority and its **RLIMIT**___**RTPRIO** soft
          limit.

       *  If the **RLIMIT**___**RTPRIO** soft limit is 0, then the only permitted changes  are  to  lower  the
          priority, or to switch to a non-real-time policy.

       *  Subject  to  the  same  rules, another unprivileged thread can also make these changes, as
          long as the effective user ID of the thread making the change matches the real  or  effec‐
          tive user ID of the target thread.

       *  Special rules apply for the **SCHED**___**IDLE** policy.  In Linux kernels before 2.6.39, an unpriv‐
          ileged thread operating under this policy cannot change  its  policy,  regardless  of  the
          value of its **RLIMIT**___**RTPRIO** resource limit.  In Linux kernels since 2.6.39, an unprivileged
          thread can switch to either the **SCHED**___**BATCH** or the **SCHED**___**OTHER** policy so long as its  nice
          value  falls  within  the  range  permitted  by  its **RLIMIT**___**NICE** resource limit (see **getr**‐‐
          [**limit**(2)](https://www.chedong.com/phpMan.php/man/limit/2/markdown)).

       Privileged (**CAP**___**SYS**___**NICE**) threads ignore the **RLIMIT**___**RTPRIO** limit; as with older kernels, they
       can  make  arbitrary changes to scheduling policy and priority.  See [**getrlimit**(2)](https://www.chedong.com/phpMan.php/man/getrlimit/2/markdown) for further
       information on **RLIMIT**___**RTPRIO**.

### Limiting the CPU usage of real-time and deadline processes
       A nonblocking infinite loop  in  a  thread  scheduled  under  the  **SCHED**___**FIFO**,  **SCHED**___**RR**,  or
       **SCHED**___**DEADLINE** policy can potentially block all other threads from accessing the CPU forever.
       Prior to Linux 2.6.25, the only way of preventing a runaway real-time process  from  freezing
       the  system was to run (at the console) a shell scheduled under a higher static priority than
       the tested application.  This allows an emergency kill of tested real-time applications  that
       do not block or terminate as expected.

       Since  Linux  2.6.25, there are other techniques for dealing with runaway real-time and dead‐
       line processes.  One of these is to use the **RLIMIT**___**RTTIME** resource limit to set a ceiling  on
       the CPU time that a real-time process may consume.  See [**getrlimit**(2)](https://www.chedong.com/phpMan.php/man/getrlimit/2/markdown) for details.

       Since  version 2.6.25, Linux also provides two _/proc_ files that can be used to reserve a cer‐
       tain amount of CPU time to be used by non-real-time processes.  Reserving CPU  time  in  this
       fashion allows some CPU time to be allocated to (say) a root shell that can be used to kill a
       runaway process.  Both of these files specify time values in microseconds:

       _/proc/sys/kernel/sched_rt_period_us_
              This file specifies a scheduling period that is equivalent to 100% CPU bandwidth.  The
              value  in  this  file  can range from 1 to **INT**___**MAX**, giving an operating range of 1 mi‐
              crosecond to around 35 minutes.  The default value in this file is 1,000,000  (1  sec‐
              ond).

       _/proc/sys/kernel/sched_rt_runtime_us_
              The  value  in  this  file  specifies how much of the "period" time can be used by all
              real-time and deadline scheduled processes on the system.  The value in this file  can
              range  from -1 to **INT**___**MAX**-1.  Specifying -1 makes the run time the same as the period;
              that is, no CPU time is set aside for non-real-time processes (which was the Linux be‐
              havior  before  kernel  2.6.25).  The default value in this file is 950,000 (0.95 sec‐
              onds), meaning that 5% of the CPU time is reserved for processes that don't run  under
              a real-time or deadline scheduling policy.

### Response time
       A  blocked  high  priority  thread  waiting  for I/O has a certain response time before it is
       scheduled again.  The device driver writer can greatly reduce this response time by  using  a
       "slow interrupt" interrupt handler.

### Miscellaneous
       Child  processes inherit the scheduling policy and parameters across a [**fork**(2)](https://www.chedong.com/phpMan.php/man/fork/2/markdown).  The schedul‐
       ing policy and parameters are preserved across [**execve**(2)](https://www.chedong.com/phpMan.php/man/execve/2/markdown).

       Memory locking is usually needed for real-time processes to avoid paging delays; this can  be
       done with [**mlock**(2)](https://www.chedong.com/phpMan.php/man/mlock/2/markdown) or [**mlockall**(2)](https://www.chedong.com/phpMan.php/man/mlockall/2/markdown).

### The autogroup feature
       Since  Linux  2.6.38, the kernel provides a feature known as autogrouping to improve interac‐
       tive desktop performance in the face of multiprocess, CPU-intensive workloads such as  build‐
       ing  the  Linux  kernel  with large numbers of parallel build processes (i.e., the [**make**(1)](https://www.chedong.com/phpMan.php/man/make/1/markdown) **-j**
       flag).

       This feature operates in conjunction with the CFS scheduler and requires  a  kernel  that  is
       configured with **CONFIG**___**SCHED**___**AUTOGROUP**.  On a running system, this feature is enabled or dis‐
       abled via the file _/proc/sys/kernel/sched_autogroup_enabled_; a value of 0 disables  the  fea‐
       ture,  while a value of 1 enables it.  The default value in this file is 1, unless the kernel
       was booted with the _noautogroup_ parameter.

       A new autogroup is created when a new session is created via [**setsid**(2)](https://www.chedong.com/phpMan.php/man/setsid/2/markdown); this happens, for ex‐
       ample,  when a new terminal window is started.  A new process created by [**fork**(2)](https://www.chedong.com/phpMan.php/man/fork/2/markdown) inherits its
       parent's autogroup membership.  Thus, all of the processes in a session are  members  of  the
       same  autogroup.   An autogroup is automatically destroyed when the last process in the group
       terminates.

       When autogrouping is enabled, all of the members of an autogroup are placed in the same  ker‐
       nel  scheduler  "task group".  The CFS scheduler employs an algorithm that equalizes the dis‐
       tribution of CPU cycles across task groups.  The benefits of  this  for  interactive  desktop
       performance can be described via the following example.

       Suppose that there are two autogroups competing for the same CPU (i.e., presume either a sin‐
       gle CPU system or the use of [**taskset**(1)](https://www.chedong.com/phpMan.php/man/taskset/1/markdown) to confine all the processes to the same  CPU  on  an
       SMP  system).   The  first group contains ten CPU-bound processes from a kernel build started
       with _make_ _-j10_.  The other contains a single CPU-bound process: a video player.   The  effect
       of  autogrouping  is  that the two groups will each receive half of the CPU cycles.  That is,
       the video player will receive 50% of the CPU cycles, rather than just 9% of the cycles, which
       would  likely  lead  to degraded video playback.  The situation on an SMP system is more com‐
       plex, but the general effect is the same: the scheduler distributes CPU  cycles  across  task
       groups  such  that  an autogroup that contains a large number of CPU-bound processes does not
       end up hogging CPU cycles at the expense of the other jobs on the system.

       A process's autogroup (task group) membership can be viewed via  the  file  _/proc/[pid]/auto__‐
       _group_:

           $ **cat** **/proc/1/autogroup**
           /autogroup-1 nice 0

       This  file  can  also be used to modify the CPU bandwidth allocated to an autogroup.  This is
       done by writing a number in the "nice" range to the file to set the autogroup's  nice  value.
       The allowed range is from +19 (low priority) to -20 (high priority).  (Writing values outside
       of this range causes [**write**(2)](https://www.chedong.com/phpMan.php/man/write/2/markdown) to fail with the error **EINVAL**.)

       The autogroup nice setting has the same meaning as the process nice  value,  but  applies  to
       distribution  of CPU cycles to the autogroup as a whole, based on the relative nice values of
       other autogroups.  For a process inside an autogroup, the CPU cycles that it receives will be
       a product of the autogroup's nice value (compared to other autogroups) and the process's nice
       value (compared to other processes in the same autogroup.

       The use of the [**cgroups**(7)](https://www.chedong.com/phpMan.php/man/cgroups/7/markdown) CPU controller to place processes in cgroups other  than  the  root
       CPU cgroup overrides the effect of autogrouping.

       The   autogroup   feature  groups  only  processes  scheduled  under  non-real-time  policies
       (**SCHED**___**OTHER**, **SCHED**___**BATCH**, and **SCHED**___**IDLE**).  It does  not  group  processes  scheduled  under
       real-time  and  deadline  policies.  Those processes are scheduled according to the rules de‐
       scribed earlier.

### The nice value and group scheduling
       When scheduling  non-real-time  processes  (i.e.,  those  scheduled  under  the  **SCHED**___**OTHER**,
       **SCHED**___**BATCH**,  and **SCHED**___**IDLE** policies), the CFS scheduler employs a technique known as "group
       scheduling", if the kernel was configured with the **CONFIG**___**FAIR**___**GROUP**___**SCHED** option  (which  is
       typical).

       Under  group  scheduling, threads are scheduled in "task groups".  Task groups have a hierar‐
       chical relationship, rooted under the initial task group on the system, known  as  the  "root
       task group".  Task groups are formed in the following circumstances:

       *  All  of  the  threads in a CPU cgroup form a task group.  The parent of this task group is
          the task group of the corresponding parent cgroup.

       *  If autogrouping is enabled, then all of the threads that are (implicitly) placed in an au‐
          togroup (i.e., the same session, as created by [**setsid**(2)](https://www.chedong.com/phpMan.php/man/setsid/2/markdown)) form a task group.  Each new au‐
          togroup is thus a separate task group.  The root task group is the parent of all such  au‐
          togroups.

       *  If autogrouping is enabled, then the root task group consists of all processes in the root
          CPU cgroup that were not otherwise implicitly placed into a new autogroup.

       *  If autogrouping is disabled, then the root task group consists of  all  processes  in  the
          root CPU cgroup.

       *  If   group  scheduling  was  disabled  (i.e.,  the  kernel  was  configured  without  **CON**‐‐
          **FIG**___**FAIR**___**GROUP**___**SCHED**), then all of the processes on the system are notionally placed in  a
          single task group.

       Under  group  scheduling,  a  thread's nice value has an effect for scheduling decisions _only_
       _relative_ _to_ _other_ _threads_ _in_ _the_ _same_ _task_ _group_.  This has some surprising  consequences  in
       terms  of the traditional semantics of the nice value on UNIX systems.  In particular, if au‐
       togrouping is enabled (which is the default in various distributions), then employing **setpri**‐‐
       [**ority**(2)](https://www.chedong.com/phpMan.php/man/ority/2/markdown)  or  [**nice**(1)](https://www.chedong.com/phpMan.php/man/nice/1/markdown)  on  a process has an effect only for scheduling relative to other pro‐
       cesses executed in the same session (typically: the same terminal window).

       Conversely, for two processes that are (for example) the sole CPU-bound processes in  differ‐
       ent  sessions (e.g., different terminal windows, each of whose jobs are tied to different au‐
       togroups), _modifying_ _the_ _nice_ _value_ _of_ _the_ _process_ _in_ _one_ _of_ _the_ _sessions_ _has_  _no_  _effect_  in
       terms  of the scheduler's decisions relative to the process in the other session.  A possibly
       useful workaround here is to use a command such as the following to modify the autogroup nice
       value for _all_ of the processes in a terminal session:

           $ **echo** **10** **>** **/proc/self/autogroup**

### Real-time features in the mainline Linux kernel
       Since  kernel  version  2.6.18, Linux is gradually becoming equipped with real-time capabili‐
       ties, most of which are derived from  the  former  _realtime-preempt_  patch  set.   Until  the
       patches  have  been  completely  merged  into  the mainline kernel, they must be installed to
       achieve the best real-time performance.  These patches are named:

           patch-_kernelversion_-rt_patchversion_

       and can be downloaded from ⟨<http://www.kernel.org/pub/linux/kernel/projects/rt/>⟩.

       Without the patches and prior to their full inclusion into the mainline  kernel,  the  kernel
       configuration  offers  only  the  three  preemption  classes **CONFIG**___**PREEMPT**___**NONE**, **CONFIG**___**PRE**‐‐
       **EMPT**___**VOLUNTARY**, and **CONFIG**___**PREEMPT**___**DESKTOP** which respectively provide no, some, and consider‐
       able reduction of the worst-case scheduling latency.

       With  the  patches  applied or after their full inclusion into the mainline kernel, the addi‐
       tional configuration item **CONFIG**___**PREEMPT**___**RT** becomes available.  If this is selected, Linux is
       transformed  into  a regular real-time operating system.  The FIFO and RR scheduling policies
       are then used to run a thread with true real-time priority and a minimum worst-case  schedul‐
       ing latency.

## NOTES
       The  [**cgroups**(7)](https://www.chedong.com/phpMan.php/man/cgroups/7/markdown)  CPU  controller  can  be used to limit the CPU consumption of groups of pro‐
       cesses.

       Originally, Standard Linux was intended as a general-purpose operating system being  able  to
       handle  background processes, interactive applications, and less demanding real-time applica‐
       tions (applications that need to usually meet timing deadlines).  Although the  Linux  kernel
       2.6  allowed  for  kernel preemption and the newly introduced [O(1)](https://www.chedong.com/phpMan.php/man/O/1/markdown) scheduler ensures that the
       time needed to schedule is fixed and deterministic  irrespective  of  the  number  of  active
       tasks, true real-time computing was not possible up to kernel version 2.6.17.

## SEE ALSO
       [**chcpu**(1)](https://www.chedong.com/phpMan.php/man/chcpu/1/markdown), [**chrt**(1)](https://www.chedong.com/phpMan.php/man/chrt/1/markdown), [**lscpu**(1)](https://www.chedong.com/phpMan.php/man/lscpu/1/markdown), [**ps**(1)](https://www.chedong.com/phpMan.php/man/ps/1/markdown), [**taskset**(1)](https://www.chedong.com/phpMan.php/man/taskset/1/markdown), [**top**(1)](https://www.chedong.com/phpMan.php/man/top/1/markdown), [**getpriority**(2)](https://www.chedong.com/phpMan.php/man/getpriority/2/markdown), [**mlock**(2)](https://www.chedong.com/phpMan.php/man/mlock/2/markdown),
       [**mlockall**(2)](https://www.chedong.com/phpMan.php/man/mlockall/2/markdown), [**munlock**(2)](https://www.chedong.com/phpMan.php/man/munlock/2/markdown), [**munlockall**(2)](https://www.chedong.com/phpMan.php/man/munlockall/2/markdown), [**nice**(2)](https://www.chedong.com/phpMan.php/man/nice/2/markdown), **sched**___**get**___**priority**___**[max**(2)](https://www.chedong.com/phpMan.php/man/max/2/markdown),
       **sched**___**get**___**priority**___**[min**(2)](https://www.chedong.com/phpMan.php/man/min/2/markdown), **sched**___**[getaffinity**(2)](https://www.chedong.com/phpMan.php/man/getaffinity/2/markdown), **sched**___**[getparam**(2)](https://www.chedong.com/phpMan.php/man/getparam/2/markdown), **sched**___**[getscheduler**(2)](https://www.chedong.com/phpMan.php/man/getscheduler/2/markdown),
       **sched**___**rr**___**get**___**[interval**(2)](https://www.chedong.com/phpMan.php/man/interval/2/markdown), **sched**___**[setaffinity**(2)](https://www.chedong.com/phpMan.php/man/setaffinity/2/markdown), **sched**___**[setparam**(2)](https://www.chedong.com/phpMan.php/man/setparam/2/markdown), **sched**___**[setscheduler**(2)](https://www.chedong.com/phpMan.php/man/setscheduler/2/markdown),
       **sched**___**[yield**(2)](https://www.chedong.com/phpMan.php/man/yield/2/markdown), [**setpriority**(2)](https://www.chedong.com/phpMan.php/man/setpriority/2/markdown), **pthread**___**getaffinity**___**[np**(3)](https://www.chedong.com/phpMan.php/man/np/3/markdown), **pthread**___**[getschedparam**(3)](https://www.chedong.com/phpMan.php/man/getschedparam/3/markdown),
       **pthread**___**setaffinity**___**[np**(3)](https://www.chedong.com/phpMan.php/man/np/3/markdown), **sched**___**[getcpu**(3)](https://www.chedong.com/phpMan.php/man/getcpu/3/markdown), [**capabilities**(7)](https://www.chedong.com/phpMan.php/man/capabilities/7/markdown), [**cpuset**(7)](https://www.chedong.com/phpMan.php/man/cpuset/7/markdown)

       _Programming_ _for_ _the_ _real_ _world_ _-_ _POSIX.4_ by Bill O. Gallmeister, O'Reilly & Associates, Inc.,
       ISBN 1-56592-074-0.

       The     Linux     kernel     source     files     _Documentation/scheduler/sched-deadline.txt_,
       _Documentation/scheduler/sched-rt-group.txt_, _Documentation/scheduler/sched-design-CFS.txt_, and
       _Documentation/scheduler/sched-nice-design.txt_

## COLOPHON
       This  page  is  part  of  release  5.10 of the Linux _man-pages_ project.  A description of the
       project, information about reporting bugs, and the latest version of this page, can be  found
       at <https://www.kernel.org/doc/man-pages/>.



Linux                                        2019-08-02                                     [SCHED(7)](https://www.chedong.com/phpMan.php/man/SCHED/7/markdown)
