Threads in Operating System (OS)
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)
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.
- 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 –
- Process are independent whereas threads exist as subsets of process.
- Processes have separate address space whereas Threads within a same process same address space.
- Processes are resource intensive whereas Threads are lightweight processes.
- 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.
- 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 –
- User level threads
- Kernel level threads
Different Types of Thread Models
Also, there are two different types of processes
- Single-threaded processes
- Multi-threaded processes
- Banker’s Algorithm
- bankers algorithm for deadlock avoidance in c (in above post itself)Resource Allocation Graph (RAG)
- Dining Philosopher’s Problem
- Bounded Buffer Problem / Producer Consumer Problem
- User Level thread Vs Kernel Level thread
- Multithreading models
- Difference between program and process