如果默认时间片是 100 毫秒,Linux 进程如何每秒产生 60 次图形输出?

How do Linux processes produce graphical output 60 times per second if the default time slice is 100ms?

我根本不明白 multitasking works in Linux (and probably also in general). If I understand correctly, each time a process wants to change its output to the screen, it needs to do some computations and send the data. But if I understand correctly, processes can hog the CPU for up to 100ms before being pre-empted under the default settings of most Linux distributions。这似乎排除了进程被足够频繁地解锁以能够以 60Hz 刷新屏幕的可能性。鉴于此,我想我可能对 Linux 如何管理其稀缺的 CPU 时间 and/or 关于进程如何将数据发送到 I/O 设备存在大量基本误解.

问题。这是怎么回事?

理论上;它可能比您想象的要糟糕得多——如果有 100 个其他进程,并且每个进程都消耗了它们允许的最大时间片(100 毫秒);那么游戏进程可能需要 100 * 100 毫秒 = 10 秒才能再次获得 CPU 时间。

但是:

a) 最大时间片长度是可配置的(在编译内核时)并且(对于桌面系统)更有可能是 10 ms

b) 消耗最大时间片的进程极为罕见。如果给一个进程最多 10 毫秒但在 1 毫秒后阻塞(因为它必须等待磁盘 IO 或网络或互斥锁或...)那么该进程将只使用 1 毫秒

c) 对于现代计算机来说,极有可能存在多个 CPUs

d) 还有其他调度策略(参见 http://man7.org/linux/man-pages/man7/sched.7.html)和任务优先级("nice")和 "cgroups"。所有这些都可用于确保特殊进程(例如游戏)在其他进程 and/or 比其他进程获得更多 CPU 时间之前获得 CPU 时间。

e) 大多数玩游戏的人根本没有其他进程同时消耗 CPU 时间 - 他们可能有多个进程不消耗任何 CPU 时间,但赢了'有多个进程试图消耗所有 CPU 时间的 100%。

您似乎混淆了不同的调度策略。

在Linux中,有several scheduling policies决定了不同的时间片。 100 毫秒默认时间片 仅适用于 SCHED_RR 策略 ,用于实时进程。实际上,在 SCHED_RR.

下没有正常进程 运行s

SCHED_OTHER 下的正常进程 运行,这是默认的调度程序策略。在这个时间表下,时间片是在 运行 时间动态确定的,并且要低得多。默认情况下,它可以介于 0.75 毫秒到 6 毫秒之间。您可以在 kernel/sched/fair.c 中看到这些默认值(以纳秒为单位)分别定义为 sysctl_sched_min_granularitysysctl_sched_latency。您可以通过阅读 /proc/sys/kernel/sched_min_granularity_ns/proc/sys/kernel/sched_latency_ns.

获得系统上的实际值

您可以了解有关 Linux 内核 CFS 调度程序的更多信息 here (or here 以获取更多文档)。