当前位置:网站首页>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 .
边栏推荐
- Tempest HDMI leak reception 4
- 印象深刻的bug汇总(持续更新)
- Value/hush in redis
- Redis common sense
- 8 best practices to protect your IAC security!
- Adjacency matrix undirected graph (I) - basic concepts and C language
- TMUX usage
- Brief analysis of edgedb architecture
- Question: what professional qualities should test engineers have?
- CAD如何设置标注小数位
猜你喜欢
华为HMS Core携手超图为三维GIS注入新动能
Software project management 9.2 Software project configuration management process
Redis configuration environment variables
Shangtang entered the lifting period: the core management voluntarily banned and strengthened the company's long-term value confidence
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%
Explore the contour detection function findcontours() of OpenCV in detail with practical examples, and thoroughly understand the real role and meaning of each parameter and mode
Exploration and practice of inress in kubernetes
图的理论基础
Binary stack (I) - principle and C implementation
金融壹账通拟7月4日香港上市:2年亏近30亿 市值蒸发超90%
随机推荐
Y48. Chapter III kubernetes from introduction to mastery -- pod status and probe (21)
Can I open a securities account anywhere? Is it safe to open an account
CPI tutorial - asynchronous interface creation and use
Compile and debug net6 source code
耐克如何常年霸榜第一名?最新财报答案来了
S7-1500PLC仿真
伸展树(一) - 概念和C实现
redis中value/SortedSet
CPI教程-异步接口创建及使用
Jd.com renewed its cooperation with Tencent: issuing class A shares to Tencent with a maximum value of US $220million
redis中value/String
Width and widthstep of iplimage
redis中value/list
田溯宁投的天润云上市:市值22亿港元 年利润下降75%
2022/6/28学习总结
Ultra detailed black apple installation graphic tutorial sent to EFI configuration collection and system
优雅地翻转数组
金鱼哥RHCA回忆录:DO447使用Ansible与API通信--使用Ansible Tower API启动作业
[buuctf.reverse] 144_[XMAN2018排位赛]easyvm
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