当前位置:网站首页>Mechanism and type of CPU context switch
Mechanism and type of CPU context switch
2022-07-01 11:36:00 【It Mingge】
CPU Mechanism and type of context switching (CPU Context Switch)
In the previous article, I introduced zero copy zero copy In the blog of , We talked CPU Context switching of is CPU Context Switch, In this blog post , Let's review CPU context switch The concept of , Mechanisms and specific types , This will help us further understand zero copy .
1 What is? CPU context
So-called CPU Context (CPU contexts), refer to CPU When dealing with tasks ( Tasks mainly refer to processes or threads ) The state that needs to be stored ,CPU Before the task , The operating system needs to prepare these contexts , then CPU Just know how to load and execute tasks ; CPU Context , There are mainly CPU register (cpu registers) And program counter (program counters); CPU The register is CPU Inside memory, CPU When executing instructions, it is not directly connected with main memory main memory Interaction , Instead, the data in main memory is copied to registers register Zhongre direct and register Interaction :cpu registers are small but extremely fast memory built into the CPU. The program counter stores cpu The address in main memory of the instruction being executed or the next instruction to be executed :program counter is used to store the position of the instruction being executed by the CPU, or the position of the next instruction to be executed; 
2 What is? cpu context switch
CPU Context switch , namely CPU context switching, It is the basis for the operating system to realize multitasking (multitasking), formal CPU The existence of context switching mechanism , To make a single CPU Multiple tasks can be performed concurrently ; CPU When context switches , First, you need to store the context state of the current task , So that the status of the task can be restored and the task can be continued , Then restore the context state of a previously saved task and continue to execute the task ; because CPU Context switching involves saving and restoring a large number of States , Need to consume a lot of computing resources , The high cost , Therefore, the design of the operating system and applications needs to be optimized for CPU Context Switch Use ;
3 What kind of cpu context switch
Depending on the trigger conditions , There are three main types of context switching :
Multitasking Multitasking Interrupt signal processing Interrupt handling Switching between user mode and kernel mode User and kernel mode switching
3.1 Context switching triggered by multitasking Multitasking
Multitasking Is the most common type of context switching , It is a context switch triggered when the operating system schedules and concurrently processes different tasks ; The operating system is under the multi task time-sharing processing mechanism , If a process is incomplete due to waiting IO Operation or other synchronous operation unrunnable In the state of , Will trigger CPU Context switch to switch to perform other tasks ; The operating system is under the multi task time-sharing processing mechanism , If a process is still in runnalbe state , But its time slice time slice It's due , Context switching will also be triggered to switch between other tasks ; stay UNIX/LINUX Under the operating system , Tasks performed by the operating system , Including threads thread, process process, The switching execution of these tasks , It's all about cpu context switch; Process is the basic unit of resource allocation , There can be one or more threads in the same process , These threads share the virtual memory and global variables of the process (virtual memoryand and global variables); Thread is CPU The basic unit of scheduling ,( Also known as lightweight processes ), The actual scheduling object of the operating system kernel is the thread , In addition to sharing virtual memory and global variables in their corresponding processes, multiple threads , Each thread also has its own private data , Such as registers and stacks (stacks and registers); Because the context state data of the process is more than that of the thread , So process switching involves CPU Context switching overhead , More than thread switching CPU Context switching costs a lot ;( So most of the time , We prefer multithreading to multiprocessing );
3.2 Context switching triggered by interrupt processing Interrupt handling
Modern operating systems are interrupt driven architectures : such as CPU In the scenario of reading data from disk , When CPU towards IO After the device sends a read instruction ,CPU There is no need to wait for the end of the read operation , But you can continue to do other tasks ; When the disk data read operation ends ,IO The device can send an interrupt signal to interrupt CPU Other tasks currently being performed and provide the data read from the disk to CPU; The specific mechanism of interrupt processing , The following concepts are involved : Interrupt request IRQ(interrupt requests), Interrupt service routine ISR (Interrupt Service Routines), And programmable interrupt controller PIC (programmable interrupt controller etc. ; In the event of an interruption , It is necessary to transfer the... Of the task being processed CPU Part of the state information of the context is switched , This part of the status information needs to contain at least enough information to ISR After processing the interrupt, you can return to the next instruction to be executed by the interrupted task ; Context switching triggered by interrupt processing , Because there is not much state information involved and IO Support of equipment, etc , Context switching triggered by multitasking , The cost is much smaller ;
3.3 Context switching triggered by user mode and kernel mode switching User and kernel mode switching/privileged mode switch/system call switch
Linux Divide the state of process execution into kernel state and user state (kernel mode and user mode), In kernel mode, you can access kernel space (kernel space), In user mode, you can only access user space (user space); The kernel state and user state of the process , According to the privilege level privilege level Divided , Kernel space correspondence privilege level Ring 0, User space correspondence privilege level Ring 3; Kernel space (Ring 0) With the highest permission, you can directly access all resources , User space (Ring 3) Only limited resources can be accessed , You cannot directly access hardware resources such as memory ; 
When a process accesses hardware resources that cannot be accessed directly in user mode , It has to be done through a system call system call Switch from user state to kernel state , For example, when reading the data of a file , Need to call open/read/write/close Wait for system call ; When a system call occurs , Will trigger twice CPU Context switch : When the system is called , First, you need to save the current CPU Address of user mode instruction in register , Then the address of the kernel state instruction can be loaded into CPU Register to execute kernel mode tasks ; After the system call ends , You need to restore the previously saved address specified in user status to CPU In the register , Then you can continue to run in user mode ;
Triggered by system call CPU Context switch , Also often called “ Privilege mode switch ”(privileged mode switch), And multitasking multitasking Triggered by process switching CPU Context switch , There are many differences : Triggered by system call CPU Context switch , Before and after the switch, a process is used , And multitasking multitasking Triggered by process switching CPU Context switch , Different processes are executed before and after switching ; multitasking multitasking Triggered by process switching CPU Context switch , Resources related to user space include virtual memory , Stack and global variables (virtual memory, stacks and global variables), It also involves kernel space resources , Such as kernel stack and register (kernel stacks and registers), Its state data is much larger than the state data involved in the system call ; Triggered by system call CPU Context switch , Compared with multitasking triggered CPU Context switch , Because its status data is smaller , Its cost is also much smaller .
边栏推荐
- Can servers bundled with flask be safely used in production- Is the server bundled with Flask safe to use in production?
- 力扣首页简介动画
- Ultra detailed black apple installation graphic tutorial sent to EFI configuration collection and system
- Redis的攻击手法
- Share the method of how to preview PSD format and PSD file thumbnail plug-in [easy to understand]
- redis中value/set
- Goldfish rhca memoirs: do447 uses ansible to communicate with API -- using ansible tower API to start jobs
- 树莓派4B安装tensorflow2.0[通俗易懂]
- Value/string in redis
- Adjacency matrix undirected graph (I) - basic concepts and C language
猜你喜欢

Tempest HDMI leak receive 5
![[Maui] add click events for label, image and other controls](/img/d6/7ac9632681c970ed99c9e4d3934ddc.jpg)
[Maui] add click events for label, image and other controls

CAD如何設置標注小數比特

TEMPEST HDMI泄漏接收 4

Introduction to unittest framework and the first demo

Mingchuang plans to be listed on July 13: the highest issue price is HK $22.1, and the net profit in a single quarter decreases by 19%

二叉堆(一) - 原理与C实现

TEMPEST HDMI泄漏接收 5

Binary stack (I) - principle and C implementation

Jd.com renewed its cooperation with Tencent: issuing class A shares to Tencent with a maximum value of US $220million
随机推荐
Nordic nrf52832 flash 下载M4错误
开发说,“ 这个不用测,回归正常流程就行 “,测试人员怎么办?
田溯宁投的天润云上市:市值22亿港元 年利润下降75%
流动性质押挖矿系统开发如何制作,dapp丨defi丨nft丨lp流动性质押挖矿系统开发案例分析及源码
自定义 grpc 插件
Why must we move from Devops to bizdevops?
Redis启动与库进入
kubernetes之ingress探索实践
【MAUI】为 Label、Image 等控件添加点击事件
TEMPEST HDMI泄漏接收 3
Nordic nrf52832 flash download M4 error
tmux使用
[Maui] add click events for label, image and other controls
How to make the development of liquidity pledge mining system, case analysis and source code of DAPP defi NFT LP liquidity pledge mining system development
Web foundation of network security note 02
redis中value/SortedSet
Openinstall: wechat applet jump to H5 configuration service domain name tutorial
How to realize the four isolation levels of MySQL (brief)
指纹浏览器工作原理、使用场景以及重要性简单讲解
Comment Cao définit la décimale de dimension