Wednesday, 2 September 2020

User level and Kernel level threads in OS

  Process vs. Thread:

Parameter

Process

Thread

Sharing of memory and address space

It can't share same memory area i.e. address space.

Threads can share memory and files.

Time for creation

It takes more time to create a process.

It takes less time to create a thread.

Time for termination

Requires more time to complete the execution and termination of process

 Less time for termination.

Execution speed

Execution is very slow.

Execution is a very fast.

Time for switching

Requires more time to switch between two processes.

Requires less time to switch between two threads.

Communication

Implementing communication between processes is bit more difficult.

Communication between two threads is very easy to implement because threads share the same memory.

System calls

Required to communicate with each other.

System calls are not required.

Coupling

Process is loosely coupled.

Threads are tightly coupled.

Resource requirement

It requires more resources to execute.

Requires fewer resources, hence, called as lightweight process.

Parallelism

Processes are not suitable for parallel activities.

Suitable for parallel activities.


Thread Functionalities:

 

 Thread synchronization:

 

All threads within a process share the memory and files. If one thread is trying to modify one record in a file, it will affect with another threads. Hence, there is a need of mechanism for synchronizing the activities of thread.

  

User level and Kernel level threads:

 

 There are two main ways to implement threads:

1.    In user space and;

2.    In the kernel.

  

1.   User Level Threads:

 

These types of threads are loaded entirely in a user space; the kernel knows nothing about them. When Threads are managed in user’s space, each process needs its own private thread table. A thread table consists of the information of program counter, stack pointer, registers, state, etc.  The thread table is managed by runtime system. When a thread is moved to ready state or blocked state, the information needed to restart it is stored in the thread table.


User level thread


Advantages:

 

·         They allow each process to have its own scheduling algorithm.

·         Entire process is loaded in user space, so the process does not switch to the kernel mode to do thread management. This saves the overhead of two mode switches.

·         They can run on any operating system.

 

 

 Disadvantages:

·         When a user level thread executes a system call, not only the particular thread gets blocked but, all of the threads within the process are blocked.

·         In user level threads, only a single thread within a process can execute at a time. So, multiprocessing cannot be implemented.


2.   Kernel Level Threads:

 

In kernel level threads, the kernel does total work of thread movement. There is no thread table in each process.  The kernel has a thread table that keeps track of all the threads in system. When a thread wants to create a new thread or destroy any existing thread, it makes a kernel call, which takes the action.

 

The kernel’s thread table holds each thread registers, state and their information. The information is saved as with user level threads, but it is now in the kernel instead of the user space.


Kernel level thread

Advantages:

 

·         The kernel can simultaneously schedule a multiple threads from the same process on multiple processors. This means it supports multiprocessing whereas, user level threads can't support this.

·         If a thread in a process is blocked; the kernel can schedule another thread of the same process.

·         The kernel routine themselves can be multithreaded.

·         Scheduling by kernel is done on a thread bus.

 

 Disadvantages:

 

·         More cost for creating and destroying threads in the kernel.

·         Transfer of control from one thread to another within the same process requires a mode switch to the kernel.

 

 3.   Combined approaches:

 

If we combine both approaches then we get the advantages of both. Some operating systems provide this type of facility. In combined system, thread creation is done completely in user space. Best example for this approach is Sun Solaris.


No comments:

Post a Comment