Linux 中的实时调度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9374653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:42:43  来源:igfitidea点击:

real time scheduling in Linux

linuxkernelschedulingfiforound-robin

提问by theB

This morning I read about Linux real time scheduling. As per the book 'Linux system programming by Robert Love', there are two main scheduling there. One is SCHED_FIFO, fifo and the second is SCHED_RR, the round robin. And I understood how a fifo and a rr algorithm works. But as we have the system call,

今天早上我读到了 Linux 实时调度。根据 Robert Love 的《Linux 系统编程》一书,那里有两个主要的调度程序。一个是 SCHED_FIFO,fifo,第二个是 SCHED_RR,轮循机制。我了解了 fifo 和 rr 算法的工作原理。但是当我们有系统调用时

sched_setscheduler (pid_t pid, int policy, const struct sched_parem *sp)

we can explicitly set the scheduling policy for our process. So in some case, two process running by root, can have different scheduling policy. As one process having SCHED_FIFO and another having SCHED_RR and with same priority. In that case, which process will be selected first? the FIFO classed process or the RR classed process? Why?

我们可以为我们的进程显式设置调度策略。所以在某些情况下,两个以 root 身份运行的进程,可以有不同的调度策略。作为一个进程具有 SCHED_FIFO 和另一个具有 SCHED_RR 并具有相同的优先级。在这种情况下,将首先选择哪个进程?FIFO 类过程还是 RR 类过程?为什么?

Consider this case. There are three process A,B,C. All are having same priority. A and B are RR classed processes and C is FIFO classed one. A and B are runnable (so both are running alternatively in some time intervel). And currently A is running. Now C becomes runnable. In this case, whether

考虑这个案例。有A、B、C三个过程。所有人都具有相同的优先级。A 和 B 是 RR 类进程,C 是 FIFO 一类。A 和 B 是可运行的(因此两者都在某个时间间隔内交替运行)。目前 A 正在运行。现在 C 变得可运行了。在这种情况下,无论

1. A will preempt for C, or
2. A will run until its timeslice goes zero and let C run. Or
3. A will run until its timeslice goes zero and let B run.
   a) here after B runs till its timeslice becomes zero and let C run or
   b) after B runs till its timeslice becomes zero and let A run again (then C will starve untill A and B finishes)

回答by Maxim Egorushkin

man sched_setschedulerexplains these scheduling policies in detail.

man sched_setscheduler详细解释了这些调度策略。

In this particular case because the two real-time processes have the same priority none of them will preempt the other. A SCHED_FIFOprocess runs until it blocks itself, SCHED_RRprocess runs until it blocks itself or its time quantum expires.

在这种特殊情况下,因为两个实时进程具有相同的优先级,它们中的任何一个都不会抢占另一个。一个SCHED_FIFO进程运行直到它自己阻塞,SCHED_RR进程运行直到它阻塞自己或它的时间片到期。

回答by yves Baumes

My understanding of the two different classes is that a process SCHED_FIFO is never pre-empted by the kernel. Even if another "SCHED_FIFO" class process is waiting its turn...

我对这两个不同类的理解是,进程 SCHED_FIFO 永远不会被内核抢占。即使另一个“SCHED_FIFO”类进程正在等待轮到它......

While SCHED_RR policy shares the cpu ressources a little bit more. The scheduler will let the SCHED_RR process run for a quanta of time, then pre-empt it only to let turn another SCHED_RR process. That is exactly Round Robin.

虽然 SCHED_RR 策略更多地共享 CPU 资源。调度程序会让 SCHED_RR 进程运行一段时间,然后抢占它只是为了让另一个 SCHED_RR 进程运行。这正是轮循机制。

SCHED_FIFO is "stronger" in the sense that if a SCHED_FIFO process never yield() to the kernel or invoke a system call on a single core device, then all your other Real time processes may never run.

SCHED_FIFO 是“更强”的,因为如果 SCHED_FIFO 进程从不向内核 yield() 或在单个核心设备上调用系统调用,那么所有其他实时进程可能永远不会运行。

回答by svenfx

