📜  交换和上下文切换的区别

📅  最后修改于: 2021-09-28 11:01:31             🧑  作者: Mango

程序是为完成特定任务而设计的指令集。类似地,进程指的是计算机程序的运行时实例。在程序执行期间,可能有多个线程并行运行。单线程进程将线程本身称为进程。

1. 上下文切换:
操作系统使用这种技术在状态之间切换进程以通过 CPU 执行其功能。它是保存旧进程的上下文(状态)(挂起)并将其加载到新进程(恢复)的过程。每当 CPU 在一个进程和另一个进程之间切换时就会发生这种情况。基础知识,CPU 的寄存器和程序计数器在任何时候的状态都代表一个上下文。这里,当前正在执行的进程的保存状态保存状态意味着将所有活动寄存器复制到PCB(进程控制块)。此外,在此之后,恢复进程的状态以运行或执行下一个,这意味着将实时寄存器的值从 PCB 复制到寄存器。

2. 交换:
这是一个进程从主内存(RAM)临时交换(移动)到辅助内存(磁盘)的过程。主存速度快,但空间比二级存储少,所以不活动的进程被移动到二级内存,系统稍后将内存从二级内存交换到主内存。在交换过程中,大部分时间都花在传输信息上,交换的内存量与总时间成正比。 Swapping 又分为两个概念:Swap-in 和 Swap-out。

换入是从硬盘中删除程序并将其移回主内存或 RAM 的过程。
换出从 RAM 或主存储器中删除程序,并将其移动或存储到硬盘或辅助存储器。

交换和上下文切换的区别:

Context switching

Swapping

It is a procedure for storing the state of an old process and loading it into a new process. Essentially, it is a method of replicating the entire process.
A context switch occurs when the kernel switches contexts when it transfers control of the CPU from one process to another already ready to run state. Swapping happens when the entire process is moved to the disk.
A context switch determines whether a process is in the pause mode. When it comes to Swapping, It deals with memory, how much memory is being swapped.
The context switch toggles the process from running to ready states, while the dispatcher is responsible for allocating CPU resources to processes present in the ready queue. It is an OS term that we use for referring to the exchange of data between the disk and the main memory.
Active processes do context switching.  Inactive processes do swapping.
It offers a higher degree of multi-tasking. It provides a more significant degree of multiprogramming.
It helps to get better utilization of the operating system. It helps to get better utilization of memory.