Implement Threads in User Space
In this article, you will learn the implement threads in user space with their advantages and disadvantages.
An operating system is a collection of programs that share the hardware and software resources of the computer system with the user. It distinguishes between user-defined and operating-system code to ensure that an improper application doesn't cause harm to other programs or the operating system. The concept is that only the tasks running on the operating system receive hardware support, which is referred to as kernel mode. In contrast, the task executing on a user program is not known as user mode.
- Kernel mode is a privileged state in which the process has complete control over all resources, including hardware, kernel data, and OS kernel code.
- The main difference is that the kernel has direct access to the hardware in kernel mode, whereas there is no case in the user mode. However, when a user program asks the operating system for a service, the system must go from user to kernel mode.
A thread is a type of execution unit which is a part of a process. It distributes the process's resources. The scheduler schedules it. There are two methods for implementing a thread: user space or kernel space.
Threads may be implemented in user space without the kernel's assistance. When we implement a thread in the user space, the following things happen:
- In the user space, the corresponding code and data structures are stored.
- The threads are entirely handled by the run-time system, with no knowledge of the kernel.
- When an API is called, a user space local system call is made instead of a system call.
User-level thread models
User-level thread in the user space may be implemented using any of the following four methods: -
User-level threads are mapped to kernel-level threads in all models. The kernel thread is equivalent to a process in a non-threaded system. The kernel thread is the execution unit that the kernel schedules to run on the CPU. Instead of kernel thread, the term virtual processor is frequently used.
In this one-to-one type of model:
- Every user-level thread in user space runs on a different kernel-level thread.
- The kernel should give a system call to make a new kernel thread.
- Every user-level thread is mapped with a different kernel-level thread.
In this many-to-one type of model:
- The user-level threads may only execute one user-level thread because only a single kernel-level thread is associated with the process.
- All user-level threads may run on the same kernel thread.
- Every user thread makes a system call to make a new kernel thread.
- At the kernel level, several threads in the user space are mapped to a single thread.
In this many-to-many type of model:
- The process is assigned m number of kernel-level threads to run n number of user-level threads.
In this type of model:
- It is a mixed version of the one-to-one and many-to-many models.
- A single kernel thread may only be connected to a limited number of user threads.
Advantages and disadvantages of implementing threads in User Space
Here, you will learn the advantages and disadvantages of threads in user space. Some of the advantages and disadvantages of threads in user space are as follows:
- It may be executed on the system that's the operating system does not support the kernel-level threads.
- The overall process is quick and effective because there is no requirement for a system call.
- The organization is straightforward because the threads are produced, switched, and controlled without interfering with the kernel.
- The performance may be inconsistent.
- It has poor scheduling, such as allocating a process to an idle thread or blocking a process due to a thread holding a lock.