How to implement thread in kernel space?


Kernel is a part of the operating system. It interacts directly with the hardware of the computer with the help of a device that is built into the kernel.

Functions of Kernel

The functions of the kernel are as follows −

  • Memory management

  • Controlling access to the computer.

  • Maintaining the file system.

  • Handling interrupts

  • Handling errors

  • Performing input and output services.

  • Kernel allocates the resources of the computer of users.

  • The kernel is the most important part of the Architecture of Unix OS.

Generally, program executes in two modes, which are as follows −

  • User mode − Cannot access any hardware resources, which perform only the user operations.

  • Kernel mode − Can access hardware resources like RAM, Printer.

The processor in a computer switches between the two modes depending upon what types of code are running on the processor.

A process running in the user mode cannot access the virtual addresses that are reserved for the operating system. The system is in user mode when the operating system is running a user application such as handling a text editor.

The transition from the user mode to kernel mode occurs, when the application requests the help of the operating system or an interrupt or a system call occurs. The mode bit is usually set to 1 in the user mode.

When a program needs any hardware resources, it needs to make a call to the kernel. Through system call the program will switch to the kernel. It will happen with the hardware resources in kernel mode. After compilation of the work of hardware resources it will again come back to user mode. When it will require hardware then only it will come to kernel mode.

Given below is the structure of kernel −

Implementing thread in Kernel space

Now, let us see how to implement thread in kernel space.

Step 1 − Kernel manages the thread by maintaining thread table in the system to keep tracking all threads.

Step 2 − Whenever a thread wants to create a new thread or to destroy an existing thread, it makes a kernel call. It does not do creation or destruction by updating the kernel thread table.

Step 3 − This table holds every thread’s registers, state, and other information and also maintains the traditional process table to keep track of processes.

Given below is an image depicting the implementation of thread in kernel space −

Advantages

The advantages of implementing thread in kernel space are as follows −

  • Kernel is having complete knowledge of all threads, Scheduler decides to give more time to a process which has a large number of threads than a small number of threads.

  • Kernel-level threads are good for applications which frequently block.

Disadvantages

The disadvantages of implementing thread in kernel space are as follows −

  • These types of threads are slow and inefficient. Because, thread operations are hundreds of times slower than user-level threads.

  • Since kernel is going to manage and schedule threads as well as processes. It requires a full thread control block for each thread to maintain information about threads. This result in significant overhead and increases in kernel complexity.

Updated on: 30-Nov-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements