Kernel Scheduling Entity

Before jumping into the threading model. Let’s understand the kernel scheduling entity. For any kernel, the scheduling entity could be a process or thread. Some kernels are not multi-threaded, the scheduling entity will be processed. Most of the recent kernels are multi-threaded. This helped in general for someone implementing threading in userspace vs depending on kernel threading.

Different Thread Models

There are three different threading models we can see Mx1, 1x1, MxN

Image for post

Different Threading Models

Mx1 — There are two different possible operating systems, where scheduling entity for kernel is a process or a thread. Either case one would have implemented Mx1 threading model in userspace.

In this implementation, kernel is not aware of any of the user threads. a scheduled user thread maps to kernel thread/process based on the scheduling entity in kernel. So there is only one thread/process in kernel which is serving the user-space scheduler.

The main advantage of this mechanism is that it will not create much load on the kernel.

The main disadvantage with this approach is and depends on the way userspace scheduler is implemented, if a thread within the process blocks since the underlying kernel scheduling entity does not know about other user threads, it will block the entire process. So, even there are some schedulable threads in user space, it cannot schedule them.

Some implementations take care of it by creating another thread and schedule runnable user-space threads. But most of the implementations do not do this.

1x1 — In this mechanism, there is one to one mapping for the user-space threads to kernel threads. POSIX threads implemented in Linux use 1x1 model.

The main advantage of this mechanism is userspace scheduler is simplified since it completely depends on kernel scheduling.

The main disadvantage is that it generates too much load on the kernel. A process could have thousands of threads and causes kernel to create that many threads.

#golang #go #threads #golang-tutorial #threading

Why I Feel Go Threading Is Better, Though With Some Limitations
1.20 GEEK