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.

  1. Kernel mode is a privileged state in which the process has complete control over all resources, including hardware, kernel data, and OS kernel code.
  2. 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:

  1. In the user space, the corresponding code and data structures are stored.
  2. The threads are entirely handled by the run-time system, with no knowledge of the kernel.
  3. 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: -

  1. One-to-One
  2. Many-to-One
  3. Many-to-Many
  4. Two-level

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.

One-to-One

In this one-to-one type of model:

Implement Threads in User Space
  1. Every user-level thread in user space runs on a different kernel-level thread.
  2. The kernel should give a system call to make a new kernel thread.
  3. Every user-level thread is mapped with a different kernel-level thread.

Many-to-One

In this many-to-one type of model:

Implement Threads in User Space
  1. The user-level threads may only execute one user-level thread because only a single kernel-level thread is associated with the process.
  2. All user-level threads may run on the same kernel thread.
  3. Every user thread makes a system call to make a new kernel thread.
  4. At the kernel level, several threads in the user space are mapped to a single thread.

Many-to-Many

In this many-to-many type of model:

Implement Threads in User Space
  1. The process is assigned m number of kernel-level threads to run n number of user-level threads.

Two-Level

In this type of model:

Implement Threads in User Space
  1. It is a mixed version of the one-to-one and many-to-many models.
  2. 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:

Advantages

  1. It may be executed on the system that's the operating system does not support the kernel-level threads.
  2. The overall process is quick and effective because there is no requirement for a system call.
  3. The organization is straightforward because the threads are produced, switched, and controlled without interfering with the kernel.

Disadvantages

  1. The performance may be inconsistent.
  2. It has poor scheduling, such as allocating a process to an idle thread or blocking a process due to a thread holding a lock.