Home > Software design >  Is a schedulable unit of CPU time slice process or thread?
Is a schedulable unit of CPU time slice process or thread?

Time:05-30

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".

CodePudding user response:

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 CPU is hardware that will run a 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 needs to schedule each thread to run on a 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: the 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.


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.

CodePudding user response:

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.

CodePudding user response:

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.

  • Related