In realtime scheduling, FIFO and RR do not have exactly the same meaning they have in non-realtime scheduling. Processes are always selected in a FIFO- manner, however, the time quantum for SCHED_FIFO is not limited unlike the time quantum for SCHED_RR.

在实时调度中,FIFO 和 RR 在非实时调度中的含义并不完全相同。进程总是以 FIFO 方式选择,但是,与 SCHED_RR 的时间量不同,SCHED_FIFO 的时间量不受限制。

SCHED_FIFO processes do not preempt SCHED_RR processes of the same priority.

SCHED_FIFO 进程不会抢占相同优先级的 SCHED_RR 进程。

sched_setscheduler(2) - Linux man page

sched_setscheduler(2) - Linux 手册页

...

...

"A process's scheduling policy determines where it will be inserted into the list of processes with equal static priority and how it will move inside this list. All scheduling is preemptive: if a process with a higher static priority becomes ready to run, the currently running process will be preempted and returned to the wait list for its static priority level. The scheduling policy only determines the ordering within the list of runnable processes with equal static priority."

“进程的调度策略决定了它将被插入到具有相同静态优先级的进程列表中的哪个位置,以及它将如何在该列表中移动。所有调度都是抢占式的:如果具有更高静态优先级的进程准备运行,则当前正在运行的进程进程将被抢占并返回到其静态优先级的等待列表中。调度策略仅确定具有相同静态优先级的可运行进程列表中的顺序。”

...

...

"A SCHED_FIFO process runs until either it is blocked by an I/O request, it is preempted by a higher priority process, or it calls sched_yield(2)."

“一个 SCHED_FIFO 进程会一直运行,直到它被 I/O 请求阻塞,被更高优先级的进程抢占,或者它调用 sched_yield(2)。”

...

...

"When a SCHED_FIFO process becomes runnable, it will be inserted at the end of the list for its priority."

“当一个 SCHED_FIFO 进程变得可运行时,它会被插入到列表的末尾以获得它的优先级。”

...

...

"SCHED_RR: Round Robin scheduling

"SCHED_RR:循环调度

SCHED_RR is a simple enhancement of SCHED_FIFO. Everything described above for SCHED_FIFO also applies to SCHED_RR, except that each process is only allowed to run for a maximum time quantum. If a SCHED_RR process 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 process that has been preempted by a higher priority process and subsequently resumes execution as a running process will complete the unexpired portion of its round robin time quantum."

SCHED_RR 是 SCHED_FIFO 的简单增强。上面针对 SCHED_FIFO 描述的所有内容也适用于 SCHED_RR,除了每个进程只允许运行一个最大时间片。如果 SCHED_RR 进程已运行的时间段等于或长于时间片,则它将以其优先级放在列表的末尾。已被更高优先级进程抢占并随后作为正在运行的进程恢复执行的 SCHED_RR 进程将完成其循环时间量的未到期部分。”

回答by annabel1832

According to the man page, I think 1 is the answer. A, B are RR policy, C is FIFO policy. Since RR is also an enhancement FIFO, all of them are FIFO class.

根据手册页,我认为 1 是答案。A、B 是 RR 策略,C 是 FIFO 策略。由于RR也是一个增强FIFO,所以都是FIFO类。

Since all of them have the same priority, and man page say " A call to sched_setscheduler() or sched_setparam(2) will put the SCHED_FIFO (or SCHED_RR) process identified by pid at the start of the list if it was runnable. As a consequence, it may preempt the currently running process if it has the same priority. (POSIX.1-2001 specifies that the process should go to the end of the list.)"

由于它们都具有相同的优先级,并且手册页说“调用 sched_setscheduler() 或 sched_setparam(2) 会将 pid 标识的 SCHED_FIFO(或 SCHED_RR)进程放在列表的开头,如果它是可运行的。作为结果,如果它具有相同的优先级,它可能会抢占当前正在运行的进程。(POSIX.1-2001 指定该进程应该转到列表的末尾。)”

Once calling sched_setscheduler to set the policy of C as FIFO, C will preempt A.

一旦调用 sched_setscheduler 将 C 的策略设置为 FIFO,C 将抢占 A。