What does sched_priority in struct sched_param refer to? - linux

im having some hard time understanding the sched_priority role in the setscheduler func.
Im using linux 2.4.X, and the documentation says :
Valid priorities for SCHED_OTHER is 0, Valid priorities for SCHED_RR\FIFO are 1...MAX_USER_RT_PRIO-1
But, I remember that priorities for rt procsses are 0-99, and for SCHED_OTHER 100-139, so... what did I miss ? what sched_priority in struct sched_param refers to?

SCHED_OTHER is the default scheduling policy with round robin. This is has no choice of priority.
SCHED_FIFO and SCHED_RR are real time scheduling policies for which the priority range from 1 to 99.
SCHED_OTHER, SCHED_BATCH and SCHED_IDLE are normal scheduling policies.
From Linux 3.14 onwards, you will find SCHED_DEADLINE that in which the task with earliest deadline is executed first.
You can use the sched_priority to set the thread priority. sched_priority is a member of the structure struct sched_param.
Try out chrt -m to check the min/max valid priorities that can be configured/set. chrt can be used to set or retrieve scheduling attributes of processes.

You are confusing the user space interface with the internal kernel implementation.
sched_priority is just the requested fixed real time priority.
A task scheduled under the SCHED_OTHER policy also has a nice level.
The scale you remember is the kernel internal representation of the relative priority of a task, aggregating real time tasks and SCHED_OTHER tasks with their respective nice levels.

Related

Real time schedulers in Linux

Why, in Linux, the sched_fifo and the sched_rr are called real-time schedulers, while sched_other is called non real-time scheduler ?
The term "real-time" when it comes to schedulers is used to denote if that scheduler can mathematically guarantee that within certain parameters all the periodic tasks within the system will meet their deadlines. This is important in things like closed loop control.
The problem with the Linux kernel is that even if you are using a real-time scheduler you do not have the guarantee that you will meet deadlines because of things like spin locks in the kernel itself. If you want true hard real-time you have to not only use a real-time scheduler but have to use the real-time patches as of this writing. In the future it is intended by some in the community that those patches be put into the mainline kernel.
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 priority, 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.)

what is the difference among three priorities used in Linux kernel?

I am new to Linux kernel and right now i am studying about Process Scheduling in Linux kernel . There are three types of priorities in Linux :
Static priority
Dynamic priority
Real time priority
Now what i have understood is that :
Static priority and Dynamic priority are defined only for Conventional processes and they can take value from 100 to 139 only.
Static priority is used to determine base time slice of a process
Dynamic priority is used to select a process to be executed next.
Real time priorities are defined only for Real time processes and it's value can range from 0 to 99
Now my questions are :
Correct me if i am wrong and please also tell me why we are using
three types of priorities in Linux and what are the differences
among these priorities?
Are the processes are differentiated as Real time or Conventional on the basis of priorities that is if priority is between 100 to 139
then processes are Conventional processes otherwise Real time
processes?
How the priorities are changed in Linux , i mean , we know that priority of a process does not remain constant through out the execution ?
Disclaimer: Following is true for scheduling in linux (I am not sure about windows or other OS). Thread and process has been used interchangeably here, althogh there is a difference in between them.
Priorities & differences
1.Static priority: These are the default priorities (value 0 for conventional processes aka non-real time processes ie when real time scheduling is not used) set while creating a new thread. You can change them using:
`pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param);`
where, sched_param contains the priority:
struct sched_param
{
int sched_priority; /* Scheduling priority */
};
2 Dynamic priority: When threads start to starve because higher priority threads being scheduled all the time, there becomes a need to raise the priority of such a thread using various mechanisms. This raised/lowered (yes, this happens too) priority is known as the dynamic priority because it keeps on changing. In Linux, even the fat kid gets to play.
3.Real time priority: This comes into picture only when threads (processes) are scheduled under one of the real-time policies (SCHED_FIFO, SCHED_RR) and have a sched_priority value in the range 1 (low) to 99 (high). This is the highest in comparison to the static/dynamic priorities of non real time processes.
More information: http://man7.org/linux/man-pages/man3/pthread_getschedparam.3.html
Now, to your questions:
Correct me if i am wrong and please also tell me why we are using three types of priorities in Linux and what are the differences among
these priorities?
So, for non-real time scheduling policies, every process has some static priorities, a higher priority gives the thread a kick-start, and later to avoid any injustice, the priority is boosted/lowered which becomes the dynamic priority.
Are the processes are differentiated as Real time or Conventional on the basis of priorities that is if priority is between 100 to 139
then processes are Conventional processes otherwise Real time
processes?
Not really, it depends upon the scheduling mechanism in place.
How the priorities are changed in Linux , i mean , we know that priority of a process does not remain constant through out the
execution ?
That's when the dynamicness comes into picture. Read about the "nice value" in the given links.

Why we define Scheduler timeslice in CFS also?

To be specific, I am talking about Linux kernel Scheduling system after CFS patch merged.
Everywhere it is mentioned that in the CFS (completely fair scheduler) there is no fixed timeslice for the process and timeslice is calculated based on the equal division of the number of processes running in the system as they were executing in parallel in hardware. Figure explains it more ..
Still why we define the scheduler timeslice in the kernel?
http://lxr.free-electrons.com/source/include/linux/sched/rt.h#L62
Like the comment in the link says, that is the default time slice. For each scheduler implemented, the value of the time slices may change, if it makes sense.
For example, in the real time scheduler with the SCHED_RR policy, you can see a default time slice is used, whereas for the SCHED_FIFO policy the time slice is 0 because tasks with the SCHED_FIFO policy must preempt every other task.
In the case of Completely Fair Scheduling, the time slice is computed in get_rr_interval_fair by calling sched_slice. It computes the slice based on the number of running tasks and its weight (which in turn is determined by the process' nice level).

How does SCHED_FIFO and SCHED_RR interfer with each other?

SCHED_FIFO and SCHED_RR are both meant for real-time uses. I am aware that SCHED_RR can be preempted by time slicing. But say if I have one thread set to SCHED_FIFO, and another set to SCHED_RR, if both threads are ready to run, are they scheduled purely by priority? What if they have same priority?
Conceptually, there is a list of runnable processes associated with each static priority level. These lists can contain both SCHED_FIFO and SCHED_RR processes - the two scheduling policies share the same set of static priorities.
When selecting a process to run, the scheduler takes the process at the head of the non-empty list with the highest static priority, regardless of the scheduling policy of that process.
The scheduling policies affect how the processes move within those lists. For SCHED_FIFO, once a process reaches the head of the list for a given priority it will stay there until it blocks or yields. For SCHED_RR, a runnable process that has exceeded its maximum time quantum will be moved to the end of the list for its static priority.

Linux SCHED_OTHER, SCHED_FIFO and SCHED_RR - differences

Can someone explain the differences between SCHED_OTHER, SCHED_FIFO and SCHED_RR?
Thanks
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.
Here is the differences between SCHED_OTHER, SCHED_FIFO and SCHED_RR based on Linux Manual (http://man7.org/linux/man-pages/man7/sched.7.html)
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 threads 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.
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.
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.

Resources