Wednesday 2 September 2020

Thread Life Cycle and Multi-threading concepts in OS

 

Concepts of Threads:

 

A process is divided into number of lightweight process.  Each light weight process is said to be a thread.  The thread has a program counter that keeps the track of which instruction to execute next, it has a registers, which holds its current working variables.  It has a stack which contains the execution history.

 

Number of threads can share an address space, open files and other resources. Same as a number of processes can share their physical memory, disks, printers and other resources. Threads operates in many respects in the same manner as process as they have some of the properties of process.

 

Threads can be in one of several states: Ready, Blocked, Running Terminated.  Like process, threads share the CPU, and only one thread at a time is active or running. Threads can create a child threads. Unlike process, threads are not independent of one another. As all threads can access every address in the stack; a thread can read or write over any other thread stacks.

 

The programmer specifies that application contain ‘Threads of execution’. It describes each thread designating portion of program that may be executed concurrently with other threads. This capability is called multi-threading.

 

Life cycle of Thread:

 

A thread is said to be in one of the following several thread states:

 

·         Born state:  a thread has just been created.

 

·         Ready state:  the thread is waiting for CPU.

 

·         Running state:  the system assigns the processor to the thread.

 

·         Blocked state:  the thread is waiting for an event to occur or waiting for an IO device.

 

·         Sleep state: sleeping thread becomes ready after the designated sleep time expires.

 

·         Dead or Terminated state: the thread has finished its execution.

 

Thread Life Cycle


Multi-threading:


Single-threaded and multi-threaded process
Fig: Single-threaded and multi-threaded process


A process is divided into a number of smaller tasks; each task is called a thread. Number of threads within a process execute at a time. This is called as multi-threading.  Supporting of multithreading is the additional capability of an operating system. Based on the functionality, threads are divided into four categories. These are:

 

1.    One Process One Thread:

It is a traditional approach in which the process maintains only one thread. Hence, it is called single thread approach. For ex: MS DOS operating system supports this type of approach.

 

2.    One Process, Multiple Threads:

In this approach, a process is divided into a number of threads. Best example for this is a Java Runtime Environment (JRE).

 

3.    Multiple Process, One Thread Per Process:

An OS supports multiple user processes but only support one thread per process. UNIX is the best example of this.

 

4.    Multiple Processes, Multiple Threads Per Process:

In this approach, each process is divided into a number of threads. For ex: Windows 2000, Sun Solaris and Linux.

 

Advantages of a thread:

 

1.    A thread takes less time to terminate than a process.

 

2.    It takes less time to switch between two threads within the same process.

 

3.    It takes less time to create a new thread within the process.

 

4.    Efficient communication between threads is possible.

No comments:

Post a Comment