Open In App

Implement Threads in User Space

Last Updated : 19 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite : Difference between Process and Thread, Difference between User Level thread and Kernel Level thread

Introduction : 
In an operating system, there are a number of programs, and both the operating system and the user share the hardware and software resources of the computer system. In order to ensure that an incorrect program doesn’t harm the other programs or the operating system, it distinguishes between the user-defined code and the operating-system code. 
The approach is, only the task that is executing on behalf of the operating system is provided with hardware support, known as the kernel mode, while the task executing on behalf of a user application is not, known as a user mode.

  • Kernel mode is a privileged mode, where the process has access to all the resources, such as hardware, kernel data, OS kernel code.
  • The basic difference is that, in kernel mode, the kernel can access the hardware directly, but this is not the case with the user mode. However, when a user application requests a service from the operating system, the system must make a transition from user to kernel mode. 

A better way to understand the concept is by looking at the following diagram. 

A thread is an execution unit, which is part of a process. It shares the process’s resources. It is scheduled by the scheduler. There are two ways to implement a thread, they’re either in user space or in the Kernel.

Threads can be implemented in user space, without the support of the kernel. The following things happen if we  implement a thread in user space –

  1. The corresponding code and the data structures used are stored in the user space.
  2. If an API is invoked, it results in a local system call in user space, rather than a system call.
  3. The threads are managed entirely by the run-time system, without the kernel knowing anything about it.

User-level thread models :
A thread in the userspace can be implemented using any of the following four methods:-

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

In each of the above models, the thread at userspace is mapped with that of a kernel thread or rather a virtual processor.

1. One-to-one :
In this type of model –

  • Each user-level thread is mapped with a separate kernel-level thread.
  • Every thread in userspace executes on a separate kernel-level thread.
  • Here, the kernel must provide a system call to create a new kernel thread.

2. Many-to-one :
In this type of model –

  • Multiple threads at user space are mapped with a single thread in the kernel level.
  • In other words, all user-level threads execute on the same kernel thread.
  • Since there is only one kernel thread, only one user-level thread runs at a time.
  • Each user thread makes a system call to create a new kernel thread.

3. Many-to-many :
In this type of model –

  • n number of threads at the user level is mapped to execute on m number of kernel-level threads.

4. Two-level :
In this type of model –

  • This is a hybrid version of the many-to-many and one-to-one models.
  • Only a limited set of user threads are allowed to be bound to a single kernel thread.

Advantages :
We have seen what threads are, how user-level threads are different from kernel level and different types of user threads. But what is the motivation behind using a user-level thread? Let us look at some of its advantages:-

  1. Since there is no need for a system call, hence, the overall process is fast as well as efficient.
  2. The simple organization, since the threads are created, switched and managed without intervening in the kernel.
  3. It can be implemented on a system, whose OS doesn’t support kernel-level threads.

Disadvantages :
To every advantage, there comes a corresponding disadvantages –

  1. Poor scheduling, like allocating a process with an idle thread or block a process, because a thread is holding a lock.
  2. Performance might be inconsistent.

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads