Real time schedulers in Linux - 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.)

Related

Clarification of process scheduling, thread scheduling, CPU scheduling in preemptive multitasking operating system

I want to clarify process scheduling, thread scheduling concept, which sometimes even being called CPU scheduling or just scheduling as if it is synonym, due to inconsistent terminology. I mean in theoretical, conceptual perspective.
Condition is preemptive multitasking operating system, which thread is the entity being scheduled for execution (ex. Windows), under multicore environment.
Scheduling algorithm is round robin if necessary.
Last time I've been taught to get authoritative sources, not wikipedia. So I've read Operating System Concepts 10th edition Abraham Silberschatz, Modern Operating Systems fourth edition Andrew S. Tanenbaum.
But my question of "When they say process scheduling and thread scheduling, are they same except that process scheduling unit is process and thread scheduling unit is thread?" hasn't been solved. In other words, "Are process scheduling and thread scheduling both task scheduling, which just scheduling unit differs?"
I know that process scheduling is mostly "written at a time, not very long ago, when threads didn't exist" because someone taught me.
Below is my understanding so far.
On modern operating systems it is kernel-level threads—not processes—that are in fact being scheduled by the operating system. (p199, Operating System Concepts 10th edition)
Many of the same issues that apply to process scheduling also apply to thread scheduling, although some are different. When the kernel manages threads, scheduling is usually done per thread, with little or no regard to which process the thread belongs. (p149, Modern Operating Systems fourth edition)
Comparison in table.
Process management
Process management
Process scheduling (of old days)
Thread scheduling
Processes with single thread
Scheduling decisions are made strictly on a thread basis, no consideration is given to what process the thread belongs to. (thread granularity) Competition for the CPU with SCS scheduling takes place among all threads in the system (System contention scope)
Selecting available processes, put selected processes to ready queue (Step 1 : scheduling queue) (Long term scheduling)
Not mentioned. But it seems implicitly "selecting available threads, put selected threads to ready queue" (step 1) (long term scheduling)
Selecting processes from ready queue, allocate a CPU core each (Step 2 : CPU scheduling) (short term scheduling)
Decide threads from ready queue, schedule threads onto CPU (system contention scope) (step 2, CPU scheduling) (short term scheduling)
Removal and restoration of processes (swap in/swap out)(intermediate form)(medium term scheduling)
Not mentioned.
Also, I've seen somewhere stating CPU scheduling as synonym as process scheduling or thread scheduling. However, isn't CPU scheduling subset of process scheduling or thread scheduling?
When a computer is multiprogrammed, it frequently has multiple processes or threads competing for the CPU at the same time. This situation occurs whenever two or more of them are simultaneously in the ready state. If only one CPU is available, a choice has to be made which process to run next. The part of the operating system that makes the choice is called the scheduler, and the algorithm it uses is called the scheduling algorithm. (p149, Modern Operating Systems fourth edition)
"The scheduler" sounds like there is no other scheduler but one, which process scheduler. However, isn't there other schedulers like I/O scheduler?
Thank you for reading.

How linux kernel scheduling works on multi core processor?

I recently started reading Robert Love's book "Linux Kernel Development 3rd edition" and dived into the scheduler part, which left me with lots of questions.
So first off, I understood there are 2 cases where the scheduler changes the task that currently runs (Correct me if I'm not precise), either by a task that willingly requested to re-schedule since it blocks on some I/O or sleeps, or a timer interrupt that caused the cpu to jump to scheduler code and preempt the current task if it's interruptible.
Does each core in a multicore processor get the interrupt that is related to re-scheduling? Do they each have a different timer, or say there is one interrupt that in some type of algorithm picks a specific core to handle it each time?
Assuming not only one core re-schedules each interrupt (since then I would imagine it might take a while to swap processes on all of the cores), what happens if two cores re-schedule at the same time? Because, I assume that when you run the schedule function the task-list must be locked, and then I'd imagine a few cores re-scheduling their current task simultaneously resulting in only one core actually doing scheduling work and all of the other cores waiting on the task-list lock.
Not only that the task-list lock is required to touch the actual task-list and say change tasks state or run-queue order, what if one core that schedules currently calculates which task should be run next and meanwhile another core finishes scheduling successfully which causes the first core calculation to be totally mistaken since the successful re-scheduling just heavily changed the system state?
I understood that in linux priority is divided to "nice value" which is -20 to 19 (higher means less priority and more "nice") and real-time priority (0-99). real-time priority values matter only for a couple of scheduling policies, and each process can register to a different scheduling policy.
Does the real-time policies always beat processes that are not registered to real-time policies? Meaning if I run a real-time process I will never get to execute normal processes? How are the "nice" values of normal processes and real-time priority values of real-time processes work together in the scheduler algorithm?

Scheduler for Linux kernel threads

Linux includes a few privileged processes called kernel threads. Is there any scheduler which runs/suspends them? If yes, is this scheduler the same as the system scheduler (I mean the one to schedule the whole system processes)?
The Linux scheduler is scheduling tasks. These can be
kernel threads (e.g. kswapd), or
single-threaded processes (e.g. bash), or
individual threads of a multi-threaded process (e.g. some browsers or servers)
The many threads of a multi-threaded process are tasks sharing a common address space (and other things, e.g. file descriptors).
AFAIK, the scheduler does not separate kernel threads from other tasks. But the scheduler do take into account scheduling policies (sched_setscheduler(2)) and priorities (setpriority(2)) (For most kernel threads, the priority is often very high). See sched(7)
Yes ! Let me clarify the system scheduler part here.
Every task is associated with a task_struct which contains the details of each task say its pid, its name, when it recently started, priority etc etc.http://lxr.free-electrons.com/source/include/linux/sched.h#L1224
Typically depending on the priority of the task either Fair scheduler or Real time scheduler kicks in and these co exist. Just to keep it simple and not to go into details, these are different scheduler algorithms that cater to different type of tasks.
Now Kernel threads also have an associated task_struct and as #Basile Starynkevitch pointed a couple of KPI's, we can use sched_setparam KPI's to modify the sched params and change the scheduler to which the task belongs to depening on what they are about to do.

Threads - Pre-emptive Mutitasking vs. Priorities

In my understanding, pre-emptive multitasking is the case when a time-slice (e.g. a 1 millisecond time-slice) makes the scheduler (of the OS) pass (to the CPU) one thread to the CPU for a particular span of time (1 millisecond in this example) and then switches to another thread (executes it for 1 millisecond and then switches back to the first thread and so on - assuming that there are only two threads, for simplicity).
Reference: https://www.youtube.com/watch?v=hsERPf9k54U
In contrast to pre-emptive multi-tasking is the concept of priorities - the OS sets priorities of applications in numbers, e.g. 1 to 39 etc., on whatever basis - that is not the concern for now.
And the advantage of this is that if one application hangs, the time-slicer simply goes back to the other thread (let's say this thread belongs to a different application, and the first application has hanged) and continues to work normally. Then you can close the hanged app.
Reference: https://www.youtube.com/watch?v=hsERPf9k54U
Now I don't think this is particularly an advantage of this kind of multitasking. It should be the same thing in the preemptive multitasking, isn't it?
Thank you in advance.
Preemptive, multitasking and priority (scheduling) are different aspects of the OS concepts.
Preemptive, in the context of process scheduling, is a strategy in which the OS can preempt (take) the resources allocated for a process whenever it (the OS) need. In contrast, non-preemptive scheduling strategy cannot preempt (take back) the resources until the process finishes using them and release them.
A priority scheduling algorithm can be implemented with preemptive or non-preemptive strategy.

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