Why must User Threads be mapped to Kernel Thread?
A kernel thread is an actual thread that is capable of being scheduled and executed by the processor. User programs can make their own schedules too. However, these aren't kernel threads. Each user thread can't actually run on its own that's why it needs a kernel-level thread.
So for a user-level thread to make progress, the user program has to have its scheduler take a user-level thread and run it on a kernel-level thread, and then we have different mappings to achieve this. Before going further, you need to know briefly about user thread, kernel thread, and their mapping.
What is Thread?
A thread is the basic atomic unit of CPU utilization in computer systems. Mostly we consider a process to be the basic unit for CPU utilization, but a process may be further divided into multiple threads of execution. A process can be divided into different tasks or units that can execute independently, and these units are called threads.
A thread is a lightweight process that a scheduler can manage independently. It improves the application performance using parallelism. Thread shares information like data segment, code segment files, etc., with its peer threads while it contains its own registers, stack, counter, etc. The two main types of threads created in a computer system are:
Threads can be created in the address space of the process itself, i.e., inside the process kernel involvement or without kernel involvement.
What are User Threads?
User threads are the threads created by the user with help from a user library and are visible just to the creating process and its run time environment, and the kernel has no idea about the creation of these threads. User threads just stay in the address space of the creating process and are run and managed by the creating process without kernel involvement.
User-level threads are small and much faster than kernel-level threads. They are represented by a program counter (PC), stack, registers, and a small process control block.
Some of the advantages of user-level threads are as follows:
User-level threads have the following disadvantages, such as:
What are Kernel Threads?
On the other hand, kernel-level threads are handled by the operating system directly, and the kernel does thread management. The kernel manages the context information for the process and the process threads. Because of this, kernel-level threads are slower than user-level threads.
Some of the advantages of kernel-level threads are as follows:
Kernel level threads are also having the following disadvantages, such as:
How User Thread mapped to Kernel Thread
User threads are mapped to kernel threads by the threads library. The way this mapping is done is called the thread model.
The mapping of user threads to kernel threads is done using virtual processors. A virtual processor (VP) is a library entity that is usually implicit. The VP is a kernel thread or a structure bound to a kernel thread in the library. For a user thread, the VP behaves like a CPU.
There are the following possible thread models, and all models are used to map user-level threads to kernel-level threads.
In both the M: N model and the two-level model, there must be some way for the kernel to communicate with the user level thread manager to maintain an appropriate number of kernel threads allocated to the process. This mechanism is called scheduler activations.
The kernel provides the application with a set of kernel threads (virtual processors), and then the application has complete control over what threads to run on each of the kernel threads. The kernel controls the number of kernel threads in the set in response to the competing demands of different processes in the system.
The kernel notifies the user-level thread manager of important kernel events using upcalls from the kernel to the user-level thread manager.
For example, let's study an example of how scheduler activations can be used.
Why must User Threads be mapped to a Kernel Thread?
User threads need to be mapped to kernel threads because it's the kernel that schedules the thread for execution onto the CPU, and for that, it must know about the thread that it is scheduling.
All the user threads created by a process are executed over the same kernel-level thread appointed to the whole process. Whenever it's the turn for the specified process to execute on the CPU, its kernel thread is scheduled onto the CPU. All the other user threads inside the process have to be mapped one by one onto the kernel thread appointed to the creating process if they have to be executed.
Since all of them are controlled by the creating process itself, the user threads must be mapped onto the appointed kernel thread one by one and, therefore, executed.