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 CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.