当前位置:网站首页>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 .
边栏推荐
- CAD如何设置标注小数位
- 伸展树(一) - 概念和C实现
- (POJ - 1456) supermarket
- 内核同步机制
- Harbor webhook from principle to construction
- 分享psd格式怎么预览的方法和psd文件缩略图插件[通俗易懂]
- Test case writing specification in unittest framework and how to run test cases
- Comment Nike a - t - il dominé la première place toute l'année? Voici les derniers résultats financiers.
- 二叉堆(一) - 原理与C实现
- 微信小程序开发 – 用户授权登陆「建议收藏」
猜你喜欢

Unittest框架中跳过要执行的测试用例

邻接矩阵无向图(一) - 基本概念与C语言

CPI tutorial - asynchronous interface creation and use

Learning summary on June 29, 2022

京东与腾讯续签合作:向腾讯发行A类股 价值最高达2.2亿美元

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

Jd.com renewed its cooperation with Tencent: issuing class A shares to Tencent with a maximum value of US $220million

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%

Face detection and recognition system based on mtcnn+facenet

Tianrunyun, invested by Tian Suning, was listed: its market value was 2.2 billion Hong Kong, and its first year profit decreased by 75%
随机推荐
Redis configuration environment variables
Learning summary on June 30, 2022
Several cases of index failure
[buuctf.reverse] 144_ [xman2018 qualifying]easyvm
树莓派4B安装tensorflow2.0[通俗易懂]
Can I open an account today and buy stocks today? Is it safe to open an account online?
Harbor webhook from principle to construction
epoll介绍
证券账户销户后果 开户安全吗
Software project management 9.2 Software project configuration management process
[Maui] add click events for label, image and other controls
微信小程序开发 – 用户授权登陆「建议收藏」
tmux使用
VScode快捷键(最全)[通俗易懂]
博途V15添加GSD文件
Wechat applet development - user authorization to log in to "suggestions collection"
图的理论基础
Extended tree (I) - concept and C implementation
分享psd格式怎么预览的方法和psd文件缩略图插件[通俗易懂]
Openinstall: wechat applet jump to H5 configuration service domain name tutorial