Thread in OS

Thread Introduction

  • A thread can be defined as a concurrent or parallel unit of execution within a single process.
  • Each thread represents a separate flow of control.
  • They provide a way to improve application performance through parallelism.
  • A process or a system with multiple units of execution that is thread is known as a multithreaded system or multithreaded process.
  • Any process will have a minimum of one thread called the main thread.
  • Threads are also known as Light weight Process.

Diag-1: A process with two Threads

Example of a Thread:

  • Microsoft word has multiple threads which perform different task parallelly, like one thread responsible for taking the input while other thread does the spell check and other does the spell count.
  • The web server runs as a multihreaded system where the request from each client is handled by a separate thread.
  • If the web server ran as a traditional single-threaded process, it would be able to service only one client at a time. The amount of time that a client might have to wait for its request to be serviced could be enormous.

Diag-2: Multithreaded Server Architecture

Threads in Uniprocessor and Multiprocessor System

  • In the case of a uniprocessor system only one thread is executed at a time by the CPU, but the CPU switches(context switch) rapidly between the threads to give the illusion that threads are running parallelly.
  • But in case of multiprocessor system, different thread can execute at the same time by different processor parallely.

Diag-3: Threads in Uniprocessor and Multiprocessor System

Use of a Thread:

There are two main reasons to have threads in a program:

  • Parallelism
  • Responsiveness

Parallelism:

  • It is nothing but executing more than one task at a time and so threads help in achieving parallelism.
  • In the case of multiprocessor, each thread can be executed independently by a different processor at the same time.
  • The task which is completely independent of each other or can be broken down into sub-task can be run made to run parallel on the different processor by using threads.
  • Consider a simple example of finding the sum of numbers from 1 to 100, this task can be broken down into two subsets task that is finding the sum from 1 to 50 and the other responsible for finding from 51 to 100 which can be summed later.
  • Since each sub task is independent of each other, hence can be done parallelly by the two threads parallel.
  • Thus parallelism optimizes the performance of the system.

Responsiveness:

  • Thread based application has better responsiveness in the sense that even if one thread is blocked because of any reason(I/O operation), the other thread can continue to execute.
  • If the process is blocked, the complete execution comes to halt and the CPU goes to the idle state.

Relevant Posts



Categories: Operating system (OS)

6 replies

Trackbacks

  1. Types of Threads - Tech Access
  2. User Level Threads - Tech Access
  3. Index of Operating System - Tech Access
  4. Characteristics of a Thread - Tech Access
  5. Pros and Cons of Thread - Tech Access
  6. Process vs Thread - Tech Access

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: