What Is Difference Between Sched_Batch and Sched_Other Scheduling

What is difference between sched_batch and sched_other scheduling?

Threads that are scheduled with sched_batch are assumed to be non-interactive, but CPU bound and optimized for throughput. Thus, this policy is more cache-friendly. The default sched_batch timeslice is 1.5 seconds. In addition, in case of SMP, sched_batch will migrate to a core with high idleness (with respect to non-batch threads).

Linux SCHED_OTHER, SCHED_FIFO and SCHED_RR - differences

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.

Update 2: since Linux 4.13, SCHED_DEADLINE has replaced CBS with the Greedy Reclamation of Unused Bandwidth (GRUB) algorithm.

Scheduling Policy in an exec system call after a fork

Firstly, exec family of system calls does not executes it's own image rather it executes the binary it loads. As per man exec:

The exec() family of functions replaces the current process image with a new process image.

So, it'll behave how that particular executable image was prepared to be scheduled.

When a process it marked to be scheduled as SCHED_BATCH, they will be scheduled based on their nice values just like SCHED_OTHER. Since, batch tasks doesn't require user interaction therefore scheduler treats the task as CPU-intensive. As per man sched_setschedparam quote from SCHED_BATCH: Scheduling batch process -

This policy is similar to SCHED_OTHER in that it schedules the process 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 process is CPU-intensive. Consequently, the scheduler will apply a small scheduling penalty with respect to wakeup behaviour, so that this process is mildly disfavored in scheduling decisions.

Therefore, if you change a process's scheduling policy to SCHED_BATCH, it'll be scheduled just like almost any other normal processes (SCHED_OTHER, the default scheduling policy). If you want to fallback to the fully default behavior then you have to utilize SCHED_RESET_ON_FORK flag ORed with the scheduling policy. If you specify that flag, any newly created process will fallback to default scheduling policy rather copying parent's behavior.

Hope this helps!

How to know linux scheduler time slice?

The quantum allocated for a particular process may vary:

You can tune "slice" by adjusting sched_latency_ns and
sched_min_granularity_ns, but note that "slice" is not a fixed
quantum. Also note that CFS preemption decisions are based upon
instantaneous state. A task may have received a full (variable)
"slice" of CPU time, but preemption will be triggered only if a more
deserving task is available, so a "slice" is not the "max
uninterrupted CPU time" that you may expect it to be.. but it is
somewhat similar.

This is because the Completely Fair Scheduler, the default Linux scheduler, assigns a proportion of the processor to a process rather than a fixed timeslice. That means the timeslice for each process is proportional to the current load and weighted by the process' priority value.

For special-purpose realtime processes which use SCHED_RR, the default timeslice is defined in the Linux kernel as RR_TIMESLICE in include/linux/sched/rt.h.

/*
* default timeslice is 100 msecs (used only for SCHED_RR tasks).
* Timeslices get refilled after they expire.
*/
#define RR_TIMESLICE (100 * HZ / 1000)

You can use sched_rr_get_interval() to get the SCHED_RR interval for a specific SCHED_RR process.

How to know linux scheduler time slice?

The quantum allocated for a particular process may vary:

You can tune "slice" by adjusting sched_latency_ns and
sched_min_granularity_ns, but note that "slice" is not a fixed
quantum. Also note that CFS preemption decisions are based upon
instantaneous state. A task may have received a full (variable)
"slice" of CPU time, but preemption will be triggered only if a more
deserving task is available, so a "slice" is not the "max
uninterrupted CPU time" that you may expect it to be.. but it is
somewhat similar.

This is because the Completely Fair Scheduler, the default Linux scheduler, assigns a proportion of the processor to a process rather than a fixed timeslice. That means the timeslice for each process is proportional to the current load and weighted by the process' priority value.

For special-purpose realtime processes which use SCHED_RR, the default timeslice is defined in the Linux kernel as RR_TIMESLICE in include/linux/sched/rt.h.

/*
* default timeslice is 100 msecs (used only for SCHED_RR tasks).
* Timeslices get refilled after they expire.
*/
#define RR_TIMESLICE (100 * HZ / 1000)

You can use sched_rr_get_interval() to get the SCHED_RR interval for a specific SCHED_RR process.

Usage of scheduling policy flags SCHED_BATCH or SCHED_IDLE gives error

SCHED_BATCH and SCHED_IDLE are Linux specific and are not part of POSIX standard.

Glibc doesn't expose them unless you define _GNU_SOURCE. So, do:

#define _GNU_SOURCE

before including headers.

What is highest priority of threads in process?

man sched_get_priority_max:

int sched_get_priority_max(int policy);
int sched_get_priority_min(int policy);

sched_get_priority_max() returns the maximum priority value that can be used with the scheduling algorithm identified by policy. sched_get_priority_min() returns the minimum priority value that can be used with the scheduling algorithm identified by policy. Supported policy values are SCHED_FIFO, SCHED_RR, SCHED_OTHER, SCHED_BATCH, SCHED_IDLE, and SCHED_DEADLINE.


Note that 0 is not specified in the list of valid policies.

How to check the process scheduling policy other than `ps` output

You can find schedule information of a process by looking at /proc/pocess_id/sched.

For example:

awk '/policy/ {print $NF}' /proc/25/sched

would give you the policy number of process 25.

For more information on policy numbers, you can look at man sched_setscheduler:

Scheduling Policies:
...
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-2001 requires an implementation to support only a minimum
32 distinct priority levels for the real-time policies, and some
systems supply just this minimum. Portable programs should
use sched_get_priority_min(2) and sched_get_priority_max(2) to
find the range of priorities supported for a particular policy.


Related Topics



Leave a reply



Submit