CSE247 on YouTube

Thread in Distributed System

 






A thread is a lightweight of process and is a basic unit of CPU utilization which consists of a program counter, a stack, and a set of registers.

A thread is also known as lightweight process. The idea is to achieve parallelism by dividing a process into multiple threads. For example, in a browser, multiple tabs can be different threads. MS Word uses multiple threads: one thread to format the text, another thread to process inputs, etc.

Process vs. Thread?

The primary difference is that threads within the same process run in a shared memory space, while processes run in separate memory spaces.

Threads are not independent of one another like processes are, and as a result threads share with other threads their code section, data section, and OS resources (like open files and signals). But, like process, a thread has its own program counter (PC), register set, and stack space.

Types of Threads:

User Level thread (ULT) – Is implemented in the user level library, they are not created using the system calls. Thread switching does not need to call OS and to cause interrupt to Kernel. Kernel doesn’t know about the user level thread and manages them as if they were single-threaded processes.

Advantages of ULT –

Can be implemented on an OS that doesn’t support multi-threading.

Simple representation since thread has only program counter, register set, stack space.

Simple to create since no intervention of kernel.

Thread switching is fast since no OS calls need to be made.

Limitations of ULT –

No or less co-ordination among the threads and Kernel.

If one thread causes a page fault, the entire process blocks.

Kernel Level Thread (KLT) – Kernel knows and manages the threads. Instead of thread table in each process, the kernel itself has thread table (a master one) that keeps track of all the threads in the system. In addition kernel also maintains the traditional process table to keep track of the processes. OS kernel provides system call to create and manage threads.

Advantages of KLT –

Since kernel has full knowledge about the threads in the system, scheduler may decide to give more time to processes having large number of threads.

Good for applications that frequently block.

Limitations of KLT –

Slow and inefficient.

It requires thread control block so it is an overhead.

Difference between Process and Thread: 

S.NOProcessThread
1.Process means any program is in execution.Thread means a segment of a process.
2.The process takes more time to terminate.The thread takes less time to terminate.
3.It takes more time for creation.It takes less time for creation.
4.It also takes more time for context switching.It takes less time for context switching.
5.The process is less efficient in terms of communication.Thread is more efficient in terms of communication.
6. Multiprogramming holds the concepts of multi-process.We don’t need multi programs in action for multiple threads because a single process consists of multiple threads.
7.The process is isolated.Threads share memory.
8.The process is called the heavyweight process.A Thread is lightweight as each thread in a process shares code, data, and resources.
9.Process switching uses an interface in an operating system.Thread switching does not require calling an operating system and causes an interrupt to the kernel.
10.If one process is blocked then it will not affect the execution of other processes If a user-level thread is blocked, then all other user-level threads are blocked. 
11.The process has its own Process Control Block, Stack, and Address Space.Thread has Parents’ PCB, its own Thread Control Block, and Stack and common Address space.
12.Changes to the parent process do not affect child processes.Since all threads of the same process share address space and other resources so any changes to the main thread may affect the behavior of the other threads of the process.
13.A system call is involved in it.No system call is involved, it is created using APIs.
14.The process does not share data with each other.Threads share data with each other.

No comments:

Powered by Blogger.