Threads in Operating System (OS)

What are Threads ?

On this page we will discuss about Threads in Operating System. Threads are Smallest sequence of programmed instruction that can be managed independently by a scheduler.

User Level Thread Vs Kernel Level Thread​

Threads in Operating System

Threads are Smallest sequence of programmed instruction that can be managed independently by a scheduler.

  • A thread comprises of its own
    • Thread ID – Unique ID for a thread in execution
    • Program counter – Keeps track of instruction to execute
    • System Register set – Active Variables of thread
    • Stack – All execution history (Can be used for debugging)
Threads in Operating System – 3

Its important to know difference between thread, process and program.

Program, Process and Threads are three basic concepts of the operating systems about which every computer science engineer must be familiar with. Here, we will explain what each of them is all about and how they differ from each other.

What is a program?

Program is an executable file containing the set of instructions written to perform a specific job on your computer. For example, notepad.exe is an executable file containing the set of instructions which help us to edit and print the text files.

Programs are not stored on the primary memory in your computer. They are stored on a disk or a secondary memory on your computer. They are read into the primary memory and executed by the kernel. A program is sometimes referred as passive entity as it resides on a secondary memory.

What is a process?

Process is an executing instance of a program. For example, when you double click on a notepad icon on your computer, a process is started that will run the notepad program.

A process is sometimes referred as active entity as it resides on the primary memory and leaves the memory if the system is rebooted. Several processes may related to same program. For example, you can run multiple instances of a notepad program. Each instance is referred as a process.

What is a thread?

Thread is the smallest executable unit of a process. For example, when you run a notepad program, operating system creates a process and starts the execution of main thread of that process.

A process can have multiple threads. Each thread will have their own task and own path of execution in a process. For example, in a notepad program, one thread will be taking user inputs and another thread will be printing a document.

All threads of the same process share memory of that process. As threads of the same process share the same memory, communication between the threads is fast.

Threads in Operating System – 6

Facts

  • Multiple threads can exist within one process , and they can also share resources such as memory. Different process do not share those resources.
  • A thread shares with other thread information like code segment, data segment and open files. All other thread segments are aware of any alternation made by a thread memory.
  • Threads can be scheduled preemptively or cooperatively .
  • Helps to use to achieve parallelism by dividing a process into multiple threads . 

Thread Versus Process –

  1. Process are independent whereas threads exist as subsets of process.
  2. Processes have separate address space whereas Threads within a same process same address space.
  3. Processes are resource intensive whereas Threads are lightweight processes.
  4. If 1 Process is blocked , no other processes can execute unless the blocked process is unblocked . whereas , if 1 thread is waiting, a 2nd thread in same task can be run.
  5. No other process changes data of other process . They are independent, whereas one thread can read, write or change other tread.

Now, let’s have a look at how threads are implemented

  • In Single processor system, they implemented multithreading by time slicing, CPU switches between software threads. This adds a context overhead as context switching happens very often. It happens very fast, so it appears to user as if these tasks are performed in parallel.
  • In multiprocessor, multiple threads execute in parallel, each processor executes 1 thread simultaneously while other processor execute other thread.

Advantages of thread –

  • Context Switching is faster between threads compared to context switching between processor.
  • We can effectively utilize CPU in multiprocessor as we can make 1 process with many threads run in different processor of multiprocessor , thus increasing speed.
  • Sharing and communication is simplified in threads.
  • Threads can implement parallelism.

Types of Threads

Threads are implemented in 2 ways –

  1. User level threads
  2. Kernel level threads

Read more about User level thread and Kernel level thread here on this post

Different Types of Thread Models

Read more about various thread models here

Also, there are two different types of processes

  1. Single-threaded processes
  2. Multi-threaded processes
Threads in Operating System

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription