CPU时间切片过程还是线程的可计划单元?

发布于 2025-02-02 18:36:30 字数 1538 浏览 3 评论 0 原文

我想澄清“ CPU时间切片的可计划单元”是否是“过程”或“线程”(内核托管线程)。我的意思是“ CPU时间切片的可计划单元”是操作系统的CPU调度程序分配CPU时间切片的单元。

根据“ 短期调度可计划单元。

“此调度程序可以先发制人,这意味着当它决定将CPU分配给另一个 Process 时,它可以强行从CPU中删除流程

。 a href =“ https://en.wikipedia.org/wiki/wiki/preemption_(computing)” rel =“ nofollow noreferrer”>时间切片

“调度程序每次运行一次,以选择下一个要运行的 Process 。”

另外,根据“ thread> thread

“ a 过程是资源单位,而 thread 调度和执行的单位”

根据“ 流程和线程

“ A 线程是操作系统分配处理器时间的基本单元。”

根据“ 是由线程调度。 CPU,内核还是两者?” Quora,

“ CPU(硬件)仅执行说明。CPU本身没有线程或调度的概念,尽管CPU中可能有支持它们的功能。

“操作系统内核(一组指令,又称软件)在CPU(硬件)上执行。操作系统内核中的调度算法选择哪个 thread 接下来执行和执行指示CPU开始执行下一个指令,其中所选的线程”。

I want to clarify whether "a schedulable unit of CPU time slice" is "process" or "thread" (kernel managed thread). What I mean by "schedulable unit of CPU time slice" is the unit which CPU scheduler of an operating system allocates CPU time slice.

According to "Short-term scheduling" in wikipedia, process is used to refer the schedulable unit.

"This scheduler can be preemptive, implying that it is capable of forcibly removing processes from a CPU when it decides to allocate that CPU to another process"

Also, according to "Time slice" in wikepedia,

"The scheduler is run once every time slice to choose the next process to run."

Also, according to "Thread" in wikepedia,

"a process is a unit of resources, while a thread is a unit of scheduling and execution"

According to "Processes and Threads" in microsoft docs,

"A thread is the basic unit to which the operating system allocates processor time."

According to "Is thread scheduling done by the CPU, kernel, or both?" in quora,

"The CPU (hardware) just carries out instructions. The CPU itself has no concept of threads or scheduling, although there may be features in the CPU that support them.

"The operating system kernel (a set of instructions, aka software) executes on the CPU (hardware). A scheduling algorithm in the kernel of the operating system chooses which thread to execute next, and directs the CPU to begin executing the next instruction in that chosen thread".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

狠疯拽 2025-02-09 18:36:30

澄清:我对“ CPU时间切片的可计划单元”的理解是“可以在给定CPU时间片期间进行安排的单元”(因为如果“计划单位”是一个时间,则该问题对我来说没有多大意义)。

基于此,请简单地将其视为软件线程(更具体地说,是由寄存器和流程信息组成的)。


操作系统调度程序在任务上运行。任务可以是线程,过程或其他不寻常的结构(例如数据流)。

现代主流操作系统主要是安排线程处理单元(通常硬件线程也称为逻辑核心)。您可以获取有关Windows Scheduler在 Microsoft文档。该文档明确指出:

线程是可以安排执行的过程中的实体

,默认调度程序,,在 task 。任务可以是线程,一组线程或过程。这样做是为了使调度程序更加通用,并且因为该调度程序是在很久以前设计的,当时处理器只有1个核心,而人们专注于过程而不是线程。此后,多核时代使应用程序使用了许多线程,以便使用可用的核心。结果,如今,通常是安排AFAIK的线程。这在著名的研究论文中进行了解释,内核(这也解释了CFS在目标处理器方面的运行方式)。

请注意,术语“过程”有时可以参考线程,因为线程有时称为“轻量级过程”,并且基本过程有时称为“重型过程”。对于重量和轻质过程(即线程和实际过程),过程甚至可能是一个通用术语。这是一个非常令人困惑的术语和对语言的滥用(例如,有时用于核心的“处理器”一词)。实际上,在特定上下文中,这通常不是问题,因为线程和过程可以互换使用(在这种情况下,人们应该使用诸如“任务”之类的通用术语)。

