程序是旨在执行特定任务的一组指令。因此,程序是被动实体。
正在执行(正在执行)的程序称为过程。
因此,过程是一个活动实体。当程序运行时,它可能包含相应运行的各种线程。在单线程进程中,线程本身就是进程。在多线程进程中,我们需要在不同线程之间切换以执行程序。
1.线程切换:
线程切换是在同一进程中从一个线程切换到另一个线程的上下文切换的一种。线程切换非常有效且便宜得多,因为它只切换出身份和资源,例如程序计数器,寄存器和堆栈指针。线程到线程切换的成本与进入和退出内核的成本大致相同。
2.流程切换:
进程切换是上下文切换的一种,我们在其中切换一个进程与另一个进程。它涉及将所有过程资源与新过程所需的那些资源进行交换。这意味着切换存储器地址空间。这包括内存地址,页表和内核资源,处理器中的缓存。
线程上下文切换和进程上下文切换之间的区别:
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. |