Table of contents
1.
Introduction
2.
Threads
2.1.
Examples
3.
Process vs Thread
4.
Types of threads
5.
Thread Libraries
5.1.
User-level library
5.2.
Kernel-level library
6.
Frequently Asked Questions. 
6.1.
What is a process?
6.2.
What are the different states of a process?
6.3.
What is context switching?
6.4.
What is a kernel?
7.
Conclusion
Last Updated: Mar 27, 2024

Introduction to Thread

Author Debarati Ghatak
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Operating Systems

Introduction

OS(Operating System) is one the most asked topic in interviews. So if you are preparing for interviews, it’s essential to be clear with certain critical topics. One such important topic is Threads in OS. 

This blog will discuss everything you need to know to get started with threads.

Also see, Features of Operating Systems, Multiprogramming vs Multitasking

Threads

‘A thread is a basic unit of CPU utilization, which consists of its thread ID, a program counter, a register and a stack.’

A process may contain multiple threads, which is referred to as a ‘multi-threaded’ process. These multiple threads share the code, data, files, and signals.

However, a traditional process has a single thread of control and is known as a ‘single-threaded’ process. 

thread
set of threads

 

On the left, we have a single-threaded processor, and on the right, we have a multi-threaded process.

Examples

All modern applications are multi-threaded.

For instance, a word processor software might have multiple threads running in the background.

The word processor may have,

  • a thread for displaying the GUI
  • a thread for responding to keystrokes from the user
  • a thread for checking grammar and spelling

Also Read About, FCFS Scheduling Algorithm

Process vs Thread

Before learning more about threads, let us see the significant differences between processes and threads.

Process v/s Thread

 

Illustration Image

Must Recommended Topic, procedure call in compiler design“ and Open Source Operating System.

Types of threads

There are two types of threads,

  1. User-level threads
  2. Kernel-level threads

User-level threads: User-level threads are supported above the kernel without the kernel support.

Kernel-level threads: Kernel-level threads are supported and managed by the operating system.

See User Level and Kernal Level Threads

Thread Libraries

Thread libraries provide an API (Application Programming Interface) to the programmer for creating and managing the threads. There are two methods to implement a thread library,

  1. User-level library
  2. Kernel-level library

User-level library

  • This method implements a user-level library supported by users with no kernel support.
  • In this method, all the code and data structures for the threads exist in the user space.
  • Whenever a function is invoked in the library, it results in a local systemin the user space.

Kernel-level library

  • This method implements a kernel-level library supported directly by the kernel or operating system.
  • In this method, all the code and data structures for the threads exist in the kernel space.
  • Invoking a function in the library causes a system to the kernel.

Also read, fork() system call

The three main thread libraries that are used are

  1. POSIX Pthreads
  2. Win32
  3. Java

 

POSIX Pthreads: Pthreads is the threads extension of the POSIX standard. It may be provided as a user-level or kernel-level library. Pthreads are often used by UNIX and Linux systems.

Win32: Win32 is a kernel-level thread library available on Windows systems.

Java: Java thread API allows the creation and management of threads directly in Java programs. It is implemented using a thread library available on the host system.

Also see, Process Based and thread Based Multitasking
Must Read Multithreading Operating System

Read about Batch Operating System here.

Frequently Asked Questions. 

What is a process?

A process is the instance of a program executed by one or many threads. 

A process may contain multiple threads, which is called a multi-threaded process. A traditional process consists of a single thread and is known as a single-threaded process. 

What are the different states of a process?

The state of a process refers to the current activity of the process. The different states of a process are

  • New: Process is created.
  • Running: Instructions of the process are executed by a processor.
  • Waiting: Process is waiting for some event (I/O, signal).
  • Ready: Process is waiting to be assigned to a processor.
  • Terminated: Process is terminated after execution is finished.

What is context switching?

Context switching is a technique the operating system uses to switch from one process or task to another. The current state or context of a process or thread is saved so that it can be restored, and the execution can be resumed later. 

What is a kernel?

The kernel is the core part of an operating system.  It is a computer program that has complete control over everything in the system. It manages the operations of hardware and software.

Conclusion

In this blog, we discussed threads in Operating systems. We talked about the fundamental differences between processes and threads, the types of threads, and thread libraries.

If you are preparing for interviews, don’t forget to study Operating Systems - as it is one of the most asked topics in interviews. You should check our amazing course on Operating System here, designed specifically for you! 

Recommended Readings:


Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Happy Learning Ninja!

Live masterclass