至于“ CPU时间切片的可计划单元”,这要复杂一些。一个简单而幼稚的答案是: thread (绝对不是单独处理)。话虽如此,线程是一个软件定义的概念(如过程)。它基本上是一个堆栈,很少的寄存器和父过程(可能有一些元信息和TLS空间)。 CPU不直接在此类数据结构上运行。例如,CPU没有线程堆栈的概念(它只是虚拟过程内存的一部分)。他们只需要由寄存器组成和过程配置组成的执行上下文(在 syultaneous multineReading (aka。 X86物理内核通常由两个可以执行软件线程的两个逻辑线程(即硬件线程)组成。

Clarification: my understanding of "a schedulable unit of CPU time slice" is "a unit that can be scheduled during a given CPU time slice" (since if "schedulable unit" would be a time then the question does not make much sense to me).

Based on this, put it shortly, "a schedulable unit of CPU time slice" for a given logical core can be seen as a software thread (more specifically its execution context composed of registers and process information).


Operating systems scheduler operates on tasks. Tasks can be threads, processes, or other unusual structure (eg. dataflows).

Modern mainstream operating system mainly schedule threads on processing units (typically hardware threads also called logical cores). You can get more information about how the Windows scheduler works in the Microsoft documentation. The documentation explicitly states:

A thread is the entity within a process that can be scheduled for execution

On Linux, the default scheduler, CFS, operates on task (ie. task_struct data structure). Tasks can be a thread, a group of threads or a process. This was done that way so to make the scheduler more generic and also because this scheduler was designed long ago, when processors had only 1 core and people focused on processes rather than thread. The multi-core era since caused applications to use a lot of threads so to use available cores. As a result, nowadays, it is generally threads that are actually scheduled AFAIK. This is explained in the famous research paper The Linux Scheduler: a Decade of Wasted Cores (which also explain a bit how the CFS operate regarding the target processor).

Note that the term "process" can sometime refer to a thread since threads are sometime called "lightweight processes" and basic processes are sometime called "heavy processes". Processes can even be a generic term for both heavy and lightweight processes (ie. threads and actual processes). This is a very confusing terminology and a misuse of language (like the term "processors" sometimes used for cores). In practice, this is often not a problem in a specific context since threads and processes may be used interchangeably though (in such a case, people should use a generic term like "tasks").

As for "a schedulable unit of CPU time slice" this is a bit more complex. A simple and naive answer is: a thread (it is definitively not processes alone). That being said, a thread is a software-defined concept (like processes). It is basically a stack, few registers, and a parent process (with possibly some meta-information and a TLS space). CPUs does not operate directly on such data structure. CPU does not have a concept of thread stack for example (it is just a section of the virtual process memory like any other). They just need an execution context which is composed of registers and a process configuration (in protected mode). For sake of simplicity, we can say that they execute threads. Mainstream modern x86 processors are very complex, and each core is often able to run multiple threads at the same time. This is called simultaneous multithreading (aka. Hyper-Threading for Intel processors). x86 physical cores are typically composed of two logical threads (ie. hardware threads) that can each execute a software threads.

初见终念 2025-02-09 18:36:30

我认为您的误解实际上是对英语单词在这种情况下的含义的误解。

时间切片为时间。也许这是一秒钟的一小部分。也许几秒钟。

线程和过程有效地是计算机将要执行的任务。 (我在这里简化。任务的概念即使在IT上下文中也具有多种含义。在现代操作系统上,一个过程实际上是共享相同虚拟内存地址空间的线程的集合。)

CPU 1 或处理器是将 run a(本机)线程的硬件。典型的计算机将具有多个CPU。但是,计算机中的每个CPU一次只能在一次上运行一个线程

因此,操作系统需要安排它知道要在特定CPU上运行的每个线程。操作系统的一部分称为调度程序

如果要运行的线程多于运行它们,则调度程序通常会将线程安排到CPU固定的时间段内;即时间切片。当线程的时间切片经过时,调度程序将暂停并将其放回队列,然后安排其他线程以在CPU上运行。

隐喻是我们正在“切片” CPU上的可用计算时间,并在需要它的线程之间共享切片。

1-关于“ CPU”的实际含义存在分歧。我认为它是指通常称为“核心”的内容。英特尔混乱地介绍了营销术语 2 “超线程”,该功能是指“核心”的物理硬件可以作为两个独立的指令执行者运行的功能。但是,在超线程模式下,操作系统调度程序通常会 对待超线程,就像它们是不同的核心一样,因此这与您的问题无关。
2-超线程背后的实际概念可以追溯到1960年代,然后英特尔甚至作为一家公司就存在。参见 https://en.wikipedia.org/wiki/wiki/barrel_processor


因此,您的问题的答案:

我想澄清“ CPU时间切片的可计划单元”是“过程”还是“线程”(内核托管线程)。

时间片是CPU时间的可计划单元。

时间切片既不是过程也不是线程。确实,这甚至没有意义,因为“过程”和“线程”不是时间。


我同意@Solomon Slow的评论。维基百科不是权威的。但是真正的问题是,随着时间的流逝,不同的人编写和编辑了不同的页面,并且他们经常不一致地使用术语。

我的建议是查找并阅读一本(现代)操作系统设计和建筑的好教科书。写得很好的教科书应该在使用术语方面是自以为是的。

I think your misunderstanding is actually a misunderstanding of what the English words mean in this context.

A time slice is a period of time. Maybe it is a fraction of a second. Maybe a few seconds.

Threads and processes are effectively tasks that the computer is going to perform. (I am simplifying here. The notion of a task has multiple meanings, even in the IT context. And on a modern OS, a process is actually a collection of threads that share the same virtual memory address space.)

The CPU1 or processor is hardware that will run a (native) thread. A typical computer will have multiple CPUs. However, each CPU in a computer can only run one thread at a time.

The operating system therefore needs to schedule each of the threads it knows about to run on a specific CPU. The part of the operating system that does this is called the scheduler.

If there are more threads to run than CPUs to run them, the scheduler will typically schedule a thread to a CPU for a fixed period of time; i.e a time slice. When the thread's time slice has elapsed, the scheduler will suspend it and put it back into the queue, and then schedule a different thread to run on the CPU.

The metaphor is that we are "slicing up" the available compute time on the CPUs and sharing the slices between the threads that need it.

1 - There is some disagreement over what "CPU" actually means. I am taking the view that it refers to what it commonly called a "core". Intel confusingly introduced the marketing term2 "hyperthread" which refers to a feature in which the physical hardware of a "core" can run as two independent instruction executors. However, in hyperthread mode, the OS scheduler will typically treat the hyperthreads as if they are distinct cores, so this is not pertinent to your question.
2 - The actual concept behind hyperthreads goes back to the 1960s, before Intel even existed as a company; see https://en.wikipedia.org/wiki/Barrel_processor.


So the answer to your question:

I want to clarify whether "a schedulable unit of CPU time slice" is "process" or "thread" (kernel managed thread).

A time slice is a schedulable unit of CPU time.

A time slice is neither a process or a thread. Indeed that doesn't even make sense because "process" and "thread" are not time.


I concur with @Solomon Slow's comment. Wikipedia is not authoritative. But the real problem is that different pages are written and edited by different people over time, and they often use IT terminology inconsistently.

My advice would be to find and read One good textbook on (modern) operating system design and architecture. A well-written text book should be self-consistent in its use of terminology.

倾城°AllureLove 2025-02-09 18:36:30

时间切片是时间单位,例如,在传统的Linux内核中使用Hz = 100(每秒100个计时器中断)。在CPU核心上运行了这么长时间的任务后,内核重新对该CPU进行了控制,并调用 schedule()来决定下一步应运行的CPU核心任务;它已经运行的任务或另一个任务。

如果外部中断出现,调度程序也可以更早地运行,尤其是在当前时间的末端附近:如果现在有更高的优先级任务,现在正在等待CPU,例如等待I/O或 Sleep之后()系统调用结束了,OS将其安排在此核心上是有意义的,而不是完成中断CPU结合任务的时间单板。


任务是调度程序必须从中选择的东西的有用词。不暗示过程中的单独过程或线程,也没有留出空间来完成诸如Linux中断处理程序“下半部”之类的内核任务,而不是线程或过程。

一个过程的每个线程都需要分别安排在CPU核心上执行(如果没有阻止)。

您发现有关调度过程的文章使用的是简化的假设,即每个过程都是单线程。

或者他们假设一个1:n线程模型,其中OS仅知道整个过程的一个任务,并且在用户空间中完成了多线程,” 绿色线程“而不是本机线程。如今,大多数主流OS都使用1:1线程模型,其中每个C ++或Java线程都是OS可见的单独可计划任务,尽管N:m型号在您执行了多个OSSSEDED任务的地方可能是可能的,但不是那么多您有高级语言线程。

A time slice is a unit of time, for example 10 ms in a traditional Linux kernel built with HZ=100 (100 timer interrupts per second). After a task has been running for that long on a CPU core, the kernel regains control on that CPU and calls schedule() to decide what task this CPU core should run next; the task it was already running, or a different task.

The scheduler can also run earlier if an external interrupt comes in, especially near the end of the current timeslice: if there's a higher-priority task that's now waiting for a CPU, e.g. after waiting for I/O or after a sleep() system call ended, it makes sense for the OS to schedule it onto this core, instead of finishing the time-slice of whatever CPU-bound task was interrupted.


Task is a useful word for the things the scheduler has to pick from. Without implying separate process or threads within a process, and also leaving room for kernel tasks like a Linux interrupt handler "bottom half" that aren't threads or processes.

Every thread of a process needs to get scheduled separately to execute on a CPU core (if it's not blocked).

The articles you found about scheduling processes is using the simplifying assumption that each process is single-threaded.

Or they're assuming a 1:n threading model, where the OS is only aware of one task for the whole process, and multithreading is done in user-space, "green threads" instead of native threads. Most mainstream OSes these days use a 1:1 threading model, where every C++ or Java thread is a separately schedulable task visible to the OS, although n:m models are possible where you have multiple OS-scheduled tasks, but not as many as you have high-level-language threads.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文