A program is a set of instructions that is aimed at performing specific tasks.A program is therefore a passive entity.
A program that is executing (under action) is called a process.
Thus a process is an active entity. When a program runs then it may contain various threads run accordingly. In single threaded processes, the thread itself is the process. While in multithreaded processes we need to switch between different threads for the execution of our program.
1. Thread Switching :
Thread switching is a type of context switching from one thread to another thread in the same process. Thread switching is very efficient and much cheaper because it involves switching out only identities and resources such as the program counter, registers and stack pointers. The cost of thread-to-thread switching is about the same as the cost of entering and exiting the kernel.
2. Process Switching :
Process switching is a type of context switching where we switch one process with another process. It involves switching of all the process resources with those needed by a new process. This means switching the memory address space. This includes memory addresses, page tables, and kernel resources, caches in the processor.
Difference between Thread Context Switch and Process Context Switch :
|No.||Thread Context Switch||Process Context Switch|
|1.||TCS occurs when the CPU saves the current state of the thread and switches to another thread of the same process.||PCS occurs when the operating system’s scheduler saves the current state of the running Program (including the state of PCB) and switches to another program.|
|2.||TCS helps the CPU to handle multiple threads simultaneously.||PCS involves loading of the states of the new program for it’s execution.|
|3.||TCS does not involves switching of memory address spaces.All the memory addresses that the processor accounts remain saved.||PCS involves switching of memory address spaces.All the memory addresses that the processor accounts gets flushed.|
|4.||Processor’s cache and Translational Lookaside Buffer preserves their state.||Processor’s cache and TLB gets flushed.|
|5.||Though TCS involves switching of registers and stack pointers, it does not afford the cost of changing the address space.Hence it is more efficient.||PCS involves the heavy cost of changing the address space.Hence it is less efficient.|
|6.||TCS is a bit faster and cheaper.||PCS is relatively slower and costlier.|
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Measure the time spent in context switch?
- Difference between Hub and Switch
- Difference between Switch and Bridge
- Difference between Switch and Gateway
- Difference between Router and Switch
- Difference between Router and Layer-3 Switch
- Difference between Process and Thread
- Difference between Process and Kernel Thread
- Context-sensitive Grammar (CSG) and Language (CSL)
- Various Properties of context free languages (CFL)
- Classification of Context Free Grammars
- Check if the language is Context Free or Not
- Difference between Process and User Level Thread
- Regular Expression Vs Context Free Grammar
- Difference between Process Image and Multi Thread Process image
- Converting Context Free Grammar to Chomsky Normal Form
- Converting Context Free Grammar to Greibach Normal Form
- switch vs if else
- Difference between Thread.start() and Thread.run() in Java
- Difference between User Level thread and Kernel Level thread
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.