The Wayback Machine - https://web.archive.org/web/20230419194358/https://www.geeksforgeeks.org/thread-in-operating-system/
Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Thread in Operating System

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article
 

What is a Thread? 

Within a program, a thread is a separate execution path. It is a lightweight process that the operating system can schedule and run concurrently with other threads. The operating system creates and manages threads, and they share the same memory and resources as the program that created them. This enables multiple threads to collaborate and work efficiently within a single program.

A thread is a single sequence stream within a process. Threads are also called as lightweight processes as it possess some of the properties of processes. Each thread belongs to exactly one process.

In operating system that support multithreading, process can consist of many threads.

These threads run in parallel improving the application performance.

Each such thread has its own CPU state and stack, but they share the address space of the process and the environment.

Threads can share common data so they do not need to use interprocess communication.

Like the processes, threads also have states like ready, executing, blocked etc. priority can be assigned to the threads just like process and highest priority thread is scheduled first.

Each thread has its own Thread Control Block (TCB). Like process, context switch occurs for the thread and register contents are saved in (TCB).

As threads share the same address space and resources, synchronization is also required for the various activities of the thread.

Why Multithreading? 

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. More advantages of multithreading are discussed below.

Multithreading is a technique used in operating systems to improve the performance and responsiveness of computer systems. Multithreading allows multiple threads (i.e., lightweight processes) to share the same resources of a single process, such as the CPU, memory, and I/O devices.

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. 

Advantages of Thread over Process 

  1. Responsiveness: If the process is divided into multiple threads, if one thread completes its execution, then its output can be immediately returned.
  2. Faster context switch: Context switch time between threads is lower compared to process context switch. Process context switching requires more overhead from the CPU. 
  3. Effective utilization of multiprocessor system: If we have multiple threads in a single process, then we can schedule multiple threads on multiple processor. This will make process execution faster. 
  4. Resource sharing: Resources like code, data, and files can be shared among all threads within a process. Note: stack and registers can’t be shared among the threads. Each thread has its own stack and registers. 
  5. Communication: Communication between multiple threads is easier, as the threads shares common address space. while in process we have to follow some specific communication technique for communication between two process. 
  6. Enhanced throughput of the system: If a process is divided into multiple threads, and each thread function is considered as one job, then the number of jobs completed per unit of time is increased, thus increasing the throughput of the system. 

Types of Threads 

There are two types of threads:

  • User Level Thread 
  • Kernel Level Thread

Refer User Thread vs Kernel Thread for more details. 

Below are previous years’ gate questions on threads:

Reference: Multithreading in C 

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up
Last Updated : 09 Apr, 2023
Like Article
Save Article
Similar Reads