<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
    <channel>
        <title>Richard Liu’s Blog</title>
        <link>http://richardliuda.top/</link>
        <description>Under construction.</description>
        <lastBuildDate>Thu, 23 Apr 2026 20:42:04 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>https://github.com/jpmonette/feed</generator>
        <language>en-US</language>
        <copyright>All rights reserved 2026, Richard Liu</copyright>
        <item>
            <title><![CDATA[操作系统第三章讲义]]></title>
            <link>http://richardliuda.top/article/34b69776-e204-8041-8892-d2c3c5afd1ed</link>
            <guid>http://richardliuda.top/article/34b69776-e204-8041-8892-d2c3c5afd1ed</guid>
            <pubDate>Fri, 24 Apr 2026 00:00:00 GMT</pubDate>
            <content:encoded><![CDATA[<div id="notion-article" class="mx-auto overflow-hidden "><main class="notion light-mode notion-page notion-block-34b69776e20480418892d2c3c5afd1ed"><div class="notion-viewport"></div><div class="notion-collection-page-properties"></div><div class="notion-sync-block notion-block-34b69776e20480ee9a99ffa0bf99e9fa"><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-33769776e20480bc8b4cf4dc46f9420e" data-id="33769776e20480bc8b4cf4dc46f9420e"><span><div id="33769776e20480bc8b4cf4dc46f9420e" class="notion-header-anchor"></div><a class="notion-hash-link" href="#33769776e20480bc8b4cf4dc46f9420e" title="3.1 处理机调度的层次和调度算法的目标"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3.1 处理机调度的层次和调度算法的目标</span></span></h2><div class="notion-callout notion-gray_background_co notion-block-34a69776e20480c18319eb85e8e94c03"><div class="notion-page-icon-inline notion-page-icon-span"><span class="notion-page-icon" role="img" aria-label="💡">💡</span></div><div class="notion-callout-text"><h3 class="notion-h notion-h2 notion-block-34a69776e20480728a66fdcc9f36c7d4" data-id="34a69776e20480728a66fdcc9f36c7d4"><span><div id="34a69776e20480728a66fdcc9f36c7d4" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34a69776e20480728a66fdcc9f36c7d4" title="什么是调度？"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">什么是调度？</span></span></h3><div class="notion-text notion-block-34a69776e20480b99bd5cc909e58e4b7"><b>调度的本质：分配资源</b></div><ul class="notion-list notion-list-disc notion-block-34a69776e204803b88e4d2cdbe460486"><li><b>资源是什么？</b> 处理机（CPU）的使用时间。</li></ul><ul class="notion-list notion-list-disc notion-block-34a69776e20480e6a96dc61d178279f6"><li><b>谁来分？</b> 操作系统。</li></ul><ul class="notion-list notion-list-disc notion-block-34a69776e20480728db8d96812c40036"><li><b>分配给谁？</b> 内存里排队等着的各个任务（进程/作业）。</li></ul><div class="notion-text notion-block-34a69776e204807c85e9f107dc5d1ca6">因为 CPU 有限，但任务有很多。所以需要一套运行的规则，这就是调度。</div></div></div><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-34a69776e20480bc9087da8ac63c75c6" data-id="34a69776e20480bc9087da8ac63c75c6"><span><div id="34a69776e20480bc9087da8ac63c75c6" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34a69776e20480bc9087da8ac63c75c6" title="3.1.1 处理机调度层次"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3.1.1 处理机调度层次</span></span></h3><div class="notion-text notion-block-34a69776e20480ce9856e5c55ea3b820">因为计算机的架构所限，<b>计算机硬件资源的速度极不匹配，且内存容量有限。</b>所以单靠单一的调度策略很难顾全大局。</div><div class="notion-text notion-block-34a69776e20480549aadf425c4009c5d">这时候就必须对每一层级进行分工调度，协调好“慢速的磁盘”、“金子做的内存”和“极速的CPU”。</div><ul class="notion-list notion-list-disc notion-block-34a69776e20480588f7ec98dbe23562d"><li>高级调度——作业调度</li></ul><ul class="notion-list notion-list-disc notion-block-34a69776e2048050ba16fbb30431df45"><li>中级调度——内存调度</li></ul><ul class="notion-list notion-list-disc notion-block-34a69776e204808aa17de4d062844c49"><li>低级调度——进程调度</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34a69776e204800b93a2fcf15b77b25e" data-id="34a69776e204800b93a2fcf15b77b25e"><span><div id="34a69776e204800b93a2fcf15b77b25e" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34a69776e204800b93a2fcf15b77b25e" title="如果没有多级调度"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">如果没有多级调度</span></span></h4><ol start="1" class="notion-list notion-list-numbered notion-block-34a69776e20480ebbdbed17995a48a4d" style="list-style-type:decimal"><li><b>没有高级调度会“撑死”：</b> 内存撑爆，系统崩溃。</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-34a69776e20480d0b51bf15691619c93" style="list-style-type:decimal"><li><b>没有中级调度会“饿死”：</b> 小任务等不到响应，长任务霸占资源。</li></ol><ol start="3" class="notion-list notion-list-numbered notion-block-34a69776e204807d9d1cee81927a2a7e" style="list-style-type:decimal"><li><b>没有低级调度会“急死”：</b> 电脑变得极其不灵活，开个网页可能要把后台所有程序都关掉才行。</li></ol><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-34a69776e20480019d83dfe0d499246d" data-id="34a69776e20480019d83dfe0d499246d"><span><div id="34a69776e20480019d83dfe0d499246d" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34a69776e20480019d83dfe0d499246d" title="3.1.2 处理机调度算法的目标"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3.1.2 处理机调度算法的目标</span></span></h3><div class="notion-callout notion-gray_background_co notion-block-34a69776e20480a5b737e327f53ddfba"><div class="notion-page-icon-inline notion-page-icon-span"><span class="notion-page-icon" role="img" aria-label="💡">💡</span></div><div class="notion-callout-text"><div class="notion-text notion-block-34a69776e20480f5a649c4c432ec1649"><b>核心目标：尽量减少处理机的闲置。</b></div></div></div><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34a69776e204801f9e44e44f4f3bf342" data-id="34a69776e204801f9e44e44f4f3bf342"><span><div id="34a69776e204801f9e44e44f4f3bf342" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34a69776e204801f9e44e44f4f3bf342" title="1. 高资源利用率"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">1. 高资源利用率</span></span></h4><div class="notion-text notion-block-34a69776e20480c59f05e7d33ce2b5b1"><em>——别让CPU偷懒。</em></div><ul class="notion-list notion-list-disc notion-block-34a69776e2048053a18dc3b456193431"><li>应使系统中的处理机和其它所有资源都尽可能地保持忙碌状态。</li></ul><ul class="notion-list notion-list-disc notion-block-34a69776e20480a8a62ef2013353f5a7"><li>计算处理机利用率：</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34a69776e20480218e38c5e5d1b22f79" data-id="34a69776e20480218e38c5e5d1b22f79"><span><div id="34a69776e20480218e38c5e5d1b22f79" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34a69776e20480218e38c5e5d1b22f79" title="2. 公平性"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">2. 公平性</span></span></h4><div class="notion-text notion-block-34a69776e20480338182c3635a538a8e"><em>——别让老实人等死。</em></div><ul class="notion-list notion-list-disc notion-block-34a69776e20480fbaf06cea8ae51306f"><li>调度算法需要使各进程都要获得合理的CPU时间。</li></ul><ul class="notion-list notion-list-disc notion-block-34a69776e2048040be80c5f8acf611be"><li>不能厚此薄彼，不能因为进程小就不给其分配资源。</li></ul><ul class="notion-list notion-list-disc notion-block-34a69776e2048073a18ac98fb3f44cd8"><li>不能发生饥饿现象。</li></ul><div class="notion-callout notion-gray_background_co notion-block-34a69776e20480649e68f17919304ac5"><div class="notion-page-icon-inline notion-page-icon-span"><span class="notion-page-icon" role="img" aria-label="💡">💡</span></div><div class="notion-callout-text"><h5 class="notion-h notion-h4 notion-block-34a69776e20480c08ad3c2f01e809cef" data-id="34a69776e20480c08ad3c2f01e809cef"><span><div id="34a69776e20480c08ad3c2f01e809cef" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34a69776e20480c08ad3c2f01e809cef" title="饥饿现象"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">饥饿现象</span></span></h5><div class="notion-text notion-block-34a69776e204808a969ed3e50a7b4c6d">如果一个算法总是让优先级高的先跑，那么一个优先级低的进程可能等了 3 天还没跑上 1 秒钟，这就叫“饥饿”。</div></div></div><ul class="notion-list notion-list-disc notion-block-34a69776e2048045bc5ce6282cdc91de"><li><b>公平是相对的</b></li><ul class="notion-list notion-list-disc notion-block-34a69776e2048045bc5ce6282cdc91de"><li><b>同类等同：</b> 如果都是普通的文档编辑程序，大家拿到的 CPU 时间应该差不多。</li><li><b>异类区别：</b> 视频播放器（实时性要求高）和后台杀毒软件（不急）相比，系统会优先照顾视频播放器，这虽然“不平等”，但在逻辑上是“公平”的。</li></ul></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34a69776e204809e8190fbfccbb9d516" data-id="34a69776e204809e8190fbfccbb9d516"><span><div id="34a69776e204809e8190fbfccbb9d516" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34a69776e204809e8190fbfccbb9d516" title="3. 平衡性"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3. 平衡性</span></span></h4><div class="notion-text notion-block-34a69776e204800ab6c5d7a380aff425"><em>——别让厨师闲着，也别让洗碗机闲着。</em></div><div class="notion-callout notion-gray_background_co notion-block-34a69776e20480e5a43fee2ccd8629bb"><div class="notion-page-icon-inline notion-page-icon-span"><span class="notion-page-icon" role="img" aria-label="💡">💡</span></div><div class="notion-callout-text"><h5 class="notion-h notion-h4 notion-block-34a69776e2048001b820e1fd0b929663" data-id="34a69776e2048001b820e1fd0b929663"><span><div id="34a69776e2048001b820e1fd0b929663" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34a69776e2048001b820e1fd0b929663" title="进程的分类"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">进程的分类</span></span></h5><ol start="1" class="notion-list notion-list-numbered notion-block-34a69776e2048028990fc3dbba908a80" style="list-style-type:decimal"><li><b>计算型作业 (CPU-bound)：</b> 狂算数字，CPU 很累，但不怎么用硬盘/网络。</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-34a69776e204807b9bf8e09517cce5af" style="list-style-type:decimal"><li><b>I/O 型作业 (I/O-bound)：</b> 频繁读写硬盘、收发网络包，CPU 很闲，但外部设备很忙。</li></ol></div></div><ul class="notion-list notion-list-disc notion-block-34a69776e20480f185ccce3c697439e3"><li>调度算法要把这两种作业<b>混合搭配，</b>平衡CPU和IO设备的使用情况。</li></ul><ul class="notion-list notion-list-disc notion-block-34a69776e20480dbb6d0e6355f47c894"><li><b>理想状态：</b> CPU、硬盘、网卡都在同时工作，系统资源利用率达到最高。</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34a69776e20480298b4ec8be6d420ee1" data-id="34a69776e20480298b4ec8be6d420ee1"><span><div id="34a69776e20480298b4ec8be6d420ee1" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34a69776e20480298b4ec8be6d420ee1" title="4. 策略强制执行"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">4. 策略强制执行</span></span></h4><div class="notion-text notion-block-34a69776e20480a38753d6ef29fed416"><em>——规矩大过天。</em></div><blockquote class="notion-quote notion-block-34a69776e20480d6ad51cf70e53c6b63"><div>就像医院急诊室，规定“抢救病人第一优先”。哪怕普通感冒患者已经等了 2 小时，只要来了一个心梗病人，医生必须立刻去抢救心梗病人。</div></blockquote><ul class="notion-list notion-list-disc notion-block-34a69776e2048011a372d87eddaba384"><li>操作系统设定好的规则，调度算法必须无条件准确执行。</li></ul><ul class="notion-list notion-list-disc notion-block-34a69776e204800cbc8ae274377a1fd4"><li>即使可能会导致某些普通任务被推迟、延误，但为了系统的安全性、稳定性，必须严格执行预设策略。</li></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-34b69776e204809183dbf77e38f2d000" data-id="34b69776e204809183dbf77e38f2d000"><span><div id="34b69776e204809183dbf77e38f2d000" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e204809183dbf77e38f2d000" title="3.1.3. 系统的目标"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3.1.3. 系统的目标</span></span></h3><div class="notion-text notion-block-34b69776e20480b69ffad8a514d147af">操作系统历史上有三种系统：</div><ul class="notion-list notion-list-disc notion-block-34b69776e2048096bf0bce90c0ddeace"><li>批处理系统</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e204807189c4d5f5af3749f5"><li>分时系统</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e2048006b944dc55e4386e88"><li>实时系统</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e20480aa96dec18d4f5880b7" data-id="34b69776e20480aa96dec18d4f5880b7"><span><div id="34b69776e20480aa96dec18d4f5880b7" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480aa96dec18d4f5880b7" title="批处理系统的目标"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">批处理系统的目标</span></span></h4><ul class="notion-list notion-list-disc notion-block-34b69776e2048033a6bbd39141efa5ad"><li>批处理系统通常用于大型计算任务，它不强调人机交互，而是强调“干活的效率”。</li></ul><h5 class="notion-h notion-h4 notion-h-indent-3 notion-block-34b69776e20480cb8a2be8b2f7ed2dbe" data-id="34b69776e20480cb8a2be8b2f7ed2dbe"><span><div id="34b69776e20480cb8a2be8b2f7ed2dbe" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480cb8a2be8b2f7ed2dbe" title="1. 平均周转时间短"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">1. 平均周转时间短</span></span></h5><ul class="notion-list notion-list-disc notion-block-34b69776e204806ea329f680a3a3a559"><li>周转时间——衡量系统快慢最基本的指标</li><ul class="notion-list notion-list-disc notion-block-34b69776e204806ea329f680a3a3a559"><li>定义：从你把作业提交给系统开始，到系统帮你把活干完输出结果为止，这段总时间。</li></ul></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480f4bc3ac1870a9722a2"><li>平均周转时间：</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480e083e3eabe5d6a5efd"><li>带权周转时间——衡量小任务是否被大任务压住了。</li><ul class="notion-list notion-list-disc notion-block-34b69776e20480e083e3eabe5d6a5efd"><li>定义为：周转时间 / 实际运行时间。</li></ul></ul><ul class="notion-list notion-list-disc notion-block-34b69776e2048026815ae4682d0abbef"><li>平均带权周转时间：</li><ul class="notion-list notion-list-disc notion-block-34b69776e2048026815ae4682d0abbef"><li>衡量一组作业的整体等待体验，值越接近 1 说明用户满意度越高。</li><li>如果 W 很大，说明你的小任务在后台被大任务压住了，无法及时运行。</li></ul></ul><h5 class="notion-h notion-h4 notion-h-indent-3 notion-block-34b69776e204800a97c2e83dd00807d6" data-id="34b69776e204800a97c2e83dd00807d6"><span><div id="34b69776e204800a97c2e83dd00807d6" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e204800a97c2e83dd00807d6" title="2. 系统吞吐量高"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">2. 系统吞吐量高</span></span></h5><ul class="notion-list notion-list-disc notion-block-34b69776e204808c8551dbf602c9a084"><li>定义：单位时间内系统完成的作业数量。</li><ul class="notion-list notion-list-disc notion-block-34b69776e204808c8551dbf602c9a084"><li>与批处理作业的平均长度有关</li></ul></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480d28d77ff590891b57e"><li>提高</li><ul class="notion-list notion-list-disc notion-block-34b69776e20480d28d77ff590891b57e"><li>多跑短作业</li></ul></ul><h5 class="notion-h notion-h4 notion-h-indent-3 notion-block-34b69776e20480b8b8f5ebe2d5e01e06" data-id="34b69776e20480b8b8f5ebe2d5e01e06"><span><div id="34b69776e20480b8b8f5ebe2d5e01e06" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480b8b8f5ebe2d5e01e06" title="3. 处理机利用率高"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3. 处理机利用率高</span></span></h5><ul class="notion-list notion-list-disc notion-block-34b69776e204802f8564f469d9d2525a"><li>多跑计算量大的作业</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e2048045b2c3cf5441669592" data-id="34b69776e2048045b2c3cf5441669592"><span><div id="34b69776e2048045b2c3cf5441669592" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e2048045b2c3cf5441669592" title="分时系统的目标"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">分时系统的目标</span></span></h4><ul class="notion-list notion-list-disc notion-block-34b69776e20480d1a6b3cc1f50e5f02f"><li>响应时间快</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480728ff8f12f0e53634e"><li>均衡性</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e204804bb9a3d9b7562f4629" data-id="34b69776e204804bb9a3d9b7562f4629"><span><div id="34b69776e204804bb9a3d9b7562f4629" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e204804bb9a3d9b7562f4629" title="实时系统的目标"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">实时系统的目标</span></span></h4><ul class="notion-list notion-list-disc notion-block-34b69776e20480eea35dcdbf208a1887"><li>截止时间的保证</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480d89518eb8936128d65"><li>可预测性</li></ul><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-34a69776e204809395a2d7060d497c16" data-id="34a69776e204809395a2d7060d497c16"><span><div id="34a69776e204809395a2d7060d497c16" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34a69776e204809395a2d7060d497c16" title="3.2 作业与作业调度"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3.2 作业与作业调度</span></span></h2><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-34b69776e2048039bfd8dc72a05131ab" data-id="34b69776e2048039bfd8dc72a05131ab"><span><div id="34b69776e2048039bfd8dc72a05131ab" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e2048039bfd8dc72a05131ab" title="3.2.1 作业（Job）"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3.2.1 作业（Job）</span></span></h3><ul class="notion-list notion-list-disc notion-block-34b69776e2048053ab68e700cae0b40f"><li>作业：用户交给计算机的一个<b>完整的任务</b></li><ul class="notion-list notion-list-disc notion-block-34b69776e2048053ab68e700cae0b40f"><li>不仅包含程序和数据，还包含一份作业说明书，告诉操作系统该怎么运行。</li></ul></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480859e33d7f173058f82"><li>作业步：作业被拆分成若干个<b>相互联系、顺序执行</b>的处理步骤。每一个步骤就是一个作业步</li></ul><h5 class="notion-h notion-h4 notion-h-indent-2 notion-block-34b69776e20480d1b071c6dcf1e78f5d" data-id="34b69776e20480d1b071c6dcf1e78f5d"><span><div id="34b69776e20480d1b071c6dcf1e78f5d" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480d1b071c6dcf1e78f5d" title="作业控制块（JCB）"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">作业控制块（JCB）</span></span></h5><ul class="notion-list notion-list-disc notion-block-34b69776e2048005b95ac23cda8b338a"><li>操作系统中用于管理和描述“作业”的数据结构。</li></ul><div class="notion-text notion-block-34b69776e2048013aaa7e186615e170f">一个典型的 JCB 通常包含以下四类信息：</div><ol start="1" class="notion-list notion-list-numbered notion-block-34b69776e20480ab89f5e944ff4a2935" style="list-style-type:decimal"><li><b>作业标识（Identity）：</b></li><ol class="notion-list notion-list-numbered notion-block-34b69776e20480ab89f5e944ff4a2935" style="list-style-type:lower-alpha"><ul class="notion-list notion-list-disc notion-block-34b69776e2048074bc9ee9a2a81ce93d"><li>作业名 / 作业 ID。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480909eb6d3b442ab27e4"><li>用户账号（谁提交的，方便扣费或查权限）。</li></ul></ol></ol><ol start="2" class="notion-list notion-list-numbered notion-block-34b69776e2048045afb5e1dbf2a9729e" style="list-style-type:decimal"><li><b>资源需求（Resource Requirements）：</b> —— <em>这是调度时的重要依据</em></li><ol class="notion-list notion-list-numbered notion-block-34b69776e2048045afb5e1dbf2a9729e" style="list-style-type:lower-alpha"><ul class="notion-list notion-list-disc notion-block-34b69776e20480178983f3018af602c3"><li>预计运行时间（需要多久）。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e204807aa0cdcef612e81b8f"><li>内存需求（需要多大空间）。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480e08453f51ddf074ffc"><li>外设需求（是否需要打印机、磁带机等）。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480b78979f625828e8d37"><li>库函数或文件需求。</li></ul></ol></ol><ol start="3" class="notion-list notion-list-numbered notion-block-34b69776e2048093abebfa2f14d42999" style="list-style-type:decimal"><li><b>调度信息（Scheduling Info）：</b></li><ol class="notion-list notion-list-numbered notion-block-34b69776e2048093abebfa2f14d42999" style="list-style-type:lower-alpha"><ul class="notion-list notion-list-disc notion-block-34b69776e2048070879dcb2836691d6d"><li><b>优先级：</b> 决定谁先进入内存。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480019b58d2e2611e3d2b"><li><b>作业状态：</b> 进入系统了（提交）、正在排队（后备）、正在跑（运行）、跑完了（完成）。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480c7b1d7cd7ed0251c55"><li><b>进入时间：</b> 记录作业是什么时候来的（用于计算等待时间）。</li></ul></ol></ol><ol start="4" class="notion-list notion-list-numbered notion-block-34b69776e2048041bb5bd61462d36786" style="list-style-type:decimal"><li><b>控制信息：</b></li><ol class="notion-list notion-list-numbered notion-block-34b69776e2048041bb5bd61462d36786" style="list-style-type:lower-alpha"><ul class="notion-list notion-list-disc notion-block-34b69776e204805aa7acfa26ba169666"><li>作业说明书的存放地址。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e2048047ab96f5a11ff7cab4"><li>程序及数据的存放起始地址。</li></ul></ol></ol><h5 class="notion-h notion-h4 notion-h-indent-2 notion-block-34b69776e204802c81bbc63de93c849b" data-id="34b69776e204802c81bbc63de93c849b"><span><div id="34b69776e204802c81bbc63de93c849b" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e204802c81bbc63de93c849b" title="作业运行的生命周期"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">作业运行的生命周期</span></span></h5><div class="notion-text notion-block-34b69776e20480f384f9dbe29424e8a9"><b>1. 收容阶段 (Admission Stage) —— “挂号排队”</b></div><ul class="notion-list notion-list-disc notion-block-34b69776e20480fe90a3c9afec96304a"><li><b>过程：</b> 当你把程序、数据和作业说明书交给操作系统时，系统会先检查一下：你的格式对不对？系统还有没有地方存你的信息？如果没问题，系统就为你创建一个 <b>JCB（作业控制块）</b>，把你放在磁盘的“后备队列”里。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e2048053a6c0d03df318da69"><li><b>对应状态：后备状态 (Backlog State)</b>。</li></ul><blockquote class="notion-quote notion-block-34b69776e204804a968cda39dc9f8bba"><div>你到了医院，在大厅挂了号，拿到了一个就诊号。此时你并没见到医生，而是在等候区坐着，你的名字出现在了候诊大屏幕上。<b>这个“号”就是 JCB，候诊大厅就是后备队列。</b></div></blockquote><div class="notion-text notion-block-34b69776e204808a9531c032de3ed27d"><b>2. 运行阶段 (Execution Stage) —— “进入诊室”</b></div><ul class="notion-list notion-list-disc notion-block-34b69776e204800dab29e27dae924c48"><li><b>过程：</b> 当系统资源（主要是内存）有空闲时，<b>作业调度程序</b>会从后备队列里挑一个作业，把它调入内存，为它分配必要的资源，并创建进程去执行它。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e2048037a3fafa8045142e59"><li><b>对应状态：运行状态 (Running State)</b>。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480148e2cd8f7e591de80"><li><b>注意：</b> 这里的“运行状态”是一个宏观概念。从作业进入内存开始，到它运行结束前，都属于这个阶段。在这个阶段里，作业对应的<b>进程</b>可能会在 CPU 上跑，也可能在等 IO。</li></ul><blockquote class="notion-quote notion-block-34b69776e20480759e3af0cf95452db6"><div>护士喊你的号了，你进入了诊室。医生给你开检查单、化验、诊断。这时候你正处于“被处理”的过程中。</div></blockquote><div class="notion-text notion-block-34b69776e20480f08cbfdc158089e8b1"><b>3. 完成阶段 (Termination Stage) —— “缴费取药走人”</b></div><ul class="notion-list notion-list-disc notion-block-34b69776e2048008b393e1bf0b37240d"><li><b>过程：</b> 作业的所有步骤都跑完了，或者中途出错了被强行停止。系统会开始“收尾工作”：把运行结果输出，回收分配给它的内存、设备等资源，最后把它的 <b>JCB 撤销</b>（从系统里注销）。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e2048005aa6ce1316ef26f1c"><li><b>对应状态：完成状态 (Finished State)</b>。</li></ul><blockquote class="notion-quote notion-block-34b69776e20480328e18d3f8b41aeb22"><div>医生看完了，你拿着处方去缴费、拿药。最后你离开了医院，医院的系统里显示你今天的就诊已经结束，你的号牌被回收了。</div></blockquote><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-34b69776e2048065a603ca3fafd80c08" data-id="34b69776e2048065a603ca3fafd80c08"><span><div id="34b69776e2048065a603ca3fafd80c08" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e2048065a603ca3fafd80c08" title="3.2.2 作业调度的主要任务"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3.2.2 作业调度的主要任务</span></span></h3><ul class="notion-list notion-list-disc notion-block-34b69776e20480c19f43c812cc2d5884"><li>根据JCB中的信息，检查系统中的资源能否满足作业对资源的需求。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e204809c89ebe15fc6c265a0"><li>按照一定的调度算法，从外存的后备队列中选取某些作业调入内存，并为它们创建进程、分配必要的资源。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480caac6ae07950786676"><li>将新创建的进程排在就绪队列上等待调度。</li></ul><div class="notion-text notion-block-34b69776e2048069b760f07ed4bd5c6c">每次执行调度都做决定：</div><ul class="notion-list notion-list-disc notion-block-34b69776e20480b6b8e4f54ed6b90d90"><li>接纳多少个作业？</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480eeb05cc996fc37a918"><li>接纳哪些作业？</li></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-34b69776e20480aeadf5efe6c16c2721" data-id="34b69776e20480aeadf5efe6c16c2721"><span><div id="34b69776e20480aeadf5efe6c16c2721" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480aeadf5efe6c16c2721" title="3.2.3 调度算法"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3.2.3 调度算法</span></span></h3><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e20480a4a318c424857b6f87" data-id="34b69776e20480a4a318c424857b6f87"><span><div id="34b69776e20480a4a318c424857b6f87" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480a4a318c424857b6f87" title="1. 先来先服务——FCFS (First-Come, First-Served)"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">1. 先来先服务——<b>FCFS (First-Come, First-Served)</b></span></span></h4><ul class="notion-list notion-list-disc notion-block-34b69776e20480438c8fde8d3e78faec"><li>核心逻辑：谁先来，谁先办</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480789979c4a50c881db9"><li>唯一准则就是<b>时间顺序。</b></li><ul class="notion-list notion-list-disc notion-block-34b69776e20480789979c4a50c881db9"><li><b>判断标准：</b> 等待时间。谁在队列里等得最久，谁就最优先。</li><li><b>无视因素：</b> 它完全不看这个作业需要运行多久。哪怕排在第一位的作业需要跑 10 小时，而后面排队的作业只需要 1 秒，系统也会先处理那个 10 小时的。</li></ul></ul><ul class="notion-list notion-list-disc notion-block-34b69776e204807cbeb1ee9e0b380e79"><li>优点</li><ul class="notion-list notion-list-disc notion-block-34b69776e204807cbeb1ee9e0b380e79"><ol start="1" class="notion-list notion-list-numbered notion-block-34b69776e204809a88edeb16ad7cc8cc" style="list-style-type:decimal"><li><b>最公平：</b> 谁先来谁先办，谁也没意见。</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-34b69776e204807b9591d4f4ca972817" style="list-style-type:decimal"><li><b>实现极其简单：</b> 只要一个 FIFO（先进先出）队列就行，算法开销极小。</li></ol></ul></ul><ul class="notion-list notion-list-disc notion-block-34b69776e2048080ab1dee469c798802"><li>缺点</li><ul class="notion-list notion-list-disc notion-block-34b69776e2048080ab1dee469c798802"><ol start="1" class="notion-list notion-list-numbered notion-block-34b69776e20480579aa1dbae554a046d" style="list-style-type:decimal"><li><b>对“短作业”非常不友好：</b> 短作业如果跟在长作业后面，周转时间会变得非常长。</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-34b69776e204803a96fefa6d965166d2" style="list-style-type:decimal"><li><b>平均等待时间较长：</b> 整体效率通常不如其他算法。</li></ol></ul></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e20480a0bd70ee4bce425410" data-id="34b69776e20480a0bd70ee4bce425410"><span><div id="34b69776e20480a0bd70ee4bce425410" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480a0bd70ee4bce425410" title="2. 短作业优先——SJF（Short Job First）"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">2. 短作业优先——SJF（<b>Short Job First</b>）</span></span></h4><div class="notion-callout notion-gray_background_co notion-block-34b69776e20480628522c51987bea121"><div class="notion-page-icon-inline notion-page-icon-span"><span class="notion-page-icon" role="img" aria-label="💡">💡</span></div><div class="notion-callout-text"><div class="notion-text notion-block-34b69776e20480869ca1f677544f43fd">在实际情况中，短作业(进程)占有很大比例，为了能使它们能比长作业优先执行，而产生了短作业优先调度算法。   </div></div></div><ul class="notion-list notion-list-disc notion-block-34b69776e2048090934aec5a0dcae858"><li>核心逻辑：<b>谁快谁先上</b></li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480858a2af421088fe2f8"><li>唯一准则是作业的<b>预计运行时间（估计时长）</b></li><ul class="notion-list notion-list-disc notion-block-34b69776e20480858a2af421088fe2f8"><li>时间越短，优先级越高</li><li>实现：当系统准备从后备队列挑选作业进入内存时，它会扫描一遍所有的作业，找出那个“干活最快”的。</li></ul></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480a4b9e3dcea09ba4362"><li>缺点</li><ul class="notion-list notion-list-disc notion-block-34b69776e20480a4b9e3dcea09ba4362"><ol start="1" class="notion-list notion-list-numbered notion-block-34b69776e204805a94f7c5a0c5f1a63a" style="list-style-type:decimal"><li><b>估计时间不准确：</b></li><ol class="notion-list notion-list-numbered notion-block-34b69776e204805a94f7c5a0c5f1a63a" style="list-style-type:lower-alpha"><ul class="notion-list notion-list-disc notion-block-34b69776e204809e9d74dbdcbebf2070"><li>作业还没跑，系统怎么知道它要跑多久？这通常是用户在提交作业时<b>自己填写的</b>。如果用户为了早点运行而故意把时间填短，或者用户自己也预估不准，就会导致调度不准确。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480b997c9d17e0e49bb42"><li>一般会偏长估计。</li></ul></ol></ol><ol start="2" class="notion-list notion-list-numbered notion-block-34b69776e20480b8bbcdc5cabef85bf2" style="list-style-type:decimal"><li><b>“饥饿”现象（Starvation）：</b></li><ol class="notion-list notion-list-numbered notion-block-34b69776e20480b8bbcdc5cabef85bf2" style="list-style-type:lower-alpha"><ul class="notion-list notion-list-disc notion-block-34b69776e20480bb87e1ff5bac9fc7e2"><li>如果系统里源源不断地有短作业进来，那么那些长作业（大项目）就永远得不到运行的机会，只能在后备队列里“饿死”。</li></ul></ol></ol><ol start="3" class="notion-list notion-list-numbered notion-block-34b69776e20480efa3eff32e4fb44bb8" style="list-style-type:decimal"><li><b>不考虑紧急程度：</b></li><ol class="notion-list notion-list-numbered notion-block-34b69776e20480efa3eff32e4fb44bb8" style="list-style-type:lower-alpha"><ul class="notion-list notion-list-disc notion-block-34b69776e20480b9aad0d3774b91b8cd"><li>它只看长短。一个很紧急但时间略长的任务，会被一个不紧急但很短的任务插队。</li></ul></ol></ol></ul></ul><table class="notion-simple-table notion-block-34b69776e20480a0b15ef0bf20bdd46b"><tbody><tr class="notion-simple-table-row notion-simple-table-header-row notion-block-34b69776e20480be9067f5ddb9c7ce6e"><td class="" style="width:125.328125px"><div class="notion-simple-table-cell">特性</div></td><td class="" style="width:219.33333333333334px"><div class="notion-simple-table-cell"><b>FCFS (先来先服务)</b></div></td><td class="" style="width:288.328125px"><div class="notion-simple-table-cell"><b>SJF (短作业优先)</b></div></td></tr><tr class="notion-simple-table-row notion-block-34b69776e20480d9a318e05d4b812a0d"><td class="" style="width:125.328125px"><div class="notion-simple-table-cell"><b>判定标准</b></div></td><td class="" style="width:219.33333333333334px"><div class="notion-simple-table-cell">到达时间</div></td><td class="" style="width:288.328125px"><div class="notion-simple-table-cell">运行时间</div></td></tr><tr class="notion-simple-table-row notion-block-34b69776e2048096af1be8e6ea8ffdc0"><td class="" style="width:125.328125px"><div class="notion-simple-table-cell"><b>公平性</b></div></td><td class="" style="width:219.33333333333334px"><div class="notion-simple-table-cell">对长短作业都公平</div></td><td class="" style="width:288.328125px"><div class="notion-simple-table-cell">对短作业极其偏心，对长作业不公</div></td></tr><tr class="notion-simple-table-row notion-block-34b69776e204802da534c088254dfbc5"><td class="" style="width:125.328125px"><div class="notion-simple-table-cell"><b>效率</b></div></td><td class="" style="width:219.33333333333334px"><div class="notion-simple-table-cell">平均等待时间较长</div></td><td class="" style="width:288.328125px"><div class="notion-simple-table-cell"><b>平均等待时间最短</b>（在所有非抢占算法中）</div></td></tr><tr class="notion-simple-table-row notion-block-34b69776e204800e8177c9a0942f1c72"><td class="" style="width:125.328125px"><div class="notion-simple-table-cell"><b>风险</b></div></td><td class="" style="width:219.33333333333334px"><div class="notion-simple-table-cell">无</div></td><td class="" style="width:288.328125px"><div class="notion-simple-table-cell">可能导致长作业“饥饿”</div></td></tr></tbody></table><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e20480699549f94a2ddd6300" data-id="34b69776e20480699549f94a2ddd6300"><span><div id="34b69776e20480699549f94a2ddd6300" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480699549f94a2ddd6300" title="3. 优先级调度算法——PSA（Priority-Scheduling Algorithm）"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3. 优先级调度算法——PSA（Priority-Scheduling Algorithm）</span></span></h4><ul class="notion-list notion-list-disc notion-block-34b69776e20480de9183d688c1401a07"><li>前两种算法都无法反应作业的“紧迫程度“。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e2048050a060c895d2a19467"><li>只看“等了多久”或“要跑多久”是不够的，我们需要一种能直接定义“这个任务有多急、多重要”的机制。</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e204801b9da6d917edb9f41f" data-id="34b69776e204801b9da6d917edb9f41f"><span><div id="34b69776e204801b9da6d917edb9f41f" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e204801b9da6d917edb9f41f" title="4. 高响应比优先算法——HRRN（Highest Response Ratio Next）"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">4. 高响应比优先算法——HRRN（Highest Response Ratio Next）</span></span></h4><div class="notion-text notion-block-34b69776e204809eb0f3d0ad543e79c4"><em>——对 FCFS（先来先服务）和 SJF（短作业优先）的一种</em><em><b>综合折中</b></em><em>。</em></div><ul class="notion-list notion-list-disc notion-block-34b69776e20480d3a45cf49e9b957148"><li>目标：既要照顾短作业，又要保证长作业不会等太久。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480399a1dcd4ba4eb9320"><li>方法：</li><ul class="notion-list notion-list-disc notion-block-34b69776e20480399a1dcd4ba4eb9320"><li>引入响应比（<!-- -->），谁分高谁就先执行</li></ul></ul><ul class="notion-list notion-list-disc notion-block-34b69776e204805fac92cccb68ec7f68"><li>原理：</li><ul class="notion-list notion-list-disc notion-block-34b69776e204805fac92cccb68ec7f68"><li>对短作业：因为分母（要求服务时间）很小，所以即使它刚来（等待时间短），它的响应比 <!-- --> 也会迅速变得很高。这体现了 <b>SJF（短作业优先）</b> 的优点。</li><li><b>对长作业：</b> 虽然分母很大，一开始 <!-- --> 很低。但随着它在队里<b>等得越来越久</b>，分子上的“等待时间”不断变大，它的 <!-- --> 会慢慢涨上来。等到了一定程度，它总会变成全场最高，从而获得处理机。这解决了 <b>“饥饿”问题</b>，体现了公平性。</li></ul></ul></div><div class="notion-sync-block notion-block-34b69776e204800eb3cdea1a983cd37c"><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-34b69776e204800db88be8b29551684d" data-id="34b69776e204800db88be8b29551684d"><span><div id="34b69776e204800db88be8b29551684d" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e204800db88be8b29551684d" title="3.3 进程调度"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3.3 进程调度</span></span></h2><div class="notion-text notion-block-34b69776e20480eea214e3ec7d1c6ba7"><em>——从就绪队列中选一个进程，把 CPU 分配给它。</em></div><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-34b69776e2048029a726c4d9d37a0f9a" data-id="34b69776e2048029a726c4d9d37a0f9a"><span><div id="34b69776e2048029a726c4d9d37a0f9a" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e2048029a726c4d9d37a0f9a" title="3.3.1 进程调度的任务、机制与方式"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3.3.1 进程调度的任务、机制与方式</span></span></h3><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e2048075ba8dc2f46ec4ae6f" data-id="34b69776e2048075ba8dc2f46ec4ae6f"><span><div id="34b69776e2048075ba8dc2f46ec4ae6f" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e2048075ba8dc2f46ec4ae6f" title="任务（上下文切换）"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">任务（上下文切换）</span></span></h4><div class="notion-text notion-block-34b69776e2048038ab7ed1b75398a0fd"><b>1. 保存处理机的现场信息（现场“存档”）</b></div><ul class="notion-list notion-list-disc notion-block-34b69776e20480cdae1dcd637dd4b313"><li><b>含义：</b> 当一个正在运行的进程（比如 A）时间到了或者被抢占时，系统必须把 A 运行到哪一步了、寄存器里的数值是多少、程序计数器（PC）指到哪了，通通记下来。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480f49bcfdb41a856a80d"><li><b>存到哪：</b> 存入该进程的 <b>PCB（进程控制块）</b> 中。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e204809d9f8dedacd95c5d3c"><li><b>目的：</b> 就像打游戏存档一样，下次轮到 A 运行的时候，能从断点处精准“读档”继续跑，而不是从头开始。</li></ul><div class="notion-text notion-block-34b69776e2048014aa4ed4705417d75d"><b>2. 按某种算法选取进程（决定“谁上”）</b></div><ul class="notion-list notion-list-disc notion-block-34b69776e20480dca533ebeb3b925a7b"><li><b>含义：</b> 调度程序（Scheduler）查看“就绪队列”，里面排了一堆等着干活的进程。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e2048010aedef99cc51d350e"><li><b>依据：</b> 这就是我们之前讨论的<b>调度算法</b>。</li><ul class="notion-list notion-list-disc notion-block-34b69776e2048010aedef99cc51d350e"><li>是按<b>响应比（HRRN）</b>选？</li><li>还是按<b>时间片轮转（RR）</b>选？</li><li>还是按<b>短作业优先（SJF）</b>选？</li></ul></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480e39a0dd8a76d23f746"><li><b>目的：</b> 按照系统的目标（如：追求公平、追求效率、追求响应速度）选出最合适的那个进程（比如进程 B）。</li></ul><div class="notion-text notion-block-34b69776e20480d7ae9af74358adb47d"><b>3. 把处理器分配给进程（正式“换班”）</b></div><ul class="notion-list notion-list-disc notion-block-34b69776e20480dbbe25e725de75e0f9"><li><b>含义：</b> 这一步由<b>分派程序（Dispatcher）</b>执行。它会做两件事：</li><ul class="notion-list notion-list-disc notion-block-34b69776e20480dbbe25e725de75e0f9"><ol start="1" class="notion-list notion-list-numbered notion-block-34b69776e2048069b67ff8f0289ddcb2" style="list-style-type:decimal"><li>从进程 B 的 PCB 中取出上次保存的现场信息，恢复到 CPU 的寄存器里（“读档”）。</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-34b69776e204808ca7b7fb4ac1430363" style="list-style-type:decimal"><li>把 CPU 的控制权真正交给进程 B，让它开始执行。</li></ol></ul></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480818600d582acfaea47"><li><b>目的：</b> 让选中的进程真正跑起来。</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e20480b49a30edcaa7d2e03c" data-id="34b69776e20480b49a30edcaa7d2e03c"><span><div id="34b69776e20480b49a30edcaa7d2e03c" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480b49a30edcaa7d2e03c" title="机制"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">机制</span></span></h4><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-34b69776e204808c9e1ecb6e1473c951"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:505.984375px;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A7a32d4f5-4b9a-47cb-94a1-4b7f69fab27e%3Aimage.png?table=block&amp;id=34b69776-e204-808c-9e1e-cb6e1473c951&amp;t=34b69776-e204-808c-9e1e-cb6e1473c951" alt="notion image" loading="lazy" decoding="async"/></div></figure><div class="notion-text notion-block-34b69776e20480fbb0b5d04ef994bfdb">主要有三部分构成：</div><div class="notion-text notion-block-34b69776e20480e98d3cfe1a0d0a3ca5"><b>1. 排队器 (Queuer) —— “排号员”</b></div><ul class="notion-list notion-list-disc notion-block-34b69776e204801cb99dd2dd21cc7d9a"><li><b>功能：</b> 当一个进程进入就绪状态时（比如刚创建完，或者刚从阻塞状态醒来），排队器负责把它插入到相应的<b>就绪队列</b>中。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e204806cbfafc94293c7c7a5"><li><b>逻辑：</b> 为了提高效率，系统通常不只有一个队列。排队器会根据进程的优先级、类型（是计算密集型还是 I/O 密集型）把它们送往不同的“窗口”排队。</li></ul><div class="notion-text notion-block-34b69776e20480858603fb01c83161f7"><b>2. 分派器 (Dispatcher) —— “叫号员”</b></div><ul class="notion-list notion-list-disc notion-block-34b69776e204805ab2f2f982475c7ac3"><li><b>功能：</b> 根据<b>调度算法</b>，从就绪队列中选出一个进程。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e2048075a443c299410d6e8a"><li><b>动作：</b> 它是做决策的那个“大脑”。它决定了谁是下一个作业上 CPU 运行。</li></ul><div class="notion-text notion-block-34b69776e2048028a769d8e294cc77fd"><b>3. 上下文切换器 (Context Switcher) —— “换班员”</b></div><ul class="notion-list notion-list-disc notion-block-34b69776e204801bbe3cde0a173170e5"><li><b>功能：</b> 这是真正干体力活的，负责处理“换班”时的细节。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480b0b391e3213cf31e32"><li><b>流程（图中虚线部分）：</b></li><ul class="notion-list notion-list-disc notion-block-34b69776e20480b0b391e3213cf31e32"><ol start="1" class="notion-list notion-list-numbered notion-block-34b69776e204807c8e2bcb3f37bb3c4c" style="list-style-type:decimal"><li><b>保存现场：</b> 把当前正在 CPU 上跑的进程的各种数据（寄存器数值、程序计数器等）塞回它对应的 <b>PCB（进程控制块）</b> 里。</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-34b69776e20480ae86cae66b923bbffe" style="list-style-type:decimal"><li><b>恢复现场：</b> 把分派器选中的那个新进程的 <b>PCB</b> 里的数据拿出来，填到 CPU 的寄存器里。</li></ol><ol start="3" class="notion-list notion-list-numbered notion-block-34b69776e20480f99469e32173b5d298" style="list-style-type:decimal"><li><b>移交控制权：</b> 让 CPU 开始跑新进程的代码。</li></ol></ul></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e204803194e7ef1d01b45878" data-id="34b69776e204803194e7ef1d01b45878"><span><div id="34b69776e204803194e7ef1d01b45878" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e204803194e7ef1d01b45878" title="进程调度方式"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">进程调度方式</span></span></h4><h5 class="notion-h notion-h4 notion-h-indent-3 notion-block-34b69776e2048036b831dd3171506d21" data-id="34b69776e2048036b831dd3171506d21"><span><div id="34b69776e2048036b831dd3171506d21" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e2048036b831dd3171506d21" title="1. 非抢占方式"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">1. 非抢占方式</span></span></h5><div class="notion-text notion-block-34b69776e20480bf98a9c26e6539a31e"><em>——任务结束才能换作业</em></div><ul class="notion-list notion-list-disc notion-block-34b69776e20480198f30d80628dc9e14"><li>一旦把处理机分配给某进程后，就一直让它运行下去</li><ul class="notion-list notion-list-disc notion-block-34b69776e20480198f30d80628dc9e14"><li>不会因为时钟中断或任何其它原因去抢占当前正在运行进程的处理机</li><li>直至该进程完成，或发生某事件而被阻塞（如读硬盘，请求IO）时，才把处理机分配给其它进程。</li></ul></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480a8868ed7d4747b2beb"><li><b>优点：</b> 简单，系统开销小（不需要频繁切换）。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480248b23c3705512d6fc"><li><b>缺点：</b> 极不公平。如果前面排了一个“大长作业”，后面的短作业或紧急任务只能干等。</li></ul><h5 class="notion-h notion-h4 notion-h-indent-3 notion-block-34a69776e20480829220e689323e40d1" data-id="34a69776e20480829220e689323e40d1"><span><div id="34a69776e20480829220e689323e40d1" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34a69776e20480829220e689323e40d1" title="2. 抢占方式"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">2. 抢占方式</span></span></h5><div class="notion-text notion-block-34b69776e2048032949fd1ef688efbd1"><em>——随时叫停更换任务</em></div><ul class="notion-list notion-list-disc notion-block-34b69776e204803c8e78ce6b48f4ab90"><li>一旦发现更重要（优先级更高）或者更紧急（时间片到了）的进程，会暂停某个正在执行的进程，让其重新排队，把 CPU 让给留那个一个进程。</li></ul><div class="notion-text notion-block-34b69776e2048073821cc705348765d9">三个原则：</div><ol start="1" class="notion-list notion-list-numbered notion-block-34b69776e204802484a5f5f60c542211" style="list-style-type:decimal"><li><b>优先权原则：</b> 来了个更重要的（高优先级）进程。</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-34b69776e204801ea2c2cd5e645244a1" style="list-style-type:decimal"><li><b>短作业优先原则：</b> 新来的作业明显比现在的快得多。</li></ol><ol start="3" class="notion-list notion-list-numbered notion-block-34b69776e204802b9972ec2511b7fe83" style="list-style-type:decimal"><li><b>时间片原则：</b> 规定每个任务跑 10ms，到点就换（这是分时系统的基础）。</li></ol><div class="notion-callout notion-gray_background_co notion-block-34b69776e20480a2b139c2a4122b6f42"><div class="notion-page-icon-inline notion-page-icon-span"><span class="notion-page-icon" role="img" aria-label="💡">💡</span></div><div class="notion-callout-text"><div class="notion-text notion-block-34b69776e204804a9c70e2909eba14c4">目前的现代OS都采用抢占方式。</div><div class="notion-text notion-block-34b69776e2048008a472f74cb73e9ba9">原因：</div><ol start="1" class="notion-list notion-list-numbered notion-block-34b69776e20480dc9909cd55d236fc23" style="list-style-type:decimal"><li><b>公平性（批处理系统）：</b> 防止一个大任务一直霸占 CPU，让其他任务都能轮流用上。</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-34b69776e20480b19ddfe708cac20ec8" style="list-style-type:decimal"><li><b>人机交互（分时系统）：</b> 这是最关键的。你移动鼠标、敲击键盘，系统必须立刻“抢占”当前正在后台跑的程序来响应你。如果不抢占，你点个图标可能要等后台压缩完文件才有反应。</li></ol><ol start="3" class="notion-list notion-list-numbered notion-block-34b69776e2048011ad09f4421e1d7c37" style="list-style-type:decimal"><li><b>实时性（实时系统）：</b> 比如车载系统感应到要撞车了，这个“刹车进程”必须瞬间抢占正在放音乐的进程，否则后果不堪设想。</li></ol></div></div><ul class="notion-list notion-list-disc notion-block-34b69776e20480ec9578d8de23f7e6aa"><li>代价：系统开销大</li></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-34b69776e20480b48537e5fbd831f577" data-id="34b69776e20480b48537e5fbd831f577"><span><div id="34b69776e20480b48537e5fbd831f577" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480b48537e5fbd831f577" title="3.3.2 轮转调度算法（RR）"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3.3.2 轮转调度算法（RR）</span></span></h3><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e20480df8c10fb02eb2e3627" data-id="34b69776e20480df8c10fb02eb2e3627"><span><div id="34b69776e20480df8c10fb02eb2e3627" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480df8c10fb02eb2e3627" title="过程"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">过程</span></span></h4><ul class="notion-list notion-list-disc notion-block-34b69776e20480269b24d291a8f2037b"><li><b>排队：</b> 所有想要运行的进程按“先来先服务”（FCFS）原则排成一队。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e204801ca1c7c730390e929d"><li><b>定额：</b> 系统规定一个固定的时间长度，叫做<b>“时间片”</b>（Time Slice，如 30ms）。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480f68f2fe55ccf0db0eb"><li><b>分配：</b> CPU 只给排在队首的任务用这 30ms。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e2048033a659e6369917b54d"><li><b>轮换：</b> 30ms 时间一到，不管干没干完，打回队尾重新排队，CPU 给下一个任务用。</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e2048087a584e5a5dd48922b" data-id="34b69776e2048087a584e5a5dd48922b"><span><div id="34b69776e2048087a584e5a5dd48922b" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e2048087a584e5a5dd48922b" title="切换时机"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">切换时机</span></span></h4><div class="notion-text notion-block-34b69776e20480129445da6fd18ec185"><b>① 提前完成</b></div><div class="notion-text notion-block-34b69776e204806cbd48e45c83dd3ea9"><b>场景：</b> 给这个进程分配了 10ms，但它执行到第 5ms 就把任务跑完了。</div><div class="notion-text notion-block-34b69776e204805bbe7ecccd28eff345"><b>动作：</b> 系统会<b>立即</b>把它从就绪队列里删掉，然后调下一个进程上来，并且给下一个进程<b>重新计时</b>（开始一个新的完整时间片）。</div><div class="notion-text notion-block-34b69776e20480e0b465d8c54694eb86"><b>② 时间片用完</b></div><div class="notion-text notion-block-34b69776e20480d6a71fd49903771897"><b>场景：</b> 10ms 到了，但这个大工程还没搞定。</div><div class="notion-text notion-block-34b69776e2048084930eeb7a6c6eb457"><b>动作：</b> 计时器发出中断信号，调度程序强行中止该进程。为了公平，系统把它<b>踢到就绪队列的末尾</b>去重新排队，然后把 CPU 给现在的队首进程。</div><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e204805ab186cf798a868633" data-id="34b69776e204805ab186cf798a868633"><span><div id="34b69776e204805ab186cf798a868633" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e204805ab186cf798a868633" title="时间片大小的确定"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">时间片大小的确定</span></span></h4><div class="notion-text notion-block-34b69776e20480f38672cbab3ae5e631">时间片大小的确定对系统性能影响很大。</div><h5 class="notion-h notion-h4 notion-h-indent-3 notion-block-34b69776e20480f6b396f1639635a31f" data-id="34b69776e20480f6b396f1639635a31f"><span><div id="34b69776e20480f6b396f1639635a31f" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480f6b396f1639635a31f" title="对响应时间的影响"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">对响应时间的影响</span></span></h5><div class="notion-text notion-block-34b69776e20480e6b258d563b87bf940">这张图揭示了时间片 q 与用户交互时间 s（比如你点一下鼠标，系统处理完所需的时间）之间的博弈。</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-34b69776e20480598e4aca3c667ed4e2"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:477.9921875px;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A76cce239-56a0-44c7-9809-f1763f872250%3Aimage.png?table=block&amp;id=34b69776-e204-8059-8e4a-ca3c667ed4e2&amp;t=34b69776-e204-8059-8e4a-ca3c667ed4e2" alt="notion image" loading="lazy" decoding="async"/></div></figure><ul class="notion-list notion-list-disc notion-block-34b69776e20480aca802f6270014b1af"><li><b>图 (a) </b><b>（理想情况）：</b></li><ul class="notion-list notion-list-disc notion-block-34b69776e20480aca802f6270014b1af"><li>时间片 q 足够大，能覆盖掉大部分简单的交互任务。</li><li><b>结果：</b> 进程在第一个时间片内就干完活了，<b>响应时间就是 s</b>。用户感觉非常快，系统也没有多余的切换开销。</li></ul></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480f0a0d0e95a334adbb7"><li><b>图 (b) </b><b>（糟糕情况）：</b></li><ul class="notion-list notion-list-disc notion-block-34b69776e20480f0a0d0e95a334adbb7"><li>时间片太短，任务还没干完就被强行踢下 CPU 去排队。</li><li><b>结果：</b> 进程被迫等待其他进程运行完后，再拿第二个时间片才能完成。</li><li><b>代价：</b> 响应时间变成了原来的好几倍（图中的总长度 s 变长了），用户会明显感到<b>掉帧或卡顿</b>。</li></ul></ul><h5 class="notion-h notion-h4 notion-h-indent-3 notion-block-34b69776e20480d1bc19c727b87d184f" data-id="34b69776e20480d1bc19c727b87d184f"><span><div id="34b69776e20480d1bc19c727b87d184f" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480d1bc19c727b87d184f" title="对周转时间的影响"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">对周转时间的影响</span></span></h5><table class="notion-simple-table notion-block-34b69776e20480799534d7bf486d4ee1"><tbody><tr class="notion-simple-table-row notion-simple-table-header-row notion-block-34b69776e20480a7a982cb6de4014cbd"><td class="" style="width:94px"><div class="notion-simple-table-cell">进程名称及指标</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">A</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">B</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">C</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">D</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">E</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">平均 (Average)</div></td></tr><tr class="notion-simple-table-row notion-block-34b69776e20480b49c0fca5a78b32e9e"><td class="" style="width:94px"><div class="notion-simple-table-cell"><b>到达时间</b></div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">0</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">1</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">2</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">3</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">4</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">-</div></td></tr><tr class="notion-simple-table-row notion-block-34b69776e204803cb8bfce4716eeb280"><td class="" style="width:94px"><div class="notion-simple-table-cell"><b>服务时间</b></div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">4</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">3</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">4</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">2</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">4</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">-</div></td></tr><tr class="notion-simple-table-row notion-block-34b69776e2048036a83ad902c49f9291"><td class="" style="width:94px"><div class="notion-simple-table-cell"><b>RR (q=1) 情况</b></div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">ㅤ</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">ㅤ</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">ㅤ</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">ㅤ</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">ㅤ</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">ㅤ</div></td></tr><tr class="notion-simple-table-row notion-block-34b69776e20480be8843d6f1391304ca"><td class="" style="width:94px"><div class="notion-simple-table-cell">完成时刻</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">15</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">12</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">16</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">9</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">17</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">-</div></td></tr><tr class="notion-simple-table-row notion-block-34b69776e2048074b5d6f490e8f7bcd1"><td class="" style="width:94px"><div class="notion-simple-table-cell">周转时间</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">15</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">11</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">14</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">6</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">13</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell"><b>11.8</b></div></td></tr><tr class="notion-simple-table-row notion-block-34b69776e20480ff8431cdb8334559b9"><td class="" style="width:94px"><div class="notion-simple-table-cell">带权周转时间</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">3.75</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">3.67</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">3.5</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">3</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">3.33</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell"><b>3.46</b></div></td></tr><tr class="notion-simple-table-row notion-block-34b69776e20480e8b296c201bb53ddbb"><td class="" style="width:94px"><div class="notion-simple-table-cell"><b>RR (q=4) 情况</b></div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">ㅤ</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">ㅤ</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">ㅤ</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">ㅤ</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">ㅤ</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">ㅤ</div></td></tr><tr class="notion-simple-table-row notion-block-34b69776e20480ea931dd4e540542019"><td class="" style="width:94px"><div class="notion-simple-table-cell">完成时刻</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">4</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">7</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">11</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">13</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">17</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">-</div></td></tr><tr class="notion-simple-table-row notion-block-34b69776e204804d9ebdf9515f376a20"><td class="" style="width:94px"><div class="notion-simple-table-cell">周转时间</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">4</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">6</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">9</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">10</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">13</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell"><b>8.4</b></div></td></tr><tr class="notion-simple-table-row notion-block-34b69776e204806fb6a7d87d89ce7fd1"><td class="" style="width:94px"><div class="notion-simple-table-cell">带权周转时间</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">1</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">2</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">2.25</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">5</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell">3.33</div></td><td class="" style="width:94px"><div class="notion-simple-table-cell"><b>2.5</b></div></td></tr></tbody></table><ul class="notion-list notion-list-disc notion-block-34b69776e204809db690ce7b431760dd"><li>在本例中，<b>q=4 的表现明显优于 q=1</b>。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e2048090b30df9941c7c16d9"><li>因为 q=4 足够大，使得像 A、B、C 这样的进程在第一个时间片内或较短时间内就能完成，减少了进程在就绪队列中往返排队的次数，从而大幅降低了平均周转时间（从 11.8 降到 8.4）。</li></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-34b69776e20480acbad8c1576a22f710" data-id="34b69776e20480acbad8c1576a22f710"><span><div id="34b69776e20480acbad8c1576a22f710" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480acbad8c1576a22f710" title="3.3.3 优先级调度算法"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3.3.3 优先级调度算法</span></span></h3><blockquote class="notion-quote notion-block-34b69776e204804d981cf43bfeb222f6"><div>如果说之前的“轮转调度（RR）”是追求<b>绝对公平</b>，那么“优先级调度”就是追求<b>效率和差异化服务</b>（重要的、急的事先办）。</div></blockquote><div class="notion-text notion-block-34b69776e2048036a985f5ec629c67b3">根据优先级的变动情况，分为以下两类：</div><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e20480428601d589ffde5cf1" data-id="34b69776e20480428601d589ffde5cf1"><span><div id="34b69776e20480428601d589ffde5cf1" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480428601d589ffde5cf1" title="1. 静态优先级"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">1. 静态优先级</span></span></h4><ul class="notion-list notion-list-disc notion-block-34b69776e20480fd9992dbffc4975d2c"><li><b>定义：</b> 在进程创建时就定死了，整个运行期间优先数保持不变。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480c79a1fc9e892ecda0c"><li>定数依据：</li><ul class="notion-list notion-list-disc notion-block-34b69776e20480c79a1fc9e892ecda0c"><ol start="1" class="notion-list notion-list-numbered notion-block-34b69776e2048065996ac5d39876d5c3" style="list-style-type:decimal"><li><b>进程类型：</b> 系统进程优先级通常高于用户进程。</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-34b69776e204800c97b2c604d07a3018" style="list-style-type:decimal"><li><b>资源需求：</b> 对 CPU 要求不高的进程（如频繁等待读写磁盘的进程）往往会被给高一点的优先级，这样能提高整体资源利用率。</li></ol><ol start="3" class="notion-list notion-list-numbered notion-block-34b69776e2048075bbcce8b42bc517c3" style="list-style-type:decimal"><li><b>用户要求：</b> 比如管理员手动给某个关键任务提高优先级。</li></ol></ul></ul><ul class="notion-list notion-list-disc notion-block-34b69776e2048081b4b7ca33389bf4c3"><li><b>缺点：</b> 可能会导致<b>“饥饿”现象</b>。如果系统里一直有高优先级的新任务进来，低优先级的任务可能一辈子都轮不到运行。</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e20480b5b55dc0f9bac55d0d" data-id="34b69776e20480b5b55dc0f9bac55d0d"><span><div id="34b69776e20480b5b55dc0f9bac55d0d" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480b5b55dc0f9bac55d0d" title="2. 动态优先级"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">2. 动态优先级</span></span></h4><ul class="notion-list notion-list-disc notion-block-34b69776e2048063a95ad24b494bf07b"><li><b>定义：</b> 进程刚创建时有一个初始值，但在运行过程中，它的值会根据情况<b>“自动调整”</b>。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480c8b4e0d33742259070"><li>规则：</li><ul class="notion-list notion-list-disc notion-block-34b69776e20480c8b4e0d33742259070"><li><b>等待越久，级别越高：</b> 这叫“老化”（Aging）。为了防止低优先级进程被饿死，如果它在队列里等太久，系统会自动给它“提拔”一级，直到它能被运行。</li><li><b>运行越久，级别越低：</b> 如果一个进程占着 CPU 不撒手，系统会慢慢调低它的优先级，让出位置给别人。</li></ul></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-34b69776e204802baf67f17a4244fbb1" data-id="34b69776e204802baf67f17a4244fbb1"><span><div id="34b69776e204802baf67f17a4244fbb1" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e204802baf67f17a4244fbb1" title="3.3.4 多队列调度算法"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3.3.4 多队列调度算法</span></span></h3><div class="notion-text notion-block-34b69776e20480e690f8f81bcff4653c"><em>——为了解决之前算法的一个通病：</em><em><b>“一刀切”</b></em><em>。</em></div><ul class="notion-list notion-list-disc notion-block-34b69776e20480529263ccb15ed3f594"><li><b>以前的做法：</b> 系统里只有一个“就绪队列”，不管是系统关键进程、交互式进程（如键盘输入），还是后台大计算进程（如视频渲染），大家都挤在一起。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480c1a0cac7d873964f87"><li><b>单一策略的局限性：</b> 如果用轮转法（RR），对后台任务不划算（切换太频繁）；如果用先来先服务（FCFS），对前台交互任务太慢。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e204809a9a2bce9340e61418"><li><b>多处理机环境：</b> 在多核 CPU 时代，如果只有一个队列，所有的 CPU 核心都要去抢这一个队列，会造成严重的拥堵和管理混乱。</li></ul><details class="notion-toggle notion-block-34b69776e204805887cce9a30ad52823"><summary>工作原理</summary><div><div class="notion-text notion-block-34b69776e204804d9df8c71259e4de97">它的核心思想是：<b>“分类管理，因材施教”</b>。</div><div class="notion-text notion-block-34b69776e2048039acf4e2b9fd84dfa8">系统将就绪队列拆分成多个独立的队列，每个队列代表一种特定的任务类型。</div><ul class="notion-list notion-list-disc notion-block-34b69776e20480a1985fdbdc69ad6dc7"><li><b>队列 1（高优先级）：</b> 放系统任务（如驱动、内核任务）。采用抢占式优先级算法。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480988c32fef3ce057058"><li><b>队列 2（中优先级）：</b> 放前台交互任务（如文档编辑器、浏览器）。采用<b>轮转法（RR）</b>，追求响应快。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e2048053b1acc3226a13426e"><li><b>队列 3（低优先级）：</b> 放后台批处理任务（如系统更新、杀毒扫描）。采用<b>先来先服务（FCFS）</b>，追求吞吐量。</li></ul></div></details><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-34b69776e20480559993d3e5e67f6611" data-id="34b69776e20480559993d3e5e67f6611"><span><div id="34b69776e20480559993d3e5e67f6611" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480559993d3e5e67f6611" title="3.3.5多级反馈队列（Multilevel Feedback Queue, MLFQ）调度算法"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>3.3.5多级反馈队列（Multilevel Feedback Queue, MLFQ）调度算法</b></span></span></h3><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e20480899297d8542b53bde5" data-id="34b69776e20480899297d8542b53bde5"><span><div id="34b69776e20480899297d8542b53bde5" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480899297d8542b53bde5" title="1. 调度机制 (Mechanism)"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>1. 调度机制 (Mechanism)</b></span></span></h4><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-34b69776e20480f49525dc0a45212b13"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:430.984375px;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A51d88ef3-2670-4f9f-83ee-f4b67ac94111%3Aimage.png?table=block&amp;id=34b69776-e204-80f4-9525-dc0a45212b13&amp;t=34b69776-e204-80f4-9525-dc0a45212b13" alt="notion image" loading="lazy" decoding="async"/></div></figure><div class="notion-text notion-block-34b69776e2048026ab4eff7a96d56380">该算法通过设置多个队列并动态调整进程所属队列，实现了灵活调度：</div><ul class="notion-list notion-list-disc notion-block-34b69776e20480f889c8e1e9d796a564"><li><b>设置多个就绪队列：</b></li><ul class="notion-list notion-list-disc notion-block-34b69776e20480f889c8e1e9d796a564"><li><b>优先级逐级降低：</b> 队列1优先级最高，队列2次之，以此类推。</li><li><b>时间片逐级增长：</b> 优先级越高的队列，分配到的时间片越小。即：<!-- -->。</li></ul></ul><ul class="notion-list notion-list-disc notion-block-34b69776e204801aafe6cb8a7e0620a1"><li><b>进程在队列间的移动规则：</b></li><ul class="notion-list notion-list-disc notion-block-34b69776e204801aafe6cb8a7e0620a1"><li><b>新进程：</b> 首先放入<b>第一队列</b>末尾，按先来先服务（FCFS）等待调度。</li><li><b>降级处理：</b> 如果进程在当前队列的时间片内没跑完，调度程序会把它降级到<b>下一级队列</b>的末尾。</li><li><b>最终阶段：</b> 当进程降到最后一级（第 n 队列）时，该队列通常采用<b>时间片轮转（RR）</b>方式运行。</li></ul></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480c1b2cef3dc0571d755"><li><b>队列间的调度优先级：</b></li><ul class="notion-list notion-list-disc notion-block-34b69776e20480c1b2cef3dc0571d755"><li><b>高优先级优先：</b> 只有当第 <!-- --> 队列全部为空时，才会调度第 i 队列中的进程。</li><li><b>抢占机制：</b> 如果 CPU 正在执行第 i 队列的进程，此时有一个新进程进入了更高优先级的队列（<!-- -->，新进程会<b>抢占</b> CPU，被抢占的进程放回原队列末尾。</li></ul></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e20480429359db75660fadc3" data-id="34b69776e20480429359db75660fadc3"><span><div id="34b69776e20480429359db75660fadc3" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480429359db75660fadc3" title="2. 调度算法的性能 (Performance)"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>2. 调度算法的性能 (Performance)</b></span></span></h4><div class="notion-text notion-block-34b69776e204803398eed2bc8e91544c">该算法之所以被广泛应用，是因为它能同时兼顾各类用户的需求：</div><ol start="1" class="notion-list notion-list-numbered notion-block-34b69776e20480b689e5c8b5ccf6aa6a" style="list-style-type:decimal"><li><b>终端型用户（交互式）：</b></li><ol class="notion-list notion-list-numbered notion-block-34b69776e20480b689e5c8b5ccf6aa6a" style="list-style-type:lower-alpha"><ul class="notion-list notion-list-disc notion-block-34b69776e20480c5a270c8a2c50fac45"><li>这类任务通常很短（如点个鼠标、打个字）。由于它们一进入就在第一队列，且第一队列优先级最高，因此能获得<b>极快的响应时间</b>。</li></ul></ol></ol><ol start="2" class="notion-list notion-list-numbered notion-block-34b69776e204801ba256e6bace3218af" style="list-style-type:decimal"><li><b>短批处理作业用户：</b></li><ol class="notion-list notion-list-numbered notion-block-34b69776e204801ba256e6bace3218af" style="list-style-type:lower-alpha"><ul class="notion-list notion-list-disc notion-block-34b69776e20480d2bc86c488001c2eba"><li>这类任务通常在第一或第二队列的时间片内就能完成，周转时间非常短。</li></ul></ol></ol><ol start="3" class="notion-list notion-list-numbered notion-block-34b69776e2048051ac4cf73d29b5306a" style="list-style-type:decimal"><li><b>长批处理作业用户：</b></li><ol class="notion-list notion-list-numbered notion-block-34b69776e2048051ac4cf73d29b5306a" style="list-style-type:lower-alpha"><ul class="notion-list notion-list-disc notion-block-34b69776e20480f5b314fabac50b31fc"><li>这类任务虽然会不断下沉到低优先级队列，但<b>时间片会越来越大</b>。这意味着虽然它们被调度的次数少了，但一旦拿到 CPU，就能连续运行很久，减少了频繁切换的开销。</li></ul></ol></ol><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-34b69776e2048033a2dbf13250f50928" data-id="34b69776e2048033a2dbf13250f50928"><span><div id="34b69776e2048033a2dbf13250f50928" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e2048033a2dbf13250f50928" title="3.3.6 基于公平原则的调度算法"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3.3.6 基于公平原则的调度算法</span></span></h3><blockquote class="notion-quote notion-block-34b69776e2048051ab28c8ed78f21604"><div>与之前的“优先级调度”不同，这类算法不分贵贱，核心目标是公平，确保每个进程（或每个用户）都能得到应得的一份 CPU 时间。</div></blockquote><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e2048062aaead6f77aceb645" data-id="34b69776e2048062aaead6f77aceb645"><span><div id="34b69776e2048062aaead6f77aceb645" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e2048062aaead6f77aceb645" title="1. 保证调度算法 (Guaranteed Scheduling)"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>1. 保证调度算法 (Guaranteed Scheduling)</b></span></span></h4><div class="notion-text notion-block-34b69776e20480369cf3d086a2c23d70">这种算法向用户做出一个明确的承诺：<b>“如果你和其他</b><b>个任务一起跑，我保证你能分到 </b><b>的 CPU 时间。”</b></div><div class="notion-text notion-block-34b69776e20480c1a7d8e4d3d6e34023"><b>原理：</b></div><div class="notion-text notion-block-34b69776e20480cda277e06617146021">为了实现这个“合同”，系统需要不断地进行数学计算：</div><ol start="1" class="notion-list notion-list-numbered notion-block-34b69776e20480828257e45704485160" style="list-style-type:decimal"><li><b>记录实际时间：</b> 记录每个进程从创建到现在，到底在 CPU 上跑了多久（<b>实际时间</b>）。</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-34b69776e204800d978ede1b2330b752" style="list-style-type:decimal"><li><b>计算应得时间：</b> 计算从进程创建到现在，它本来“应该”分到多少时间（<b>总时间 / n</b>）。</li></ol><ol start="3" class="notion-list notion-list-numbered notion-block-34b69776e20480c28496dfe7fd726761" style="list-style-type:decimal"><li><b>计算比率：</b> <!-- -->。</li><ol class="notion-list notion-list-numbered notion-block-34b69776e20480c28496dfe7fd726761" style="list-style-type:lower-alpha"><ul class="notion-list notion-list-disc notion-block-34b69776e20480809ad0f2bb3de6e9f8"><li><b>比率 &lt; 1.0：</b> 说明你还没分够，系统欠你的。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e204800a83dad643e387f225"><li><b>比率 &gt; 1.0：</b> 说明你跑超了，占了别人的便宜。</li></ul></ol></ol><ol start="4" class="notion-list notion-list-numbered notion-block-34b69776e20480ecade2cef25a55dafa" style="list-style-type:decimal"><li><b>调度策略：</b> 调度程序总是挑选<b>比率最小</b>的进程来运行，直到它的比率追上别人为止。</li></ol><blockquote class="notion-quote notion-block-34b69776e20480879d4cc023a9cca5da"><div>就像分蛋糕，如果现在有 4 个人。系统会盯着看谁吃得最慢，然后就把叉子递给谁，直到他吃得跟别人一样多为止。</div></blockquote><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e20480fb963fcc15a8af68fb" data-id="34b69776e20480fb963fcc15a8af68fb"><span><div id="34b69776e20480fb963fcc15a8af68fb" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480fb963fcc15a8af68fb" title="2. 公平分享调度算法 (Fair Share Scheduling)"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>2. 公平分享调度算法 (Fair Share Scheduling)</b></span></span></h4><div class="notion-text notion-block-34b69776e204807b8a6dca74a2f58316"><em>——对“保证调度算法”的一个重要修正。</em></div><h5 class="notion-h notion-h4 notion-h-indent-3 notion-block-34b69776e2048069a450e7b1a7416900" data-id="34b69776e2048069a450e7b1a7416900"><span><div id="34b69776e2048069a450e7b1a7416900" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e2048069a450e7b1a7416900" title="公平性的漏洞"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>公平性的漏洞</b></span></span></h5><div class="notion-text notion-block-34b69776e20480b5a53ddca6d7890da2">如果只看“进程”层面的公平，会导致“用户”层面的不公平：</div><ul class="notion-list notion-list-disc notion-block-34b69776e204805c965ec7399b675092"><li><b>用户 A：</b> 开了 9 个下载任务（9 个进程）。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480f1bd3fdbfb2d270d26"><li><b>用户 B：</b> 只开了 1 个网页（1 个进程）。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e204806a8780def11bdc4774"><li>如果按“进程”平分，用户 A 会占掉 90% 的 CPU，而用户 B 只有 10%。这显然对用户 B 极度不公平。</li></ul><h5 class="notion-h notion-h4 notion-h-indent-3 notion-block-34b69776e20480578270f56ce292f990" data-id="34b69776e20480578270f56ce292f990"><span><div id="34b69776e20480578270f56ce292f990" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480578270f56ce292f990" title="它的核心逻辑"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>它的核心逻辑</b></span></span></h5><div class="notion-text notion-block-34b69776e20480eca474dc02b11fcd5e">公平分享算法把“公平”的维度从<b>进程</b>提升到了<b>用户</b>（或用户组）。</div><ul class="notion-list notion-list-disc notion-block-34b69776e204802aa293cac2be628118"><li>系统先给每个<b>用户</b>分配相等的 CPU 份额。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480e4a0dcdf91ca26af99"><li>比如两个用户，每人 50%。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480879329dfbfaeab0d29"><li>无论用户 A 开了多少个进程，这 9 个进程只能内部去分那 50% 的时间；而用户 B 的那 1 个进程能独享剩下的 50%。</li></ul></div><div class="notion-sync-block notion-block-34b69776e2048006aefde58b6e635137"><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-34a69776e2048091a3b9dad9eee4aca2" data-id="34a69776e2048091a3b9dad9eee4aca2"><span><div id="34a69776e2048091a3b9dad9eee4aca2" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34a69776e2048091a3b9dad9eee4aca2" title="3.4 实时调度"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3.4 实时调度</span></span></h2><div class="notion-text notion-block-34a69776e20480f5b44ce3ab3a3aee92"><em>——核心目标：必须满足截止时间要求</em></div><ul class="notion-list notion-list-disc notion-block-34b69776e204808ca1e7dc8b8fd5073a"><li>重点不在快，而是准时</li></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-34b69776e20480e2a456c06ed6b94fd2" data-id="34b69776e20480e2a456c06ed6b94fd2"><span><div id="34b69776e20480e2a456c06ed6b94fd2" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480e2a456c06ed6b94fd2" title="3.4.1 实现实时调度的基本条件"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3.4.1 实现实时调度的基本条件</span></span></h3><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e204802b9892e0bb2595b7eb" data-id="34b69776e204802b9892e0bb2595b7eb"><span><div id="34b69776e204802b9892e0bb2595b7eb" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e204802b9892e0bb2595b7eb" title="1. 提供必要的信息"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">1. 提供必要的信息</span></span></h4><div class="notion-text notion-block-34b69776e20480d2b4e5fecf1b739385">实时调度程序需要根据任务的紧急程度来排班，所以每个任务必须提前告知：</div><ul class="notion-list notion-list-disc notion-block-34b69776e2048051b8cfe68d4bfbe00d"><li><b>就绪时间：</b> 任务什么时候能开始。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480a6b135e26bad9c02d6"><li><b>截止时间：</b> 什么时候必须开始或完成（这是核心，没按时完成就算失败）。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e204809ab071d65e711f2a1f"><li><b>处理时间：</b> 任务跑完需要多久。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e2048047a449ea5c090dcbf2"><li><b>资源要求：</b> 需要哪些内存、外设资源。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480338d46f1e97b640e72"><li><b>优先级：</b> 是“死命令”（绝对优先级）还是“尽量快”（相对优先级）。</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e204800f913ef129318f3362" data-id="34b69776e204800f913ef129318f3362"><span><div id="34b69776e204800f913ef129318f3362" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e204800f913ef129318f3362" title="2. 系统处理能力强"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">2. 系统处理能力强</span></span></h4><div class="notion-text notion-block-34b69776e2048039813df17899cc176a"><b>核心公式</b>：</div><ul class="notion-list notion-list-disc notion-block-34b69776e2048019b0d8cc61f2819263"><li><b>：</b> 第 i 个任务的处理时间。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e204803c9cdeeec158f6445b"><li><b>：</b> 第 i 个任务的周期时间。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480a09344d43a1557ae36"><li><b>：</b> 这个任务占用了 CPU 多少百分比的“带宽”。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480dca8b2fbb5c946b982"><li><b>含义：</b> 如果所有周期性任务占用的 CPU 百分比之和大于 1（即 100%），说明 CPU 忙不过来，一定会有任务错过截止时间。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e204803081efc040c625f8af"><li><b>解决办法：</b> 增加 CPU 核心数，或者提高主频。</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e2048001b30dc71e99ca00d2" data-id="34b69776e2048001b30dc71e99ca00d2"><span><div id="34b69776e2048001b30dc71e99ca00d2" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e2048001b30dc71e99ca00d2" title="3. 采用抢占式调度机制"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3. 采用抢占式调度机制</span></span></h4><ul class="notion-list notion-list-disc notion-block-34b69776e204806ab96fe6ef53717c36"><li>在非实时系统中，一个任务可能一直占着 CPU。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480408a03ee6ee8b2e0dd"><li>在实时系统中，<b>必须是抢占式的</b>。一旦有一个更高优先级的实时任务（HRT）就绪，系统必须能够立刻打断当前任务，把 CPU 让给最紧急的任务，以确保截止时间不被错过。</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e204800e8bd5de5eb39d38c0" data-id="34b69776e204800e8bd5de5eb39d38c0"><span><div id="34b69776e204800e8bd5de5eb39d38c0" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e204800e8bd5de5eb39d38c0" title="4. 具有快速切换机制"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">4. 具有快速切换机制</span></span></h4><div class="notion-text notion-block-34b69776e2048009b386feb73ce54d35">如果抢占发生了，但系统“切换任务”的过程太慢，也会影响效率。这需要两个能力：</div><ul class="notion-list notion-list-disc notion-block-34b69776e20480d0824cfdd17e66fa4b"><li><b>快速中断响应：</b> 硬件能迅速察觉到紧急信号，并让 CPU 停下当前工作。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e204806bbb67c9045f00b6ca"><li><b>快速分派能力：</b> 操作系统内核要非常精简，把进程切换（Context Switch）的时间压缩到极致。就像F1赛车换轮胎，每一秒都关系到生死。</li></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-34b69776e2048051bcd0dea4a413f66a" data-id="34b69776e2048051bcd0dea4a413f66a"><span><div id="34b69776e2048051bcd0dea4a413f66a" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e2048051bcd0dea4a413f66a" title="3.4.2 实时调度算法的分类"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3.4.2 实时调度算法的分类</span></span></h3><ul class="notion-list notion-list-disc notion-block-34b69776e204801cb408f019d1bc4029"><li>按实时：</li><ul class="notion-list notion-list-disc notion-block-34b69776e204801cb408f019d1bc4029"><li>硬实时</li><li>软实时</li></ul></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480fb8176e4b3939b8fb4"><li>按调度方式：</li><ul class="notion-list notion-list-disc notion-block-34b69776e20480fb8176e4b3939b8fb4"><li>非抢占调度</li><li>抢占调度</li></ul></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e20480dea926f72f10d506a2" data-id="34b69776e20480dea926f72f10d506a2"><span><div id="34b69776e20480dea926f72f10d506a2" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480dea926f72f10d506a2" title="1. 非抢占式调度算法（比较“温和”，实时性较差）"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>1. 非抢占式调度算法（比较“温和”，实时性较差）</b></span></span></h4><div class="notion-text notion-block-34b69776e2048030b3f7db1fd03ae9dc">在这种模式下，就算紧急任务来了，也不能立刻把正在干活的任务踢走，必须等人家干完。</div><ul class="notion-list notion-list-disc notion-block-34b69776e20480e3afe0c596f15b7a43"><li><b>(a) 非抢占式轮转调度 (Non-preemptive Round Robin)：</b></li><ul class="notion-list notion-list-disc notion-block-34b69776e20480e3afe0c596f15b7a43"><li><b>逻辑：</b> 任务排成一圈，轮流用 CPU。</li><li><b>图解：</b> 实时进程请求调度后，得排在进程 1、进程 2... 进程 n 的后面。只有等前面的兄弟们都跑完了，才轮到实时进程。</li><li><b>缺点：</b> 响应时间非常长，不适合高实时要求的场景。</li></ul></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480d98c9df0f0bccb367e"><li><b>(b) 非抢占式优先权调度 (Non-preemptive Priority)：</b></li><ul class="notion-list notion-list-disc notion-block-34b69776e20480d98c9df0f0bccb367e"><li><b>逻辑：</b> 实时进程优先级高，可以“插队”到队列最前面。</li><li><b>图解：</b> 实时进程来了，发现 CPU 正在跑“当前进程”。它不能中断人家，只能在旁边等着。等“当前进程”<b>运行完成</b>的一瞬间，它立刻顶上去。</li><li><b>缺点：</b> 如果“当前进程”是一个需要跑很久的大任务，实时进程还是会等很久。</li></ul></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e20480f2a6b0dd3a352b5f9c" data-id="34b69776e20480f2a6b0dd3a352b5f9c"><span><div id="34b69776e20480f2a6b0dd3a352b5f9c" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480f2a6b0dd3a352b5f9c" title="2. 抢占式调度算法（比较“霸道”，实时性强）"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>2. 抢占式调度算法（比较“霸道”，实时性强）</b></span></span></h4><div class="notion-text notion-block-34b69776e204804cb21ff93495a5bb4d">这种模式下，紧急任务可以直接打断当前正在运行的任务。</div><ul class="notion-list notion-list-disc notion-block-34b69776e20480a5ab3bfb73e2fb7ba8"><li><b>(c) 基于时钟中断抢占的优先权调度：</b></li><ul class="notion-list notion-list-disc notion-block-34b69776e20480a5ab3bfb73e2fb7ba8"><li><b>逻辑：</b> 不是立刻抢，而是等“闹钟”响。</li><li><b>图解：</b> 实时进程请求调度，系统收到了，但没动作。直到<b>下一个时钟中断（Clock Interrupt）</b>到来时，调度程序才醒过来，发现有贵客，于是把当前进程换下来，让实时进程上。</li><li><b>评价：</b> 延迟取决于时钟频率（通常是几个毫秒），比非抢占式好很多。</li></ul></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480bcb8a3d0f0965ae7ca"><li><b>(d) 立即抢占 (Immediate Preemption) 的优先权调度：</b></li><ul class="notion-list notion-list-disc notion-block-34b69776e20480bcb8a3d0f0965ae7ca"><li><b>逻辑：</b> 真正的“秒回”，一刻都不等。</li><li><b>图解：</b> 只要实时进程发出请求，系统<b>立即</b>产生中断，强行停止当前进程，并在极短的调度开销后立刻运行实时进程。</li><li><b>评价：</b> 这是<b>硬实时系统</b>（如导弹控制、自动驾驶）必须采用的机制。它的响应时间（调度时间）被压缩到了微秒级。</li></ul></ul><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-34b69776e204809598fbc5aef7f65252"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:556.9921875px;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A6d640c6c-3d94-45e4-b667-b42e5701a20b%3Aimage.png?table=block&amp;id=34b69776-e204-8095-98fb-c5aef7f65252&amp;t=34b69776-e204-8095-98fb-c5aef7f65252" alt="notion image" loading="lazy" decoding="async"/></div></figure><table class="notion-simple-table notion-block-34b69776e2048053b423e277bff497d3"><tbody><tr class="notion-simple-table-row notion-simple-table-header-row notion-block-34b69776e20480519e13c1c173bd75af"><td class="" style="width:164.5px"><div class="notion-simple-table-cell">调度方式</div></td><td class="" style="width:164.5px"><div class="notion-simple-table-cell">什么时候切换？</div></td><td class="" style="width:88.5px"><div class="notion-simple-table-cell">响应延迟</div></td><td class="" style="width:233.5px"><div class="notion-simple-table-cell">适用场景</div></td></tr><tr class="notion-simple-table-row notion-block-34b69776e204800a8872db296b87d79a"><td class="" style="width:164.5px"><div class="notion-simple-table-cell"><b>(a) 轮转</b></div></td><td class="" style="width:164.5px"><div class="notion-simple-table-cell">轮到你了才换</div></td><td class="" style="width:88.5px"><div class="notion-simple-table-cell">极高</div></td><td class="" style="width:233.5px"><div class="notion-simple-table-cell">简单、非实时</div></td></tr><tr class="notion-simple-table-row notion-block-34b69776e204809bb034fad2d6293c1e"><td class="" style="width:164.5px"><div class="notion-simple-table-cell"><b>(b) 非抢占优先</b></div></td><td class="" style="width:164.5px"><div class="notion-simple-table-cell">当前进程<b>主动结束</b></div></td><td class="" style="width:88.5px"><div class="notion-simple-table-cell">较高</div></td><td class="" style="width:233.5px"><div class="notion-simple-table-cell">任务都很短的实时系统</div></td></tr><tr class="notion-simple-table-row notion-block-34b69776e20480b09433fd61e16ead8f"><td class="" style="width:164.5px"><div class="notion-simple-table-cell"><b>(c) 时钟中断抢占</b></div></td><td class="" style="width:164.5px"><div class="notion-simple-table-cell">等下一个<b>时钟滴答</b></div></td><td class="" style="width:88.5px"><div class="notion-simple-table-cell">较低</div></td><td class="" style="width:233.5px"><div class="notion-simple-table-cell">软实时系统（如视频播放）</div></td></tr><tr class="notion-simple-table-row notion-block-34b69776e204806c82beca706347e361"><td class="" style="width:164.5px"><div class="notion-simple-table-cell"><b>(d) 立即抢占</b></div></td><td class="" style="width:164.5px"><div class="notion-simple-table-cell"><b>立刻、马上</b></div></td><td class="" style="width:88.5px"><div class="notion-simple-table-cell">极低</div></td><td class="" style="width:233.5px"><div class="notion-simple-table-cell"><b>硬实时系统</b>（工业控制、航天）</div></td></tr></tbody></table><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-34b69776e204807c8f32df8f24766b79" data-id="34b69776e204807c8f32df8f24766b79"><span><div id="34b69776e204807c8f32df8f24766b79" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e204807c8f32df8f24766b79" title="3.4.3 最早截止时间有限算法——EDF(Earliest Deadline First)"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3.4.3 最早截止时间有限算法——EDF(Earliest Deadline First)</span></span></h3><div class="notion-text notion-block-34b69776e20480409d92d4d9cfcc2ab4">核心思想：<b>谁的任务最赶时间（截止时间最早），就让谁先做。</b></div><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e20480beb3adda25bf94c467" data-id="34b69776e20480beb3adda25bf94c467"><span><div id="34b69776e20480beb3adda25bf94c467" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480beb3adda25bf94c467" title="1. 非抢占式调度用于非周期任务"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">1. 非抢占式调度用于非周期任务</span></span></h4><div class="notion-text notion-block-34b69776e2048057b480ce3c4a9058c5">在<b>非抢占式</b>的情况下，即使新来的任务更紧急，也得等当前任务做完。</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-34b69776e20480e6947cf00332df543c"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:585.984375px;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A7ec459dc-89e5-4430-9e8a-ac29f09f1fc4%3Aimage.png?table=block&amp;id=34b69776-e204-80e6-947c-f00332df543c&amp;t=34b69776-e204-80e6-947c-f00332df543c" alt="notion image" loading="lazy" decoding="async"/></div></figure><ul class="notion-list notion-list-disc notion-block-34b69776e2048049a76ff780c9e5439d"><li><b>执行逻辑：</b></li><ul class="notion-list notion-list-disc notion-block-34b69776e2048049a76ff780c9e5439d"><ol start="1" class="notion-list notion-list-numbered notion-block-34b69776e204803bb264f779fb787ec1" style="list-style-type:decimal"><li><b>任务 1</b> 先到达并开始执行。</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-34b69776e204802092d3dc1d71e6f660" style="list-style-type:decimal"><li>在任务 1 执行期间，<b>任务 2</b> 和 <b>任务 3</b> 陆续到达。</li></ol><ol start="3" class="notion-list notion-list-numbered notion-block-34b69776e20480b4843ec5fc5c0464e6" style="list-style-type:decimal"><li>调度程序查看它们的截止时间：发现 <b>任务 3 的截止时间</b> 比 <b>任务 2</b> 早。</li></ol><ol start="4" class="notion-list notion-list-numbered notion-block-34b69776e20480dab7facb9dec150c55" style="list-style-type:decimal"><li>等任务 1 执行完，系统不按到达顺序，而是<b>优先安排任务 3</b>，最后才运行任务 2。</li></ol></ul></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480319cc5dcdd318e2c3a"><li><b>适用场景：</b> 任务执行时间短，或者对实时性要求不是极其严苛的场景。</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e2048029975cc32a19e94e47" data-id="34b69776e2048029975cc32a19e94e47"><span><div id="34b69776e2048029975cc32a19e94e47" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e2048029975cc32a19e94e47" title="2. 抢占式调度用于周期性任务"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">2. 抢占式调度用于周期性任务</span></span></h4><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-34b69776e20480bfad45e3a3c1b23ebd"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:341px"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A5a606e9f-0a18-43fe-9514-34de30d6fb38%3Aimage.png?table=block&amp;id=34b69776-e204-80bf-ad45-e3a3c1b23ebd&amp;t=34b69776-e204-80bf-ad45-e3a3c1b23ebd" alt="notion image" loading="lazy" decoding="async"/></div></figure><h5 class="notion-h notion-h4 notion-h-indent-3 notion-block-34b69776e20480a98fadf850f5dadcbe" data-id="34b69776e20480a98fadf850f5dadcbe"><span><div id="34b69776e20480a98fadf850f5dadcbe" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480a98fadf850f5dadcbe" title="案例背景："><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>案例背景：</b></span></span></h5><ul class="notion-list notion-list-disc notion-block-34b69776e20480a08b0eea515b8df731"><li><b>任务 A</b>：每 20ms 来一次，每次做 10ms（<!-- -->）。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480e19724d08c123ef82b"><li><b>任务 B</b>：每 50ms 来一次，每次做 25ms（<!-- -->）。</li></ul><h5 class="notion-h notion-h4 notion-h-indent-3 notion-block-34b69776e20480ceabc2fcd4dc585c92" data-id="34b69776e20480ceabc2fcd4dc585c92"><span><div id="34b69776e20480ceabc2fcd4dc585c92" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480ceabc2fcd4dc585c92" title="EDF 运行轨迹："><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>EDF 运行轨迹：</b></span></span></h5><ol start="1" class="notion-list notion-list-numbered notion-block-34b69776e2048008a84ee1a371bee2ba" style="list-style-type:decimal"><li><b>t = 0ms</b>：A1 和 B1 同时到达。A1 的截止时间是 20，B1 是 50。<b>A1 截止时间早，先跑 A1</b>。</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-34b69776e2048096a84be7ed6e37b174" style="list-style-type:decimal"><li><b>t = 10ms</b>：A1 跑完了，开始跑 B1。</li></ol><ol start="3" class="notion-list notion-list-numbered notion-block-34b69776e20480fd857cfc5b37876402" style="list-style-type:decimal"><li><b>t = 20ms</b>：<b>关键点！</b> A2 到达了。</li><ol class="notion-list notion-list-numbered notion-block-34b69776e20480fd857cfc5b37876402" style="list-style-type:lower-alpha"><ul class="notion-list notion-list-disc notion-block-34b69776e20480daa557e45cbdac965a"><li>此时 B1 还没跑完（还剩 15ms），B1 的截止时间是 50。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e2048073a338cbdb4851ec56"><li>A2 的截止时间是 40。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480c794d5d021136ae9e7"><li>因为 <b>40 &lt; 50</b>，所以 A2 抢占了 B1。<b>B1 被踢下去，A2 开始跑。</b></li></ul></ol></ol><ol start="4" class="notion-list notion-list-numbered notion-block-34b69776e204805aa02cd8cfaaafa6ec" style="list-style-type:decimal"><li><b>t = 30ms</b>：A2 跑完了，B1 继续跑剩下的 15ms。</li></ol><ol start="5" class="notion-list notion-list-numbered notion-block-34b69776e204805d9588d826e1283465" style="list-style-type:decimal"><li><b>t = 40ms</b>：A3 到达。</li><ol class="notion-list notion-list-numbered notion-block-34b69776e204805d9588d826e1283465" style="list-style-type:lower-alpha"><ul class="notion-list notion-list-disc notion-block-34b69776e20480cbaafddeb5deb61a72"><li>A3 的截止时间是 60。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e2048046bc9dc0243155c7b3"><li>此时 B1 还没跑完（还剩 5ms），B1 的截止时间是 50。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e2048083bcf7fc17716b51d5"><li>因为 <b>50 &lt; 60</b>（B1 更急），所以 <b>B1 继续跑，A3 必须等着。</b></li></ul></ol></ol><ol start="6" class="notion-list notion-list-numbered notion-block-34b69776e2048055aceec81b00506812" style="list-style-type:decimal"><li><b>t = 45ms</b>：B1 总算跑完了，A3 开始跑。</li></ol><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e204807aa050fcea9e7f8508" data-id="34b69776e204807aa050fcea9e7f8508"><span><div id="34b69776e204807aa050fcea9e7f8508" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e204807aa050fcea9e7f8508" title="3. 为什么 EDF 更好？（对比结论）"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3. 为什么 EDF 更好？（对比结论）</span></span></h4><div class="notion-text notion-block-34b69776e204803f8d0af65b491c74c7">看图片最下方的对比：</div><ul class="notion-list notion-list-disc notion-block-34b69776e2048039bf7fd03b213da6fe"><li><b>固定优先级调度</b>：如果给 A 高优先级，B 可能会错过截止时间（错过 50ms 的节点）；如果给 B 高优先级，A 可能会错过截止时间。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e204809c9645c61b4e7f4bbc"><li><b>EDF（动态优先级）</b>：优先级不是写死的，而是随着截止时间的逼近而改变。这种灵活性使得它能比固定优先级算法更充分地利用 CPU 资源，保证更多的任务不“违约”。</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e20480ecae31ed1c6543786c" data-id="34b69776e20480ecae31ed1c6543786c"><span><div id="34b69776e20480ecae31ed1c6543786c" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480ecae31ed1c6543786c" title="EDF 的精髓："><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">EDF 的精髓：</span></span></h4><ul class="notion-list notion-list-disc notion-block-34b69776e2048046831efa7b3baa2950"><li><b>不看出身（到达时间）</b>。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e2048027b878d0a24eecc75d"><li><b>不看地位（固定优先级）</b>。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e2048002936eca4c560189b3"><li><b>只看截止日期（Deadline）</b>。</li></ul><div class="notion-text notion-block-34b69776e20480e9932ec82f4feb1137">在单处理机环境下，只要总的利用率 <!-- -->，EDF 算法就能保证所有任务都在截止时间前完成。</div><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-34b69776e20480d5b03ac31a46e5a258" data-id="34b69776e20480d5b03ac31a46e5a258"><span><div id="34b69776e20480d5b03ac31a46e5a258" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480d5b03ac31a46e5a258" title="3.4.4 最低松弛度优先算法——LLF（Least Laxity First）"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>3.4.4 最低松弛度优先算法——LLF（Least Laxity First）</b></span></span></h3><blockquote class="notion-quote notion-block-34b69776e2048033986bdf08851c5892"><div><em><b>松弛度 = 必须完成的时间 - 任务本身还需要运行的时间 - 当前时间</b></em></div></blockquote><ul class="notion-list notion-list-disc notion-block-34b69776e20480fe80a1c73b89799fc2"><li><b>松弛度 = 0</b>：意味着必须现在立刻开始跑，否则绝对跑不完。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480e39309f32decaa20fc"><li><b>松弛度很大</b>：意味着可以先睡会，晚点再写也没事。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480e2a46dee4cd89985b8"><li>系统会不断计算所有就绪任务的松弛度，并<b>优先执行松弛度最低的任务</b>。这通常是一种<b>抢占式</b>调度。</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e20480c798eccb9eaad5c8d2" data-id="34b69776e20480c798eccb9eaad5c8d2"><span><div id="34b69776e20480c798eccb9eaad5c8d2" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480c798eccb9eaad5c8d2" title="案例详解："><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">案例详解：</span></span></h4><div class="notion-text notion-block-34b69776e204809cbeb5d4fa987f33c1">还是回到上一个算法的场景。</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-34b69776e204800d8aa9f854c42f78ca"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:146px"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Ad0180a5f-2740-42d0-9722-1faac1bda2e9%3Aimage.png?table=block&amp;id=34b69776-e204-800d-8aa9-f854c42f78ca&amp;t=34b69776-e204-800d-8aa9-f854c42f78ca" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-34b69776e2048011a105d738adeb2e00"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:120px"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Ae84cd589-9117-47da-9031-53b3f7dc13f6%3Aimage.png?table=block&amp;id=34b69776-e204-8011-a105-d738adeb2e00&amp;t=34b69776-e204-8011-a105-d738adeb2e00" alt="notion image" loading="lazy" decoding="async"/></div></figure><ul class="notion-list notion-list-disc notion-block-34b69776e2048000b2bcd2ff71fd129a"><li><b>t = 0ms：</b></li><ul class="notion-list notion-list-disc notion-block-34b69776e2048000b2bcd2ff71fd129a"><li>A1 的松弛度 = 20 - 10 - 0 = <b>10</b></li><li>B1 的松弛度 = 50 - 25 - 0 = <b>25</b></li><li><b>结果</b>：A1 松弛度更低，<b>A1 开始运行</b>。</li></ul></ul><ul class="notion-list notion-list-disc notion-block-34b69776e204807ebc72d1f38c422924"><li><b>t = 10ms：</b></li><ul class="notion-list notion-list-disc notion-block-34b69776e204807ebc72d1f38c422924"><li>A1 运行结束。此时只有 B1 在排队。</li><li><b>结果</b>：<b>B1 开始运行</b>。</li></ul></ul><ul class="notion-list notion-list-disc notion-block-34b69776e204804bb26de99a8e91f678"><li><b>t = 20ms（关键点）：</b></li><ul class="notion-list notion-list-disc notion-block-34b69776e204804bb26de99a8e91f678"><li>任务 A2 到达。</li><li>A2 的松弛度 = 40 - 10 - 20 = <b>10</b></li><li>B1 已经跑了 10ms，还剩 15ms。B1 的松弛度 = 50 - 15 - 20 = <b>15</b></li><li>虽然 A2 更急，但在某些 LLF 实现中（为了减少切换开销），如果松弛度没到 0，可以先不抢占。<b>但严格来说，此时 A2 (10) &lt; B1 (15)，A2 应该抢占。</b></li><li><em>注：书中的图 3-9 采用了“等到松弛度为 0 再抢占”的策略。</em></li></ul></ul><ul class="notion-list notion-list-disc notion-block-34b69776e204808aa925e23e2ea9e89a"><li><b>t = 30ms：</b></li><ul class="notion-list notion-list-disc notion-block-34b69776e204808aa925e23e2ea9e89a"><li>A2 的松弛度 = 40 - 10 - 30 = <b>0</b>（火烧眉毛了！）</li><li>B1 此时还剩 5ms。B1 的松弛度 = 50 - 5 - 30 = <b>15</b></li><li><b>结果</b>：A2 的松弛度降到了 0，<b>必须立刻抢占 B1</b>。A2 开始跑，B1 停下。</li></ul></ul><ul class="notion-list notion-list-disc notion-block-34b69776e2048060bcdde6a139e017c1"><li><b>t = 40ms：</b></li><ul class="notion-list notion-list-disc notion-block-34b69776e2048060bcdde6a139e017c1"><li>A2 运行结束。B1 还有最后 5ms 没写完。</li><li>此时 A3 也到达了，A3 的松弛度 = 60 - 10 - 40 = <b>10</b>。</li><li>B1 此时的松弛度 = 50 - 5 - 40 = <b>5</b>。</li><li><b>结果</b>：B1 更急（5 &lt; 10），<b>B1 优先补完剩下的 5ms</b>。</li></ul></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-34b69776e204803f97eefe424536972a" data-id="34b69776e204803f97eefe424536972a"><span><div id="34b69776e204803f97eefe424536972a" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e204803f97eefe424536972a" title="3.4.5 优先级倒置"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3.4.5 优先级倒置</span></span></h3><blockquote class="notion-quote notion-block-34b69776e20480c99457fcad592576f0"><div>在优先级调度算法中，我们期望高优先级的任务能优先执行，但在某些情况下，<b>高优先级任务反而会被低优先级任务阻塞，且被中优先级任务“插队”抢占执行时间</b>。</div></blockquote><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-34b69776e204800ca31dfb46098725cc"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:246px"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A61afedb5-873d-43cb-bc9b-b755dc83bb18%3Aimage.png?table=block&amp;id=34b69776-e204-800c-a31d-fb46098725cc&amp;t=34b69776-e204-800c-a31d-fb46098725cc" alt="notion image" loading="lazy" decoding="async"/></div></figure><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e20480ae8268cd8461f479d8" data-id="34b69776e20480ae8268cd8461f479d8"><span><div id="34b69776e20480ae8268cd8461f479d8" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480ae8268cd8461f479d8" title="1. 问题的起因（低优先级占用了资源）"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">1. 问题的起因（低优先级占用了资源）</span></span></h4><ul class="notion-list notion-list-disc notion-block-34b69776e204805089c5f73ff24e4bac"><li><b>初始阶段</b>：低优先级的进程 <!-- --> 最先运行，它执行了 <code class="notion-inline-code">P(mutex)</code> 操作，进入了临界区 <!-- -->，并<b>拿到了互斥锁（资源）</b>。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e2048019bf11dd6ce02cc530"><li><b>时刻 a</b>：中优先级的进程 <!-- --> 到达。因为 <!-- --> 优先级高于 <!-- -->，它抢占了 CPU，<b> 被挂起（但手里还拿着锁）</b>。</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e20480b780caff9041b69ed3" data-id="34b69776e20480b780caff9041b69ed3"><span><div id="34b69776e20480b780caff9041b69ed3" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480b780caff9041b69ed3" title="2. 冲突的发生（高优先级被阻塞）"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">2. 冲突的发生（高优先级被阻塞）</span></span></h4><ul class="notion-list notion-list-disc notion-block-34b69776e2048029bf4ec38e79330567"><li><b>时刻 b</b>：高优先级的进程 <!-- --> 到达。它是级别最高的，立刻抢占了 <!-- --> 开始运行。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e2048047bf68ce8e63131e88"><li><b>时刻 c</b>：<!-- --> 运行到一半，也需要使用那个互斥资源（执行 <code class="notion-inline-code">P(mutex)</code>）。但由于锁在 <!-- --> 手里，<b> 被阻塞</b>，进入等待状态。</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e20480819283f40977502dbf" data-id="34b69776e20480819283f40977502dbf"><span><div id="34b69776e20480819283f40977502dbf" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480819283f40977502dbf" title="3. 核心问题点（中优先级的“插队”）"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3. 核心问题点（中优先级的“插队”）</span></span></h4><ul class="notion-list notion-list-disc notion-block-34b69776e20480d883b0d79658bf5a79"><li><b>c 到 d 之间</b>：按理说，<!-- --> 在等 <!-- --> 放锁，此时应该让 <!-- --> 赶紧跑完把锁还给 <!-- -->。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480f4a1d3e8628122aaee"><li><b>但是</b>，系统中还有一个就绪的 <!-- -->。因为 <!-- --> 的优先级比 <!-- --> 高，调度器会选择 <b> 优先运行</b>。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e204803a80fcc753c0769cf3"><li><b>结果</b>：<!-- --> 一直运行到结束（时刻 d），这段时间内，最高级别的 <!-- --> 只能眼睁睁看着 <!-- --> 在跑，自己却在等 <!-- -->。</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e204807299b0ec8020e3a583" data-id="34b69776e204807299b0ec8020e3a583"><span><div id="34b69776e204807299b0ec8020e3a583" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e204807299b0ec8020e3a583" title="4. 最终解决"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">4. 最终解决</span></span></h4><ul class="notion-list notion-list-disc notion-block-34b69776e204807aa0aceb09c0d72015"><li><b>时刻 d</b>：<!-- --> 运行完了，CPU 终于回到了 <!-- --> 手中。<!-- --> 继续执行临界区代码。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e2048057a9e7ed3721c63af6"><li><b>时刻 e</b>：<!-- --> 执行 <code class="notion-inline-code">V(mutex)</code> <b>释放了锁</b>。此时，阻塞的 <!-- --> 立即被唤醒，抢占 CPU 进入临界区 <!-- --> 执行。</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-34b69776e20480008d82f2f0abdf3d6f" data-id="34b69776e20480008d82f2f0abdf3d6f"><span><div id="34b69776e20480008d82f2f0abdf3d6f" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e20480008d82f2f0abdf3d6f" title="解决：优先级继承"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">解决：优先级继承</span></span></h4><div class="notion-text notion-block-34b69776e2048081971ccb7842cf041a">在之前的问题图中，高优先级的 <!-- -->被中优先级的 <!-- --> 无端阻塞。而在这一张图中，通过“临时提升优先级”的策略，成功缩短了 <!-- --> 的等待时间。</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-34b69776e20480afa162dd022cb08011"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:249px"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Aa7b19122-8492-4e47-9925-0d7a99f8fd86%3Aimage.png?table=block&amp;id=34b69776-e204-80af-a162-dd022cb08011&amp;t=34b69776-e204-80af-a162-dd022cb08011" alt="notion image" loading="lazy" decoding="async"/></div></figure><ul class="notion-list notion-list-disc notion-block-34b69776e20480b7b8dffb176daaa235"><li><b>时刻 c</b>：P1 请求互斥资源（<code class="notion-inline-code">P(mutex)</code>），但资源被 P3 占用，P1 进入阻塞状态。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e204801b9ea9da8954e353f8"><li><b>关键变化</b>：此时系统发现高优先级的 P1 在等 P3。为了防止 P2 插队，系统<b>将 P3 的优先级临时提升到与 P1 相同的水平</b>。这就是图中标注的“<b>继承</b>”。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e204805c8db5e89fa6c85574"><li><b>c 到 d 之间</b>：因为 P3 现在拥有了“伪高优先级”，它比 P2 级别更高。所以 <b>P3 能够抢回 CPU 继续执行</b>，而 P2 只能继续等待。</li></ul><ul class="notion-list notion-list-disc notion-block-34b69776e204800e8be3c128d132ddb4"><li><b>时刻 d</b>：P3 执行完临界区代码，调用 <code class="notion-inline-code">V(mutex)</code> <b>释放资源</b>。</li><ul class="notion-list notion-list-disc notion-block-34b69776e204800e8be3c128d132ddb4"><li><b>优先级复原</b>：一旦释放资源，P3 的优先级立刻跌回原本的低水平。</li><li><b>P1 夺回控制权</b>：资源空闲后，P1 立即被唤醒并抢占 CPU，进入 CS−1 执行。</li></ul></ul><ul class="notion-list notion-list-disc notion-block-34b69776e20480779700c96d620c9c01"><li><b>时刻 e</b>：P1 全部运行结束。此时系统中优先级最高的是 <b>P2</b>，于是 P2 开始恢复运行。</li></ul><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-34b69776e204805488a7cb965a3afc25" data-id="34b69776e204805488a7cb965a3afc25"><span><div id="34b69776e204805488a7cb965a3afc25" class="notion-header-anchor"></div><a class="notion-hash-link" href="#34b69776e204805488a7cb965a3afc25" title="习题"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">习题</span></span></h2><div class="notion-text notion-block-34b69776e204805aa862ce9c825c09ae">1、下列进程调度算法中，综合考虑进程等待时间和执行时间的是（ ）。</div><div class="notion-text notion-block-34b69776e20480d4a0c4db45a8f3d7ea">A. 时间片轮转  B. 短进程优先  C. 先来先服务  D. 高响应比优先</div><details class="notion-toggle notion-block-34b69776e20480fc96faebc60aa2d9a2"><summary>答案</summary><div><div class="notion-text notion-block-34b69776e2048081b6f0d21ebd72aa14">D</div><div class="notion-text notion-block-34b69776e2048013a49ad4889dbce2a0"><b>考点：</b> HRRN 的核心公式 <code class="notion-inline-code">响应比=(等待时间+服务时间)/服务时间</code></div><div class="notion-blank notion-block-34b69776e20480a78ce1f7997d6bdbd2"> </div></div></details><div class="notion-text notion-block-34b69776e20480adbdc2d33d31607794">2、【2017 408】下列有关基于时间片的进程调度的叙述中，错误的是（ ）。</div><div class="notion-text notion-block-34b69776e204806aa033c6a00bbee84a">A. 时间片越短，进程切换次数越多，系统开销越大</div><div class="notion-text notion-block-34b69776e20480a9a995e4fe70b1c55e">B. 当前进程的时间片用完后，该进程状态由执行态变为阻塞态</div><div class="notion-text notion-block-34b69776e20480e9a43aeceedcd0c3e0">C. 时钟中断发生后，系统会修改当前进程在时间片内的剩余时间</div><div class="notion-text notion-block-34b69776e20480f68315d26d4b45a0fd">D. 影响时间片大小的主要因素包括响应时间、系统开销和进程数量等</div><details class="notion-toggle notion-block-34b69776e2048068bc8dc29c07ea2bb7"><summary>答案</summary><div><div class="notion-text notion-block-34b69776e2048021846fdab37798a738">B</div><div class="notion-text notion-block-34b69776e20480dba04bf6d8df74e58b">解析：进程切换带来系统开销，切换次数越多，系统开销越大，即A选项正确；
当前进程的时间片用完后，该进程状态由执行态变为就绪态，即B选项错误；
时钟中断是系统特定的周期性时钟节拍。操作系统通过它来确定时间间隔，实现时间的延时和任务的超时，即C选项正确；
现代操作系统为了保证性能最优，通常根据响应时间、系统开销、进程数量、进程运行时间、进程切换开销等因素确定时间片大小，即D选项正确。
</div></div></details><div class="notion-text notion-block-34b69776e2048037b618ef2bbdac5232">3、假设4个作业到达系统的时刻和运行时间如下表所示。</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-34b69776e204808f879fc0ffbe5c3430"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A3f8bbbec-09e2-4f29-adf5-2b37d8673ac8%3Aimage.png?table=block&amp;id=34b69776-e204-808f-879f-c0ffbe5c3430&amp;t=34b69776-e204-808f-879f-c0ffbe5c3430" alt="notion image" loading="lazy" decoding="async"/></div></figure><div class="notion-text notion-block-34b69776e20480f088c6d9144dd071f4">系统在t=2时开始作业调度。若分别采用先来先服务和短作业优先调度算法 ，则选中的作业分别是
A．J2、J3
B．J1、J4
C．J2、J4
D．J1、J3</div><details class="notion-toggle notion-block-34b69776e20480cea21ef093cf682aaa"><summary>答案</summary><div><div class="notion-text notion-block-34b69776e2048091b180cc61e771bb8e">D</div><div class="notion-text notion-block-34b69776e20480799604c16a728b1940">解析：系统在 t=2时开始作业调度，若采用先来先服务调度算法，此时已有J1、J2和J3作业到达，作业来得越早优先级越高，则选中的作业是J1；若采用短作业优先调度算法，此时已有J1、J2和J3作业到达，但作业运行时间大小排序为J3&lt;J2=J1，作业运行时间越短优先级越高，则选中的作业是J3。
</div></div></details><div class="notion-text notion-block-34b69776e204803c9f52c07a7a957fa4">4、 高响应比优先 HRRN 分析题 </div><div class="notion-text notion-block-34b69776e204807b80d8e8e29b94bb84">有 4 个作业： </div><div class="notion-text notion-block-34b69776e2048055a9cdf988fd2edbbd">J1：到达 0，服务时间 3 </div><div class="notion-text notion-block-34b69776e2048071b31af82bacbb2539">J2：到达 2，服务时间 6 </div><div class="notion-text notion-block-34b69776e2048005bcc4d2c0e322bf03">J3：到达 4，服务时间 4 J4：到达 6，服务时间 2 系统采用 高响应比优先 HRRN。 要求： （1）在每次调度时，计算所有已到达作业的响应比； （2）给出作业选择顺序； （3）求平均周转时间； （4）说明 HRRN 为什么能兼顾短作业和长作业。</div><div class="notion-blank notion-block-34b69776e2048079ba11c190000aa07e"> </div><div class="notion-blank notion-block-34b69776e204802aa2b7e93930e554ce"> </div><div class="notion-blank notion-block-34b69776e20480448f65cc2163921b2e"> </div><div class="notion-blank notion-block-34b69776e20480fd8950e3ae3a4e6119"> </div><div class="notion-blank notion-block-34b69776e2048061b246c942f45b378b"> </div></div></main></div>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Summer Pocket 踩点小结]]></title>
            <link>http://richardliuda.top/article/summerpocket</link>
            <guid>http://richardliuda.top/article/summerpocket</guid>
            <pubDate>Sun, 22 Mar 2026 00:00:00 GMT</pubDate>
            <content:encoded><![CDATA[<div id="notion-article" class="mx-auto overflow-hidden "><main class="notion light-mode notion-page notion-block-32b69776e20480d2b9d5cadea067430b"><div class="notion-viewport"></div><div class="notion-collection-page-properties"></div><div class="notion-sync-block notion-block-32b69776e20480f68f61e82db7c868b3"><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-32b69776e20480bd9d8ee7c3f075a2c0" data-id="32b69776e20480bd9d8ee7c3f075a2c0"><span><div id="32b69776e20480bd9d8ee7c3f075a2c0" class="notion-header-anchor"></div><a class="notion-hash-link" href="#32b69776e20480bd9d8ee7c3f075a2c0" title="东涌"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">东涌</span></span></h2><ul class="notion-list notion-list-disc notion-block-32b69776e204802da260cde4337fc3ff"><li>吃得很少，很贵，拍摄尽量自己带吃的</li></ul><ul class="notion-list notion-list-disc notion-block-32b69776e204809c9268f2c2862527a9"><li>民宿旁边有个小公园，可以拍点树林景</li><ul class="notion-list notion-list-disc notion-block-32b69776e204809c9268f2c2862527a9"><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-32b69776e20480e8bb8df2ea86009da6"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Aefc8c88e-4225-49fe-96e4-4fbc600d4d85%3AIMG_20260322_104912.jpg?table=block&amp;id=32b69776-e204-80e8-bb8d-f2ea86009da6&amp;t=32b69776-e204-80e8-bb8d-f2ea86009da6" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-32b69776e204804f8d57f56d20e3b243"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Aaf69c752-8af7-4cf6-a463-5e070325f17c%3AIMG_20260322_104935.jpg?table=block&amp;id=32b69776-e204-804f-8d57-f56d20e3b243&amp;t=32b69776-e204-804f-8d57-f56d20e3b243" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-32b69776e20480aa8d89ce3b757ebf7b"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A92d39a30-325e-4984-821c-45edd4c508a7%3AIMG_20260322_104925.jpg?table=block&amp;id=32b69776-e204-80aa-8d89-ce3b757ebf7b&amp;t=32b69776-e204-80aa-8d89-ce3b757ebf7b" alt="notion image" loading="lazy" decoding="async"/></div></figure></ul></ul><ul class="notion-list notion-list-disc notion-block-32b69776e20480ecbdd4f6d87545c208"><li>民宿是个小区，楼底下有一些娱乐设施，滑滑梯之类的，貌似还能烧烟花（有看到仙女棒的残骸），晚上如果方便的话可以拍一些夜景</li><ul class="notion-list notion-list-disc notion-block-32b69776e20480ecbdd4f6d87545c208"><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-32b69776e2048074856bc646a87f4372"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A61821db5-22a3-4357-82f5-3d20c786280e%3AIMG_20260322_104330.jpg?table=block&amp;id=32b69776-e204-8074-856b-c646a87f4372&amp;t=32b69776-e204-8074-856b-c646a87f4372" alt="notion image" loading="lazy" decoding="async"/></div></figure></ul></ul><ul class="notion-list notion-list-disc notion-block-32b69776e20480fc8a24f2708dd21017"><li>海滩入口有洗手间，比较方便</li></ul><ul class="notion-list notion-list-disc notion-block-32b69776e204804f9290fbdeae97c2c3"><li>穿鼻岩那边有石滩，但是今天没开放，六月份不知道能不能开</li><ul class="notion-list notion-list-disc notion-block-32b69776e204804f9290fbdeae97c2c3"><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-32b69776e204808e8672fc62dd75bdda"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A27cae85f-3bb1-4d78-84dd-01c55324230e%3AIMG_20260322_111821.jpg?table=block&amp;id=32b69776-e204-808e-8672-fc62dd75bdda&amp;t=32b69776-e204-808e-8672-fc62dd75bdda" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-32b69776e20480178655d5b6ec948a02"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A4ba14234-1d8c-4cf7-9a66-3a5cf9f7c122%3AIMG_20260322_111832.jpg?table=block&amp;id=32b69776-e204-8017-8655-d5b6ec948a02&amp;t=32b69776-e204-8017-8655-d5b6ec948a02" alt="notion image" loading="lazy" decoding="async"/></div></figure></ul></ul><ul class="notion-list notion-list-disc notion-block-32b69776e204803b936ddf8e2578dcee"><li>民宿到海滩大概一公里，会穿过一个小村子，挺整洁的，感觉可以拍点粗点心屋的景</li><ul class="notion-list notion-list-disc notion-block-32b69776e204803b936ddf8e2578dcee"><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-32b69776e204802db3d6ea5c38216515"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A7f38d97f-2001-4d30-88e2-961e16a4b221%3AIMG_20260322_105937.jpg?table=block&amp;id=32b69776-e204-802d-b3d6-ea5c38216515&amp;t=32b69776-e204-802d-b3d6-ea5c38216515" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-32b69776e20480ba9408c85a7465b36e"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A38a8d866-9ccd-4cce-bb05-c58cb7168e55%3AIMG_20260322_105413.jpg?table=block&amp;id=32b69776-e204-80ba-9408-c85a7465b36e&amp;t=32b69776-e204-80ba-9408-c85a7465b36e" alt="notion image" loading="lazy" decoding="async"/></div></figure><li>这边的小路也挺还原</li><ul class="notion-list notion-list-disc notion-block-32b69776e204807ab16ff2651ee0534e"><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-32b69776e204808e9739ff7af39b037a"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A87417690-8882-4f41-b9d1-25ed89cc3253%3AIMG_20260322_110825.jpg?table=block&amp;id=32b69776-e204-808e-9739-ff7af39b037a&amp;t=32b69776-e204-808e-9739-ff7af39b037a" alt="notion image" loading="lazy" decoding="async"/></div></figure></ul><div class="notion-blank notion-block-32b69776e204806bb512d7d6bca4e187"> </div></ul></ul><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-32b69776e20480e680e8f7322e31946b" data-id="32b69776e20480e680e8f7322e31946b"><span><div id="32b69776e20480e680e8f7322e31946b" class="notion-header-anchor"></div><a class="notion-hash-link" href="#32b69776e20480e680e8f7322e31946b" title="红树林公园"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">红树林公园</span></span></h2><ul class="notion-list notion-list-disc notion-block-32b69776e2048011b4efed9a877f578f"><li>不是很值得单独去，能用的景少，树矮，稀疏</li></ul><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-32b69776e20480dbbae4c6a0d286897e"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Ad1549889-9a0c-4347-8087-7ef45c8eb503%3AIMG_20260322_125818.jpg?table=block&amp;id=32b69776-e204-80db-bae4-c6a0d286897e&amp;t=32b69776-e204-80db-bae4-c6a0d286897e" alt="notion image" loading="lazy" decoding="async"/></div></figure><ul class="notion-list notion-list-disc notion-block-32b69776e20480819254e1d871abf68a"><li>树少</li></ul><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-32b69776e20480d0b56dd1a638df66eb"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A97428118-5922-4e68-bc6d-296971324bf4%3AIMG_20260322_125826.jpg?table=block&amp;id=32b69776-e204-80d0-b56d-d1a638df66eb&amp;t=32b69776-e204-80d0-b56d-d1a638df66eb" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-32b69776e2048043a286d4069f37f023"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Aa06267e0-3418-4537-8ba8-845404643376%3AIMG_20260322_125711.jpg?table=block&amp;id=32b69776-e204-8043-a286-d4069f37f023&amp;t=32b69776-e204-8043-a286-d4069f37f023" alt="notion image" loading="lazy" decoding="async"/></div></figure><ul class="notion-list notion-list-disc notion-block-32b69776e2048086a8b0ee70acf13af9"><li>这里可以拍点有纵深的合照</li></ul><div class="notion-callout notion-gray_background_co notion-block-32b69776e20480e9ab3ef8cbc432be1b"><div class="notion-page-icon-inline notion-page-icon-span"><span class="notion-page-icon" role="img" aria-label="💡">💡</span></div><div class="notion-callout-text"><div class="notion-text notion-block-32b69776e20480e095a7d61a39633b85">可以选择第一天先去华侨城拍点树林景，拍完之后再去东涌</div><div class="notion-text notion-block-32b69776e20480d5a771e80b73042728">或者就在民宿旁边的小树林拍一拍</div></div></div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-32b69776e20480299b7ddac10673821d" data-id="32b69776e20480299b7ddac10673821d"><span><div id="32b69776e20480299b7ddac10673821d" class="notion-header-anchor"></div><a class="notion-hash-link" href="#32b69776e20480299b7ddac10673821d" title="备选"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">备选</span></span></h2><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-32b69776e2048077baffcac12e06bbea" data-id="32b69776e2048077baffcac12e06bbea"><span><div id="32b69776e2048077baffcac12e06bbea" class="notion-header-anchor"></div><a class="notion-hash-link" href="#32b69776e2048077baffcac12e06bbea" title="桔钓沙&amp;杨梅坑"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">桔钓沙&amp;杨梅坑</span></span></h3><div class="notion-text notion-block-32b69776e20480bf8628c4c692ebe24d">优点</div><ul class="notion-list notion-list-disc notion-block-32b69776e204808f924aca4adbb19c37"><li>交通较便利，比东涌近一点，少走很多山路</li></ul><ul class="notion-list notion-list-disc notion-block-32b69776e20480b598c4c7fd71986bfc"><li>沙滩大，沙质比东涌略好，石滩近邻沙滩，方便</li></ul><ul class="notion-list notion-list-disc notion-block-32b69776e20480668b69e0c9d745f172"><li>有堤坝景，不过坝上应该不开放</li></ul><ul class="notion-list notion-list-disc notion-block-32b69776e20480f49877c6478f95a390"><li>吃的东西多一点，能叫到葵涌的外卖</li></ul><ul class="notion-list notion-list-disc notion-block-32b69776e204808f808bcee1b628481f"><li>树林多</li></ul><div class="notion-text notion-block-32b69776e2048081a5c9cadc2732998b">缺点</div><ul class="notion-list notion-list-disc notion-block-32b69776e2048032bcbee88bbba604e0"><li>人多，车多，节假日车位紧张</li></ul><ul class="notion-list notion-list-disc notion-block-32b69776e20480baa51dea7c8c82cf81"><li>海滩旁没有比较还原的村道，比较景区化</li><ul class="notion-list notion-list-disc notion-block-32b69776e20480baa51dea7c8c82cf81"><li>最近的要走两公里</li></ul></ul><ul class="notion-list notion-list-disc notion-block-32b69776e20480769bcec8c605ba3346"><li>民宿会贵</li></ul><ul class="notion-list notion-list-disc notion-block-32b69776e204803b8af0e5083554a005"><li>有树林景，但是小路少，都是马路或者路旁的栈道，且要走一段距离</li></ul><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-33e69776e204806b8f7cd272a1f6c0f9"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A7d7fc9bf-dce9-46ef-abe5-0e6ef021a67f%3A1d36d6c9d5898dc58ce8ac97db49aefb.jpg?table=block&amp;id=33e69776-e204-806b-8f7c-d272a1f6c0f9&amp;t=33e69776-e204-806b-8f7c-d272a1f6c0f9" alt="notion image" loading="lazy" decoding="async"/></div></figure><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-32b69776e20480e3a6c0d0efad9fc1dc" data-id="32b69776e20480e3a6c0d0efad9fc1dc"><span><div id="32b69776e20480e3a6c0d0efad9fc1dc" class="notion-header-anchor"></div><a class="notion-hash-link" href="#32b69776e20480e3a6c0d0efad9fc1dc" title="华侨城（树林景）"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">华侨城（树林景）</span></span></h3><ul class="notion-list notion-list-disc notion-block-32b69776e204809aaca4e75e6c303cb4"><li>如果老师方便（鸥/嗨梨等）第一天可以去东涌前白天先去华侨城拍一点树林景</li></ul><ul class="notion-list notion-list-disc notion-block-32b69776e204803d951be802951d93ce"><li>拍完之后和摄影老师一起开车前往东涌民宿</li></ul><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-32b69776e2048087a15ec528ce9f1ad3"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A76b2b8f5-3904-4b5f-a717-20b8d78e4b37%3Afaff7ee8370c9f388417efe539b7543f.jpg?table=block&amp;id=32b69776-e204-8087-a15e-c528ce9f1ad3&amp;t=32b69776-e204-8087-a15e-c528ce9f1ad3" alt="notion image" loading="lazy" decoding="async"/></div></figure><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-32b69776e20480a4a038d72428a3d2ad" data-id="32b69776e20480a4a038d72428a3d2ad"><span><div id="32b69776e20480a4a038d72428a3d2ad" class="notion-header-anchor"></div><a class="notion-hash-link" href="#32b69776e20480a4a038d72428a3d2ad" title="需要注意的地方"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">需要注意的地方</span></span></h2><ul class="notion-list notion-list-disc notion-block-32b69776e204809fa9b8f175b3d18b09"><li>节假日有可能会堵车，如果第二天需要返回其他城市的，需要预留一下时间</li></ul><ul class="notion-list notion-list-disc notion-block-32b69776e20480a08a80e8a8e9257826"><li>回广州/东莞可以选择去罗湖坐高铁，末班车十点半左右</li></ul><ul class="notion-list notion-list-disc notion-block-32b69776e20480ec9f37eea2bd09a270"><li>东涌开车过去最乐观估计需要一个半小时，堵车的话三四个小时（节假日包堵的）</li></ul><ul class="notion-list notion-list-disc notion-block-32b69776e2048008ae6ed786ccd5a57f"><li>大家最好带上坐垫，因为很多需要坐地上的场景，那边没有专门露营的设计，所以地上不会很干净</li></ul><ul class="notion-list notion-list-disc notion-block-32b69776e2048059ac21e14a167672fb"><li>可能涉及到海滩踩水的场景，看看各位老师是否接受吧，如果要拍的话也需要做下准备</li></ul><ul class="notion-list notion-list-disc notion-block-32b69776e20480e4b62fcf9bbb4c1fb7"><li>尽量多带点吃的，东涌本地吃的少，贵，且外卖送不进来</li></ul><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-33e69776e20480ad8108e86ba3526bed" data-id="33e69776e20480ad8108e86ba3526bed"><span><div id="33e69776e20480ad8108e86ba3526bed" class="notion-header-anchor"></div><a class="notion-hash-link" href="#33e69776e20480ad8108e86ba3526bed" title="地图"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">地图</span></span></h2><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-33e69776e2048084859ff86df671c473"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A2546d48f-74e8-42f8-a407-51b7d0cf92a0%3Aimage.png?table=block&amp;id=33e69776-e204-8084-859f-f86df671c473&amp;t=33e69776-e204-8084-859f-f86df671c473" alt="notion image" loading="lazy" decoding="async"/></div></figure><div class="notion-text notion-block-33e69776e204803882fec2bc64d2145b">地图如上↑</div><div class="notion-text notion-block-33e69776e20480fc901dd512e18e9da9">红色箭头为路线，请大家存好以防迷路(=^▽^=)</div><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-33e69776e2048081bb8bf01b98076fa5" data-id="33e69776e2048081bb8bf01b98076fa5"><span><div id="33e69776e2048081bb8bf01b98076fa5" class="notion-header-anchor"></div><a class="notion-hash-link" href="#33e69776e2048081bb8bf01b98076fa5" title="to coser"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>to coser</b></span></span></h3><div class="notion-text notion-block-33e69776e20480a3b3afda86356f996a">coser老师们请<span class="notion-inline-underscore">熟记好时间，姿势(注意右上角有标注的地方为批注)，地点！</span></div><div class="notion-text notion-block-33e69776e2048065b54bf75017124f5f">10:45为上午小组照人员出发时间（即诺儿 绀雾erika明宴 柊 海琴烟）</div><div class="notion-text notion-block-33e69776e20480b5a370ece5e26cc90b">其余所有人员可以稍晚，但需于11:15到达图中标星号位置，程约800米自己掐好走路时间</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-33e69776e20480e686d3fb9a81e3e74f"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:397.9921875px;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A48bee311-5c70-4ec8-92ff-7d76ae50c449%3Aimage.png?table=block&amp;id=33e69776-e204-80e6-86d3-fb9a81e3e74f&amp;t=33e69776-e204-80e6-86d3-fb9a81e3e74f" alt="notion image" loading="lazy" decoding="async"/></div></figure><ul class="notion-list notion-list-disc notion-block-33e69776e2048073913fd74e8b71af4b"><li>请自备好午餐 中午午休原地休息</li></ul><ul class="notion-list notion-list-disc notion-block-33e69776e20480db9674dc687bed2f7a"><li>带好补妆用品(坐高铁有些会被收要注意) 防晒用品</li></ul><ul class="notion-list notion-list-disc notion-block-33e69776e20480c5b13af906f5ac624c"><li>没有安排的时候可以自行寻找搭子拍小视频，集邮 也可以就地休息</li></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-33e69776e204804ca9e8eb0fe103dd24" data-id="33e69776e204804ca9e8eb0fe103dd24"><span><div id="33e69776e204804ca9e8eb0fe103dd24" class="notion-header-anchor"></div><a class="notion-hash-link" href="#33e69776e204804ca9e8eb0fe103dd24" title="to 摄影"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>to 摄影</b></span></span></h3><div class="notion-text notion-block-33e69776e2048036a2ecc77d0ac78080"><span class="notion-inline-underscore">请摄影老师们熟记拍摄时间，拍摄对象</span></div><div class="notion-text notion-block-33e69776e2048067a451cf2218a56bc7">10:45全员出发</div><ul class="notion-list notion-list-disc notion-block-33e69776e2048084be93e3b54372d1fd"><li>请自备好午餐 中午午休原地休息</li></ul><ul class="notion-list notion-list-disc notion-block-33e69776e20480998a78eea8db8c1456"><li>带好防晒用品</li></ul><ul class="notion-list notion-list-disc notion-block-33e69776e20480b3a3dcf7373b272634"><li>没有安排的时候可以帮忙其他老师一起拍照，集邮 也可以就地休息</li></ul><div class="notion-blank notion-block-32b69776e2048080987bdddec1832ea7"> </div><div class="notion-blank notion-block-32b69776e2048053ba54f702b486229a"> </div></div><div class="notion-blank notion-block-32b69776e20480d8a5b1d0cb78d389a5"> </div></main></div>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[数据库系统笔记]]></title>
            <link>http://richardliuda.top/article/Database</link>
            <guid>http://richardliuda.top/article/Database</guid>
            <pubDate>Mon, 12 Jan 2026 00:00:00 GMT</pubDate>
            <content:encoded><![CDATA[<div id="notion-article" class="mx-auto overflow-hidden "><main class="notion light-mode notion-page notion-block-2e669776e2048003990dd5379418e5e6"><div class="notion-viewport"></div><div class="notion-collection-page-properties"></div><div class="notion-sync-block notion-block-2e669776e204802d993ef61ec0aae132"><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-2e469776e204804d8a47d0b9e2912e40" data-id="2e469776e204804d8a47d0b9e2912e40"><span><div id="2e469776e204804d8a47d0b9e2912e40" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e204804d8a47d0b9e2912e40" title="一、绪论"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">一、绪论</span></span></h2><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e469776e2048021a88ef7e50e376e6e" data-id="2e469776e2048021a88ef7e50e376e6e"><span><div id="2e469776e2048021a88ef7e50e376e6e" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e2048021a88ef7e50e376e6e" title="文件系统的问题"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">文件系统的问题</span></span></h3><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e20480658488ff5650ea22cc" data-id="2e469776e20480658488ff5650ea22cc"><span><div id="2e469776e20480658488ff5650ea22cc" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480658488ff5650ea22cc" title="ACID"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">ACID</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e469776e2048015a80dc5a854d4591b"><li>原子性(atomicity)</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480009dbfc28cfba615ad"><li> 一致性(consistency)</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480578019ca513d064859"><li>隔离性(isolation)</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480faa9a7f4dca7ab9791"><li>持久性(durability)</li></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e469776e20480dcbc02c58b51392611" data-id="2e469776e20480dcbc02c58b51392611"><span><div id="2e469776e20480dcbc02c58b51392611" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480dcbc02c58b51392611" title="不同类型的DB"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">不同类型的DB</span></span></h3><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e20480f8b1afd62e41806ca9" data-id="2e469776e20480f8b1afd62e41806ca9"><span><div id="2e469776e20480f8b1afd62e41806ca9" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480f8b1afd62e41806ca9" title="分层数据库系统"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">分层数据库系统</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e469776e20480ff8d20fe73882d97fb"><li>树状结构，用指针连接，存在一对多</li></ul><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e20480f78d57f9e3c405f2e0"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A789a61d0-aaf3-43ab-b6ab-2b27d5684588%3Aimage.png?table=block&amp;id=2e469776-e204-80f7-8d57-f9e3c405f2e0&amp;t=2e469776-e204-80f7-8d57-f9e3c405f2e0" alt="notion image" loading="lazy" decoding="async"/></div></figure><ul class="notion-list notion-list-disc notion-block-2e469776e20480188bfdcc694b22d255"><li>缺点</li><ul class="notion-list notion-list-disc notion-block-2e469776e20480188bfdcc694b22d255"><li>查询复杂（递归）</li><li>难保证数据一致性</li><li>难扩展</li><li>缺乏标准</li><li>权限不好分配</li></ul></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e2048087bac8c66f78af5d0c" data-id="2e469776e2048087bac8c66f78af5d0c"><span><div id="2e469776e2048087bac8c66f78af5d0c" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e2048087bac8c66f78af5d0c" title="网状数据库系统"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">网状数据库系统</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e469776e20480c3818df90f51bce179"><li>无环图</li><ul class="notion-list notion-list-disc notion-block-2e469776e20480c3818df90f51bce179"><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e204800c9e71d98ee2f50572"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A3e6b6a88-4e8e-4c28-a90f-201b5110cbf3%3Aimage.png?table=block&amp;id=2e469776-e204-800c-9e71-d98ee2f50572&amp;t=2e469776-e204-800c-9e71-d98ee2f50572" alt="notion image" loading="lazy" decoding="async"/></div></figure></ul></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480229671fcb9f6558c66"><li>缺点</li><ul class="notion-list notion-list-disc notion-block-2e469776e20480229671fcb9f6558c66"><li>复杂</li><li>难维护</li></ul></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e20480369227e823cca74d91" data-id="2e469776e20480369227e823cca74d91"><span><div id="2e469776e20480369227e823cca74d91" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480369227e823cca74d91" title="关系型数据库系统"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">关系型数据库系统</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e469776e20480ee87d4ffab28e7e140"><li>组织为表</li><ul class="notion-list notion-list-disc notion-block-2e469776e20480ee87d4ffab28e7e140"><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e204803a9d9fcba2b7933420"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:681.9791870117188px;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A0a05605b-36da-4c15-8b71-540f4899d6ce%3Aimage.png?table=block&amp;id=2e469776-e204-803a-9d9f-cba2b7933420&amp;t=2e469776-e204-803a-9d9f-cba2b7933420" alt="notion image" loading="lazy" decoding="async"/></div></figure></ul></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e469776e20480488942e3ce15428c25" data-id="2e469776e20480488942e3ce15428c25"><span><div id="2e469776e20480488942e3ce15428c25" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480488942e3ce15428c25" title="数据库系统DBS"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">数据库系统DBS</span></span></h3><ul class="notion-list notion-list-disc notion-block-2e469776e2048039b24df7341cd67b02"><li>组成</li><ul class="notion-list notion-list-disc notion-block-2e469776e2048039b24df7341cd67b02"><li>Database</li><li>Data model</li><li>DBMS</li></ul></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e20480219864de19162a39b1" data-id="2e469776e20480219864de19162a39b1"><span><div id="2e469776e20480219864de19162a39b1" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480219864de19162a39b1" title="DB语言"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">DB语言</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e469776e204809b9266f8562c596cb7"><li>定义db格式</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e204807d9854dc03642c8943"><li>增删改查</li><ul class="notion-list notion-list-disc notion-block-2e469776e204807d9854dc03642c8943"><li>分类</li><ul class="notion-list notion-list-disc notion-block-2e469776e204806cac7cd93ac78ad9d7"><li>过程化DML</li><ul class="notion-list notion-list-disc notion-block-2e469776e20480da91a4ce3099ed1a7e"><li>用户需同事知道要什么和怎么要数据</li></ul><li>非过程化DML</li><ul class="notion-list notion-list-disc notion-block-2e469776e20480fd8954efe033c64ccc"><li>用户只需要知道要什么数据</li><li>SQL</li></ul></ul></ul></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e469776e2048088a345c4e011096e71" data-id="2e469776e2048088a345c4e011096e71"><span><div id="2e469776e2048088a345c4e011096e71" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e2048088a345c4e011096e71" title="数据库模式DB Schema"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">数据库模式DB Schema</span></span></h3><ul class="notion-list notion-list-disc notion-block-2e469776e2048003ba2ad7d6f963f059"><li>数据库的逻辑结构</li><ul class="notion-list notion-list-disc notion-block-2e469776e2048003ba2ad7d6f963f059"><li>概念模式</li><ul class="notion-list notion-list-disc notion-block-2e469776e2048041b473d50796d4565e"><li>定义db所有数据</li></ul><li>物理模式</li><ul class="notion-list notion-list-disc notion-block-2e469776e204801fa3f4f01e41b0bc73"><li>描述如何存储数据</li></ul><li>用户视图</li><ul class="notion-list notion-list-disc notion-block-2e469776e20480ac99e1fef01e206f6b"><li>只描述用户可访问的数据</li></ul></ul></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480fcb487c056a8b2738b"><li>三层模式结构</li><ul class="notion-list notion-list-disc notion-block-2e469776e20480fcb487c056a8b2738b"><li>外部层</li><li>概念层</li><li>内部层</li><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e20480f7be2fc49bff88026c"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Ad0e2794a-e616-49ee-ae33-4c4bf1558b97%3Aimage.png?table=block&amp;id=2e469776-e204-80f7-be2f-c49bff88026c&amp;t=2e469776-e204-80f7-be2f-c49bff88026c" alt="notion image" loading="lazy" decoding="async"/></div></figure></ul></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480d2b9b9f9118c467725"><li>提供访问控制机制</li></ul><div class="notion-blank notion-block-2e469776e20480d0950ad80f9198709a"> </div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-2e469776e2048003a350dbdd0ae6534a" data-id="2e469776e2048003a350dbdd0ae6534a"><span><div id="2e469776e2048003a350dbdd0ae6534a" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e2048003a350dbdd0ae6534a" title="二、关系型数据库管理系统RDBMS"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">二、关系型数据库管理系统RDBMS</span></span></h2><ul class="notion-list notion-list-disc notion-block-2e469776e204807fbe76c5bfcb5dd602"><li>表就是关系</li></ul><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e20480b5bd9ff90669512738"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A2f9a36f1-832c-4879-b70d-52ec33b1e43d%3Aimage.png?table=block&amp;id=2e469776-e204-80b5-bd9f-f90669512738&amp;t=2e469776-e204-80b5-bd9f-f90669512738" alt="notion image" loading="lazy" decoding="async"/></div></figure><ul class="notion-list notion-list-disc notion-block-2e469776e204809e8b8fe5eaf8fe1f7d"><li>属性：列</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480af879cf6cef24d9a46"><li>元组，记录：行</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480ef9e8ce3fbdc63280a"><li>表头：属性名</li></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e469776e204802791bedce5a8ae6657" data-id="2e469776e204802791bedce5a8ae6657"><span><div id="2e469776e204802791bedce5a8ae6657" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e204802791bedce5a8ae6657" title="模式Schema"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">模式Schema</span></span></h3><ul class="notion-list notion-list-disc notion-block-2e469776e2048050a8d9ed7b4feb4d5a"><li>定义：属性的集合，就是表的结构</li><ul class="notion-list notion-list-disc notion-block-2e469776e2048050a8d9ed7b4feb4d5a"><li>一旦确定，改动的代价很大</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480dbaa63fdf6e0bb4886"><li>State：具体的所有数据</li><ul class="notion-list notion-list-disc notion-block-2e469776e20480dbaa63fdf6e0bb4886"><li>就是数据条目，经常被修改</li></ul></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e469776e204803a9d0cdee68734188e" data-id="2e469776e204803a9d0cdee68734188e"><span><div id="2e469776e204803a9d0cdee68734188e" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e204803a9d0cdee68734188e" title="关系型数据库"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">关系型数据库</span></span></h3><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e20480c28a60c74e0f6fdce7" data-id="2e469776e20480c28a60c74e0f6fdce7"><span><div id="2e469776e20480c28a60c74e0f6fdce7" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480c28a60c74e0f6fdce7" title="关系的性质"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">关系的性质</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e469776e2048063b2a9c241ad213a1d"><li>无序性</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e2048010bd16dc07bb1b9d4a"><li>无重复行</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e2048070a501ec2543bb9df7"><li>无多值属性</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e204800f8f97c18a3f26832c" data-id="2e469776e204800f8f97c18a3f26832c"><span><div id="2e469776e204800f8f97c18a3f26832c" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e204800f8f97c18a3f26832c" title="空值Null"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">空值Null</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e469776e204800a86aad2a633e31f2b"><li>使用条件</li><ul class="notion-list notion-list-disc notion-block-2e469776e204800a86aad2a633e31f2b"><li>未知</li><li>后续将确定</li><li>不适用</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e469776e2048077bf5dda98d1a13a7a"><li>不同于0和空格</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480cb9fbcc845f7a41a32"><li>数值计算中null = 0</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e2048083bfe5ce8a43c41aed"><li>排序中null为无穷大</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e204803ab29dfbf50c1254e3" data-id="2e469776e204803ab29dfbf50c1254e3"><span><div id="2e469776e204803ab29dfbf50c1254e3" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e204803ab29dfbf50c1254e3" title="超键sk"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">超键sk</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e469776e20480e7aa1ee2b4c538d519"><li>可唯一确定的字段集合</li></ul><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e204803b894fcff569225b4d"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A2b1c90b4-1cd4-43bd-a9d6-9882471f6308%3Aimage.png?table=block&amp;id=2e469776-e204-803b-894f-cff569225b4d&amp;t=2e469776-e204-803b-894f-cff569225b4d" alt="notion image" loading="lazy" decoding="async"/></div></figure><ul class="notion-list notion-list-disc notion-block-2e469776e204801189d1dfde1659b956"><li>一个关系至少有一个sk（所有字段的集合）</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e204802cb596d1424c31443d"><li>包含sk的集合也是sk</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e2048059b8f7ec80b41c0e24"><li>只给一个关系的State，只能证明一个字段的集合不是sk，不能证明它就是sk</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e20480aaaffbfbcb2d854014" data-id="2e469776e20480aaaffbfbcb2d854014"><span><div id="2e469776e20480aaaffbfbcb2d854014" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480aaaffbfbcb2d854014" title="键"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">键</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e469776e2048001a544fceaeabe9c50"><li>极小化的sk</li><ul class="notion-list notion-list-disc notion-block-2e469776e2048001a544fceaeabe9c50"><ol start="1" class="notion-list notion-list-numbered notion-block-2e469776e20480b1a36ac419cc6d4eaf" style="list-style-type:decimal"><li>它能唯一标识一行（它是 SK）。</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-2e469776e20480e9b136fbaf31c1a072" style="list-style-type:decimal"><li><b>不能再减了</b>。你要是再从这个集合里踢掉一个字段，它就没法唯一标识了。</li></ol></ul></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480b2bf03d8038484e74a"><li>一个关系的key可唯一确定关系表的一行。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480d79469ffde9f0310d6"><li>一个关系至少有一个key，即所有字段的集合极小化的结果。</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e2048079bb11fc619824ad12" data-id="2e469776e2048079bb11fc619824ad12"><span><div id="2e469776e2048079bb11fc619824ad12" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e2048079bb11fc619824ad12" title="候选键ck"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">候选键ck</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e469776e20480158356f068065661e6"><li>一个关系的 Key 也称为 Candidate Key</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480da8a30c39643f79e8d"><li>有资格当主键pk的候选</li></ul><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e20480e1ba09faf921b42bd8"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Ad233062e-a872-4738-b601-e6de1e00777b%3Aimage.png?table=block&amp;id=2e469776-e204-80e1-ba09-faf921b42bd8&amp;t=2e469776-e204-80e1-ba09-faf921b42bd8" alt="notion image" loading="lazy" decoding="async"/></div></figure><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e20480babef6d2a3ab6ee286" data-id="2e469776e20480babef6d2a3ab6ee286"><span><div id="2e469776e20480babef6d2a3ab6ee286" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480babef6d2a3ab6ee286" title="主键pk"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">主键pk</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e469776e204803eb3dff8cc04f4aa4c"><li>包含字段个数最少的ck之一</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e2048087abc9dcbba4155288"><li>pk被选定之后不能更换</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480f4b511f66b3be2f6fe"><li>唯一且非空</li><ul class="notion-list notion-list-disc notion-block-2e469776e20480f4b511f66b3be2f6fe"><li>pk的值不能重复，且不能有空值</li></ul></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e20480fc95eecfeea0a82e90" data-id="2e469776e20480fc95eecfeea0a82e90"><span><div id="2e469776e20480fc95eecfeea0a82e90" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480fc95eecfeea0a82e90" title="外键fk"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">外键fk</span></span></h4><div class="notion-text notion-block-2e469776e20480e886b1dd8ba9cc8b11">R1 是键所在的表，R2 是被引用的表（通常主键 PK 在 R2 里）。</div><ul class="notion-list notion-list-disc notion-block-2e469776e204805db6eef2204d3f31a2"><li><b>规则 ①：门当户对。</b> FK 和它引用的 PK 必须有相同数量的字段，而且数据类型得兼容。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480d89825d1280a093b3d"><li><b>规则 ②：活要见人，死要见尸（参照完整性）。</b> 对于 R1 里的每一行，它的外键值只有两条路走：</li><ul class="notion-list notion-list-disc notion-block-2e469776e20480d89825d1280a093b3d"><ol start="1" class="notion-list notion-list-numbered notion-block-2e469776e2048054a825ed3af6931658" style="list-style-type:decimal"><li><b>要么是 </b><code class="notion-inline-code"><b>null</b></code>：代表你现在是个断线的风筝，还没跟谁搞上关系。</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-2e469776e2048077a9e5d0c28408ac52" style="list-style-type:decimal"><li><b>要么就必须在 R2 的主键里能找着。</b> 这叫<b>参照完整性约束</b>。</li></ol></ul></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480d5ac47ef8328b766bb"><li>fk需要显式指定</li></ul><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-2e469776e20480758f87e916d9633b8b" data-id="2e469776e20480758f87e916d9633b8b"><span><div id="2e469776e20480758f87e916d9633b8b" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480758f87e916d9633b8b" title="三、MySQL"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">三、MySQL</span></span></h2><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e569776e20480edbec5dec9fbab9146" data-id="2e569776e20480edbec5dec9fbab9146"><span><div id="2e569776e20480edbec5dec9fbab9146" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e569776e20480edbec5dec9fbab9146" title="SQL概览"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">SQL概览</span></span></h3><ul class="notion-list notion-list-disc notion-block-2e569776e20480c8ac6ef75f1ebb9b3c"><li>Structural Query Language</li></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480be86e6c94a6e5a2aaa"><li>核心组件</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480be86e6c94a6e5a2aaa"><li><b>DDL：数据定义语言 (Data Definition Language)</b></li><ul class="notion-list notion-list-disc notion-block-2e569776e20480188372e1923c082c0f"><li>定义数据库对象（数据库，表，字段）</li></ul><li><b>DML：数据操纵语言 (Data Manipulation Language)</b></li><ul class="notion-list notion-list-disc notion-block-2e569776e204800a86a6f836a6e034bf"><li>增删改</li></ul><li><b>DQL：数据查询语言 (Data Query Language)</b></li><ul class="notion-list notion-list-disc notion-block-2e569776e204801caa9dc0b7d4c1d14c"><li>查</li></ul><li><b>DCL：数据控制语言 (Data Control Language)</b></li><ul class="notion-list notion-list-disc notion-block-2e569776e204806f869fc5f656a93021"><li>创建DB用户，管理权限</li></ul></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480278d34c197cd760c82"><li>语句不区分大小写</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e569776e204804caa60d67ba7112983" data-id="2e569776e204804caa60d67ba7112983"><span><div id="2e569776e204804caa60d67ba7112983" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e569776e204804caa60d67ba7112983" title="DDL"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">DDL</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e569776e204809581dbc609001d1181"><li>DB的操作</li><ul class="notion-list notion-list-disc notion-block-2e569776e204809581dbc609001d1181"><li>查询</li><ul class="notion-list notion-list-disc notion-block-2e569776e2048018977cd4fe01341192"></ul><li>创建</li><ul class="notion-list notion-list-disc notion-block-2e569776e2048016972dc1e38282cd2c"><li><code class="notion-inline-code">CREATE DATABASE [IF NOT EXISTS] DB名 [DEFAULT CHARSET 字符集];</code></li><li>加<code class="notion-inline-code">IF NOT EXISTS</code> 避免因为已存在该DB而报错</li><li>一般用utf8mb4可以每个数据4bytes</li></ul><li>删</li><ul class="notion-list notion-list-disc notion-block-2e569776e204804989e1f1aafb04f81f"><li><code class="notion-inline-code"><b>DROP DATABASE [IF EXISTS] DB名;</b></code></li></ul><li>切换到指定DB</li><ul class="notion-list notion-list-disc notion-block-2e569776e204801d9fd7f1610a88251a"><li><code class="notion-inline-code">USE DB名</code></li></ul></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480e99a0be7b7189abd1e"><li>数据表</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480e99a0be7b7189abd1e"><li><code class="notion-inline-code">SHOW TABLES</code> 查询当前db所有表</li><li><code class="notion-inline-code">DESC 表名</code> 查询结构（Describe）</li><li><code class="notion-inline-code">SHOW CREATE TABLE</code> 查询指定表的建表语句</li><li>创建</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480908da5f600804247b1"></ul><li>如</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480df8e12f10367632f78"></ul></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480f988bbd675d346c6ad"><li><b>数据类型</b></li><ul class="notion-list notion-list-disc notion-block-2e569776e20480f988bbd675d346c6ad"><li>数</li><table class="notion-simple-table notion-block-2e569776e204806f98bce2271a9c076a"><tbody><tr class="notion-simple-table-row notion-simple-table-header-row notion-block-2e569776e20480dabfd5f4d08f40fd82"><td class="" style="width:219px"><div class="notion-simple-table-cell">类型</div></td><td class="" style="width:219px"><div class="notion-simple-table-cell">描述</div></td><td class="" style="width:219px"><div class="notion-simple-table-cell">大小 / Byte(s)</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480da9569d9dc200a9a8a"><td class="" style="width:219px"><div class="notion-simple-table-cell">TINYINT</div></td><td class="" style="width:219px"><div class="notion-simple-table-cell">小整数</div></td><td class="" style="width:219px"><div class="notion-simple-table-cell">1</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e204805eade2c99a302e0e6b"><td class="" style="width:219px"><div class="notion-simple-table-cell">SMALLINT</div></td><td class="" style="width:219px"><div class="notion-simple-table-cell">大整数</div></td><td class="" style="width:219px"><div class="notion-simple-table-cell">2</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e2048089b08dcb2712f5ea35"><td class="" style="width:219px"><div class="notion-simple-table-cell">MEDIUMINT</div></td><td class="" style="width:219px"><div class="notion-simple-table-cell">大整数</div></td><td class="" style="width:219px"><div class="notion-simple-table-cell">3</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e204803c980bd86bb88f9aa3"><td class="" style="width:219px"><div class="notion-simple-table-cell">INT 或 INTERGER</div></td><td class="" style="width:219px"><div class="notion-simple-table-cell">大整数</div></td><td class="" style="width:219px"><div class="notion-simple-table-cell">4</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480a5ba23f5d04b4089ae"><td class="" style="width:219px"><div class="notion-simple-table-cell">BIGINT</div></td><td class="" style="width:219px"><div class="notion-simple-table-cell">极大整数</div></td><td class="" style="width:219px"><div class="notion-simple-table-cell">8</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e2048022b35ceea18d693b7e"><td class="" style="width:219px"><div class="notion-simple-table-cell">FLOAT</div></td><td class="" style="width:219px"><div class="notion-simple-table-cell">单精度浮点数</div></td><td class="" style="width:219px"><div class="notion-simple-table-cell">4</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e204804ea279c6826a83682f"><td class="" style="width:219px"><div class="notion-simple-table-cell">DOUBLE</div></td><td class="" style="width:219px"><div class="notion-simple-table-cell">双精度浮点数</div></td><td class="" style="width:219px"><div class="notion-simple-table-cell">8</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480d3966dc3f7e819d7e3"><td class="" style="width:219px"><div class="notion-simple-table-cell">DECIMAL</div></td><td class="" style="width:219px"><div class="notion-simple-table-cell">小数值(精确定点数)</div></td><td class="" style="width:219px"><div class="notion-simple-table-cell">/</div></td></tr></tbody></table><li>文本（使用时需指定长度）</li><table class="notion-simple-table notion-block-2e569776e20480cca22fe39226ae078b"><tbody><tr class="notion-simple-table-row notion-simple-table-header-row notion-block-2e569776e20480bdaa26fbb16bbf613a"><td class="" style="width:140px"><div class="notion-simple-table-cell">类型</div></td><td class="" style="width:163px"><div class="notion-simple-table-cell">描述</div></td><td class="" style="width:342px"><div class="notion-simple-table-cell">备注</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e204805fb3e2cd46df37264c"><td class="" style="width:140px"><div class="notion-simple-table-cell">CHAR</div></td><td class="" style="width:163px"><div class="notion-simple-table-cell">定长字符串</div></td><td class="" style="width:342px"><div class="notion-simple-table-cell">长度不足用空格补足，也占固定空间，适用于性别等长度基本确定的字符串</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e204802b84f8cc3dd373c265"><td class="" style="width:140px"><div class="notion-simple-table-cell">VARCHAR</div></td><td class="" style="width:163px"><div class="notion-simple-table-cell">变长字符串</div></td><td class="" style="width:342px"><div class="notion-simple-table-cell">按实际长度分配空间，适用于用户名等长度不确定的字符串</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e204804c9048d1c8ccc7a69c"><td class="" style="width:140px"><div class="notion-simple-table-cell">TINYTEXT</div></td><td class="" style="width:163px"><div class="notion-simple-table-cell">短文本字符串</div></td><td class="" style="width:342px"><div class="notion-simple-table-cell">/</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e2048070abc3cff9237fc6ac"><td class="" style="width:140px"><div class="notion-simple-table-cell">TEXT</div></td><td class="" style="width:163px"><div class="notion-simple-table-cell">长文本数据</div></td><td class="" style="width:342px"><div class="notion-simple-table-cell">/</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480ca9a9bc66023492e6b"><td class="" style="width:140px"><div class="notion-simple-table-cell">MEDIUMTEXT</div></td><td class="" style="width:163px"><div class="notion-simple-table-cell">中等长文本数据</div></td><td class="" style="width:342px"><div class="notion-simple-table-cell">/</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480fc92d5de9c8f52cc96"><td class="" style="width:140px"><div class="notion-simple-table-cell">LONGTEXT</div></td><td class="" style="width:163px"><div class="notion-simple-table-cell">极大文本数据</div></td><td class="" style="width:342px"><div class="notion-simple-table-cell">/</div></td></tr></tbody></table><li>日期时间</li><table class="notion-simple-table notion-block-2e569776e20480dd8bb2c4340610b8b2"><tbody><tr class="notion-simple-table-row notion-simple-table-header-row notion-block-2e569776e204804cbf02c906a797d3b7"><td class="" style="width:126.25px"><div class="notion-simple-table-cell">类型</div></td><td class="" style="width:195.25px"><div class="notion-simple-table-cell">描述</div></td><td class="" style="width:164.25px"><div class="notion-simple-table-cell">格式</div></td><td class="" style="width:164.25px"><div class="notion-simple-table-cell">大小</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e204806f9c7ada50c0057ca6"><td class="" style="width:126.25px"><div class="notion-simple-table-cell">DATE</div></td><td class="" style="width:195.25px"><div class="notion-simple-table-cell">日期值</div></td><td class="" style="width:164.25px"><div class="notion-simple-table-cell">YYYY-MM-DD</div></td><td class="" style="width:164.25px"><div class="notion-simple-table-cell">3</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e204800db271f722a86bebdf"><td class="" style="width:126.25px"><div class="notion-simple-table-cell">TIME</div></td><td class="" style="width:195.25px"><div class="notion-simple-table-cell">时间值或持续时间</div></td><td class="" style="width:164.25px"><div class="notion-simple-table-cell">HH:MM:SS</div></td><td class="" style="width:164.25px"><div class="notion-simple-table-cell">3</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480f29cdbd815b509b227"><td class="" style="width:126.25px"><div class="notion-simple-table-cell">YAER</div></td><td class="" style="width:195.25px"><div class="notion-simple-table-cell">年份值</div></td><td class="" style="width:164.25px"><div class="notion-simple-table-cell">YYYY</div></td><td class="" style="width:164.25px"><div class="notion-simple-table-cell">1</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e204800a9ce1f7cd27feffa1"><td class="" style="width:126.25px"><div class="notion-simple-table-cell">DATETIME</div></td><td class="" style="width:195.25px"><div class="notion-simple-table-cell">混合日期和时间值</div></td><td class="" style="width:164.25px"><div class="notion-simple-table-cell">YYYY-MM-DD HH:MM:SS</div></td><td class="" style="width:164.25px"><div class="notion-simple-table-cell">8</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480418a1dc30fe143d391"><td class="" style="width:126.25px"><div class="notion-simple-table-cell">TIMESTAMP</div></td><td class="" style="width:195.25px"><div class="notion-simple-table-cell">混合日期和时间值、时间戳</div></td><td class="" style="width:164.25px"><div class="notion-simple-table-cell">YYYY-MM-DD HH:MM:SS</div></td><td class="" style="width:164.25px"><div class="notion-simple-table-cell">4</div></td></tr></tbody></table></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480738b83dbc94e16ffa8"><li>表的修改</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480738b83dbc94e16ffa8"><li>增</li><ul class="notion-list notion-list-disc notion-block-2e569776e204803c8dd4dce4035955d9"><div class="notion-text notion-block-2e569776e20480a3a26fe9700730f25e"><code class="notion-inline-code">ALTER TABLE 表名 ADD 字段名 类型(长度) ［COMMENT注释］［约束］;</code></div></ul><li>改</li><ul class="notion-list notion-list-disc notion-block-2e569776e2048074a0a5d6e418e1599b"><div class="notion-text notion-block-2e569776e204806abcbae313bd41a55c"><code class="notion-inline-code">AFTER TABLE 表名 MODIFY 字段名 新类型(长度)</code></div><div class="notion-text notion-block-2e569776e2048091a216df6f0c3c45ab"><code class="notion-inline-code">AFTER TABLE 表名 CHANGE 旧名 新名 类型(长度) [COMMENT] [长度]</code></div></ul><li>删</li><ul class="notion-list notion-list-disc notion-block-2e569776e204805bbad9f4f786443804"><div class="notion-text notion-block-2e569776e20480f8bb4ae4d0b5efa198"><code class="notion-inline-code">AFTER TABLE 表名 DROP 字段名</code></div></ul><li>改表名</li><ul class="notion-list notion-list-disc notion-block-2e569776e204809e88bafb8037b46ddd"><div class="notion-text notion-block-2e569776e20480c888a0d51c8955a7f4"><code class="notion-inline-code">ALTER TABLE 表名 RENAME TO 新表名</code></div></ul><li>删表</li><ul class="notion-list notion-list-disc notion-block-2e569776e204807f8fa6dc8cc7b1c3fa"><div class="notion-text notion-block-2e569776e2048084a259d739b200bcb3"><code class="notion-inline-code">DROP TABLE [IF EXISTS］表名；#删除表 </code></div><div class="notion-text notion-block-2e569776e204808aafe9d89ac948ae3e"><code class="notion-inline-code">TRUNCATE TABLE 表名；#删除指定表，并重创建该表</code> clear操作</div></ul></ul></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e569776e20480458bf1d61aa10f9be8" data-id="2e569776e20480458bf1d61aa10f9be8"><span><div id="2e569776e20480458bf1d61aa10f9be8" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e569776e20480458bf1d61aa10f9be8" title="DML"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">DML</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e569776e20480a2b108e3065108fb47"><li>添加数据</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480a2b108e3065108fb47"><li>字段顺序要对应</li><li>字符串和日期要用引号</li><li>不要超界</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480e0bdbfcda0f5ef3e58"><li>修改数据</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480e0bdbfcda0f5ef3e58"><div class="notion-text notion-block-2e569776e20480fb9b2ff47d35a81554"><code class="notion-inline-code">UPDATE 表名 SET 字段名1 = 值1, 字段名2 = 值2, ... [WHERE 条件]; # 修改数据</code> </div><li>一定加where，不然所有值都会改变</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480fc8a04d11fb24f7295"><li>删除数据</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480fc8a04d11fb24f7295"><div class="notion-text notion-block-2e569776e20480e5ab14ea588e47d0d0"><code class="notion-inline-code">DELETE FROM 表名 [WHERE 条件];  # 删除数据</code></div><li>同上</li><li>如果只是将值置空，用update+set null</li></ul></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e569776e20480df9b3accb8a67514d2" data-id="2e569776e20480df9b3accb8a67514d2"><span><div id="2e569776e20480df9b3accb8a67514d2" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e569776e20480df9b3accb8a67514d2" title="DQL"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">DQL</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e569776e20480dd8416c174325463f5"><li>基础查询</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480dd8416c174325463f5"><li>基本：SELECT，FROM</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480ca9d15f6f319bcfb93"></ul><li>条件：WHERE</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480a398cdfa375636ff7c"><div class="notion-text notion-block-2e569776e2048038bb24da008d9a9b9d">条件</div><li>单条件</li><ul class="notion-list notion-list-disc notion-block-2e569776e2048079b0b6dbb857412223"><table class="notion-simple-table notion-block-2e569776e20480ce81a4c6e5b22a98fc"><tbody><tr class="notion-simple-table-row notion-simple-table-header-row notion-block-2e569776e20480a0be7efa8153914ce4"><td class="" style="width:300.5px"><div class="notion-simple-table-cell">比较运算符</div></td><td class="" style="width:300.5px"><div class="notion-simple-table-cell">功能</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e2048076ab2ac9c77dc614cf"><td class="" style="width:300.5px"><div class="notion-simple-table-cell">&gt;</div></td><td class="" style="width:300.5px"><div class="notion-simple-table-cell">大于</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480f1a483feca6a116ca0"><td class="" style="width:300.5px"><div class="notion-simple-table-cell">&gt;=</div></td><td class="" style="width:300.5px"><div class="notion-simple-table-cell">大于等于</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e204805dbf2eefd0d4cb67c4"><td class="" style="width:300.5px"><div class="notion-simple-table-cell">&lt;</div></td><td class="" style="width:300.5px"><div class="notion-simple-table-cell">小于</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480d4af6df5509f49f4e8"><td class="" style="width:300.5px"><div class="notion-simple-table-cell">&lt;=</div></td><td class="" style="width:300.5px"><div class="notion-simple-table-cell">小于等于</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e2048005b82df16a67891bec"><td class="" style="width:300.5px"><div class="notion-simple-table-cell">=</div></td><td class="" style="width:300.5px"><div class="notion-simple-table-cell">等于</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e204806d928ad122015e1f79"><td class="" style="width:300.5px"><div class="notion-simple-table-cell">&lt;&gt; 或 !=</div></td><td class="" style="width:300.5px"><div class="notion-simple-table-cell">不等于</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e2048067a063f427d9940a27"><td class="" style="width:300.5px"><div class="notion-simple-table-cell">BETWEEN 最小值 AND 最大值</div></td><td class="" style="width:300.5px"><div class="notion-simple-table-cell">在某范围内的值(含最小值、最大值)</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e204800a8430c0d6fdd74e8b"><td class="" style="width:300.5px"><div class="notion-simple-table-cell">IN(...)</div></td><td class="" style="width:300.5px"><div class="notion-simple-table-cell">任一在列表中的值</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e2048016b150dbbd6f910262"><td class="" style="width:300.5px"><div class="notion-simple-table-cell">LIKE 占位符</div></td><td class="" style="width:300.5px"><div class="notion-simple-table-cell">模糊匹配(&#x27;_&#x27; 匹配单个字符, &#x27;%&#x27; 匹配多个字符)</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480bba2efc5a32a460a9e"><td class="" style="width:300.5px"><div class="notion-simple-table-cell">IS NULL</div></td><td class="" style="width:300.5px"><div class="notion-simple-table-cell">是 NULL</div></td></tr></tbody></table></ul><li>多条件</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480c0b7b1feb574f170c0"><table class="notion-simple-table notion-block-2e569776e2048021ab16d7bcab1917a3"><tbody><tr class="notion-simple-table-row notion-simple-table-header-row notion-block-2e569776e204800bb6aeea1093768373"><td class="" style="width:300.5px"><div class="notion-simple-table-cell">逻辑运算符</div></td><td class="" style="width:300.5px"><div class="notion-simple-table-cell">功能</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e2048000a055da851fefbbb0"><td class="" style="width:300.5px"><div class="notion-simple-table-cell">AND 或 &amp;&amp;</div></td><td class="" style="width:300.5px"><div class="notion-simple-table-cell">且</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e204808ca3f0d6a1cb4bf5f5"><td class="" style="width:300.5px"><div class="notion-simple-table-cell">OR 或 ||</div></td><td class="" style="width:300.5px"><div class="notion-simple-table-cell">或</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480eda2a4f9dc46435aff"><td class="" style="width:300.5px"><div class="notion-simple-table-cell">NOT 或 !</div></td><td class="" style="width:300.5px"><div class="notion-simple-table-cell">非, 否</div></td></tr></tbody></table><div class="notion-text notion-block-2e569776e20480439222f99769330d23">有以下的等价性：</div><div class="notion-text notion-block-2e569776e204801dadaddf7e14b5d308">① = SOME(…)等价于 IN(…) .</div><div class="notion-text notion-block-2e569776e2048036bc2dc074ad17238d">② &lt;&gt; SOME(…) 不等价于 NOT IN(…) .</div><div class="notion-text notion-block-2e569776e2048025a616de164ad0d0ed">③ &lt;&gt; ALL(…) 等级与 NOT IN(…) .</div></ul></ul><li>聚合函数：COUNT，MAX，MIN，AVG，SUM</li><ul class="notion-list notion-list-disc notion-block-2e569776e2048029ad53dc4d8a1e99ff"><table class="notion-simple-table notion-block-2e569776e20480208386cfc6a11026f1"><tbody><tr class="notion-simple-table-row notion-simple-table-header-row notion-block-2e569776e2048028a359cf37f37b26f6"><td class="" style="width:314.5px"><div class="notion-simple-table-cell">聚合函数</div></td><td class="" style="width:314.5px"><div class="notion-simple-table-cell">功能</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480ac9941f3bdf5a7963f"><td class="" style="width:314.5px"><div class="notion-simple-table-cell">COUNT</div></td><td class="" style="width:314.5px"><div class="notion-simple-table-cell">统计数量</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e2048054a500d320b1ff707c"><td class="" style="width:314.5px"><div class="notion-simple-table-cell">MAX</div></td><td class="" style="width:314.5px"><div class="notion-simple-table-cell">最大值</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480fdae80cee62ddd1176"><td class="" style="width:314.5px"><div class="notion-simple-table-cell">MIN</div></td><td class="" style="width:314.5px"><div class="notion-simple-table-cell">最小值</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e204805f9e1ed2afc878ba25"><td class="" style="width:314.5px"><div class="notion-simple-table-cell">AVG</div></td><td class="" style="width:314.5px"><div class="notion-simple-table-cell">平均值</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480c09301c8be9dfe4a99"><td class="" style="width:314.5px"><div class="notion-simple-table-cell">SUM</div></td><td class="" style="width:314.5px"><div class="notion-simple-table-cell">求和</div></td></tr></tbody></table><li><code class="notion-inline-code">SELECT AVG(age) FROM agetable;</code> 统计平均年龄（输出数字）</li><li>Null不参与运算</li></ul><li>分组：GROUP BY</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480dcac07dd8917723ac5"><li>过滤分组后的数据：HAVING</li><ul class="notion-list notion-list-disc notion-block-2e569776e204800986a4d8b90c42a952"><li>HAVING可用聚合函数作为条件</li></ul><li>分组后，除了分组字段，其他字段会被折叠，无法单独查询。所以只能查询聚合函数或者分组字段。</li></ul><li>分页：LIMIT</li><li>排序：ORDER BY</li><ul class="notion-list notion-list-disc notion-block-2e569776e204801eb2b5dfe1d184fdfe"><li>ASC：非降序（默认），从小到大</li><li>DESC：非升序，从大到小</li></ul></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e204805ab279e81d686c2d8f"><li>编写顺序：</li></ul><ul class="notion-list notion-list-disc notion-block-2e569776e204802ea0cdd93143f539e4"><li>执行顺序：</li></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480fb83e0d8c53c745bde"><li>所以别名（AS后的）只能在SELECT后用，不能在WHERE和GROUP BY用</li></ul><div class="notion-text notion-block-2e569776e2048078b9bbdd9ef763095e">综合应用：</div><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e569776e204803aa178e4ed55fb21f7" data-id="2e569776e204803aa178e4ed55fb21f7"><span><div id="2e569776e204803aa178e4ed55fb21f7" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e569776e204803aa178e4ed55fb21f7" title="DCL"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">DCL</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e569776e20480b581f0cf562bc8ca75"><li>创建用户<b> (CREATE USER)</b></li><ul class="notion-list notion-list-disc notion-block-2e569776e20480b581f0cf562bc8ca75"><li>主机名</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480628ee3f532647888ca"><li>localhost为只能本地连接</li><li>可以为’%’，即可以在任意ip连接</li></ul></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e2048012841df289220432d5"><li>赋予权限（GRANT）</li><ul class="notion-list notion-list-disc notion-block-2e569776e2048012841df289220432d5"><li>准许 <code class="notion-inline-code">caixukun</code> 在 <code class="notion-inline-code">company</code> 这个库里，干所有的脏活累活（增删改查）。</li><li><code class="notion-inline-code">company.*</code>：意思是 <code class="notion-inline-code">company</code> 库下面的<b>所有表</b>，他都能碰。</li><li>但是不能建表（CREATE），也不能删表（DROP）。</li><li>将所有库，所有表的所有权限给<code class="notion-inline-code">richardliu</code> 。</li><li>可以在权限列表中加可以操作的具体属性：</li><ul class="notion-list notion-list-disc notion-block-2e569776e204806ebe64f86aa782b2ff"><li><code class="notion-inline-code">GRANT UPDATE(stock_code, stock_price) ON stock TO brown;</code></li><li>只能操作<code class="notion-inline-code">stock_code, stock_price</code> 属性</li></ul></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480b99590e32f1da4f029"><li>移除权限（REVOKE）</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480b99590e32f1da4f029"><li>大体跟GRANT写法类似</li><li>更加根本，删除用户：<code class="notion-inline-code">DROP USER &#x27;caixukun&#x27;@&#x27;localhost&#x27;;</code></li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480ba972fff5f67f3cb7f"><li>更新缓存（<code class="notion-inline-code">FLUSH PRIVILEGES</code>）</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480ba972fff5f67f3cb7f"><li>防止数据库没反应过来</li></ul></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e569776e2048061ac76e50bed75ff27" data-id="2e569776e2048061ac76e50bed75ff27"><span><div id="2e569776e2048061ac76e50bed75ff27" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e569776e2048061ac76e50bed75ff27" title="约束"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">约束</span></span></h3><table class="notion-simple-table notion-block-2e569776e2048007b2c2ce2f7b27dbe9"><tbody><tr class="notion-simple-table-row notion-simple-table-header-row notion-block-2e569776e20480408c2ad6113d097774"><td class="" style="width:137.6666717529297px"><div class="notion-simple-table-cell">约束</div></td><td class="" style="width:335.6666717529297px"><div class="notion-simple-table-cell">描述</div></td><td class="" style="width:235.66666666666666px"><div class="notion-simple-table-cell">关键字</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480a1adb8f57c6f7c0d6a"><td class="" style="width:137.6666717529297px"><div class="notion-simple-table-cell">非空约束</div></td><td class="" style="width:335.6666717529297px"><div class="notion-simple-table-cell">限制字段数据不能为空 NULL</div></td><td class="" style="width:235.66666666666666px"><div class="notion-simple-table-cell">NOT NULL</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e2048058946cf25e13391398"><td class="" style="width:137.6666717529297px"><div class="notion-simple-table-cell">唯一约束</div></td><td class="" style="width:335.6666717529297px"><div class="notion-simple-table-cell">保证该字段的所有数据不重复，可用在身份证号、用户名等</div></td><td class="" style="width:235.66666666666666px"><div class="notion-simple-table-cell">UNIQUE</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480b29298febbeca6bae6"><td class="" style="width:137.6666717529297px"><div class="notion-simple-table-cell">主键约束</div></td><td class="" style="width:335.6666717529297px"><div class="notion-simple-table-cell">主键是一行数据的唯一标识，要求非空且唯一</div></td><td class="" style="width:235.66666666666666px"><div class="notion-simple-table-cell">PRIMARY KEY</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e2048067a64aff775eacbb65"><td class="" style="width:137.6666717529297px"><div class="notion-simple-table-cell">默认约束</div></td><td class="" style="width:335.6666717529297px"><div class="notion-simple-table-cell">保存数据时，若未指定该字段的值，则用默认值</div></td><td class="" style="width:235.66666666666666px"><div class="notion-simple-table-cell">DEFAULT</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e2048036a244ed7c4237f196"><td class="" style="width:137.6666717529297px"><div class="notion-simple-table-cell">检查约束</div></td><td class="" style="width:335.6666717529297px"><div class="notion-simple-table-cell">保证字段值满足某条件</div></td><td class="" style="width:235.66666666666666px"><div class="notion-simple-table-cell">CHECK</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480888fbccc9cea8b67bc"><td class="" style="width:137.6666717529297px"><div class="notion-simple-table-cell">外键约束</div></td><td class="" style="width:335.6666717529297px"><div class="notion-simple-table-cell">建立两张表间的连接，保证数据的一致性和完整性</div></td><td class="" style="width:235.66666666666666px"><div class="notion-simple-table-cell">FOREIGN KEY</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e204807eb948f787ca075ac9"><td class="" style="width:137.6666717529297px"><div class="notion-simple-table-cell">自增</div></td><td class="" style="width:335.6666717529297px"><div class="notion-simple-table-cell">整型数据自动增长，可用在 id 等</div></td><td class="" style="width:235.66666666666666px"><div class="notion-simple-table-cell">AUTO_INCREMENT</div></td></tr></tbody></table><div class="notion-text notion-block-2e569776e204807c85f9d692800bf0a1">在建表或改属性时，在条目后面加上对应关键词就行。</div><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e569776e204807dbeedff317607cd88" data-id="2e569776e204807dbeedff317607cd88"><span><div id="2e569776e204807dbeedff317607cd88" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e569776e204807dbeedff317607cd88" title="外键约束"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">外键约束</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e569776e2048068b385f63b6a8c53b8"><li>建立表与表之间的关系，确保引用完整性</li></ul><ul class="notion-list notion-list-disc notion-block-2e569776e2048087bfbfee96c54394cd"><li>例</li><ul class="notion-list notion-list-disc notion-block-2e569776e2048087bfbfee96c54394cd"><li>主表：部门信息</li><li>从表：员工信息，其中一个属性是部门，需要引用部门的信息。这个属性必须填主表上主键有的项</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480be8c1dda95f2b6363b"><li>从表插入属性要检查主表主键是否存在</li></ul><ul class="notion-list notion-list-disc notion-block-2e569776e204807dbf49f12716667b71"><li>主表不能随便删改</li></ul><details class="notion-toggle notion-block-2e569776e2048060b2a8f7434a7c5377"><summary>级联</summary><div><div class="notion-text notion-block-2e569776e2048004ae5ae8803ac57610">外键里最<b>凶残</b>的功能。如果你不想让数据库一直报错拦着你删数据，你可以开启<b>“殉情模式”</b>。</div><ul class="notion-list notion-list-disc notion-block-2e569776e2048078be97de68f60fca75"><li><code class="notion-inline-code"><b>ON DELETE CASCADE</b></code><b>：</b></li><ul class="notion-list notion-list-disc notion-block-2e569776e2048078be97de68f60fca75"><li>你删了“技术部”？</li><li>数据库甚至<b>不问你</b>，直接把 <code class="notion-inline-code">employee</code> 表里所有属于技术部的人<b>全杀光</b>！</li><li><b>后果：</b> 这是一个<b>灭门惨案</b>的开关。手抖删错一行主表，从表几万条数据瞬间蒸发，哭都没地方哭！</li></ul></ul></div></details><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e569776e204808a8e6ed0dd1b82c84b" data-id="2e569776e204808a8e6ed0dd1b82c84b"><span><div id="2e569776e204808a8e6ed0dd1b82c84b" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e569776e204808a8e6ed0dd1b82c84b" title="DQL多表查询"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">DQL多表查询</span></span></h4><div class="notion-text notion-block-2e569776e204800fbf95ddeb2b17c732"><b>多表关系</b></div><ul class="notion-list notion-list-disc notion-block-2e569776e20480f6addcde0d10f32001"><li>一对多/多对一</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480f6addcde0d10f32001"><li>多的一方建立外键，指向另一方主键</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480d3847adacb375bf424"><li>多对多</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480d3847adacb375bf424"><li>建立中间表，至少包含两个外键，分别关联两表的主键</li><li>案例：</li><ul class="notion-list notion-list-disc notion-block-2e569776e204807487d0d5628658ec10"><li>一个 <b>学生</b> 可以选 <b>多门课</b>。</li><li>一门 <b>课</b> 也可以被 <b>多个学生</b> 选。</li><ol start="1" class="notion-list notion-list-numbered notion-block-2e569776e204806a810ce8ce9e06cf34" style="list-style-type:decimal"><li>建一张 <code class="notion-inline-code">Student</code> 表。</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-2e569776e204809c98a1dbb5614a9801" style="list-style-type:decimal"><li>建一张 <code class="notion-inline-code">Course</code> 表。</li></ol><ol start="3" class="notion-list notion-list-numbered notion-block-2e569776e204801aa106de4f0d2db7ad" style="list-style-type:decimal"><li><b>再建一张 </b><code class="notion-inline-code"><b>Student_Course_Relation</b></code><b> 表（中间表/关联表）！</b></li></ol><li><b>中间表里存啥？</b> 就存 <code class="notion-inline-code">student_id</code> 和 <code class="notion-inline-code">course_id</code>。</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480dcacefe10e023edc0a"><li>一行记录表示：“张三 选了 数据库原理”。</li><li>另一行记录表示：“李四 也选了 数据库原理”。</li></ul></ul></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480e3b8cbd670877ee1d5"><li>一对一</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480e3b8cbd670877ee1d5"><li>多用于单表拆分，基础字段一张表，详情字段一张表，提升性能，隔离隐私</li><li>外键建在哪边都行，关联另一边的主键，但是外键要加上 <code class="notion-inline-code"><b>UNIQUE</b></code><b>（唯一）</b> 约束！</li></ul></ul><div class="notion-text notion-block-2e569776e204802ca931d79452ce2012"><b>多表查询</b></div><ul class="notion-list notion-list-disc notion-block-2e569776e20480648e12d35cd1203141"><li>实际上是求多张表的笛卡尔积，强制两两匹配</li></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480ca9f07c914b288c614"><li>需要消除冗余</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480ca9f07c914b288c614"><li>WHERE + 条件：<code class="notion-inline-code">SELECT * FROM emp, dept WHERE emp.dept_id = dept.id;</code></li><ul class="notion-list notion-list-disc notion-block-2e569776e20480629ceaddcd618185bb"><li>老标准</li></ul><li>连接查询</li><ul class="notion-list notion-list-disc notion-block-2e569776e204800eae2cca849e3c1be5"><li><b>内连接</b></li><ul class="notion-list notion-list-disc notion-block-2e569776e2048077994cef9f17eca420"><li>查询集合A，B交集的数据</li><li><code class="notion-inline-code">SELECT * FROM emp e </code><code class="notion-inline-code"><b>[INNER] JOIN</b></code><code class="notion-inline-code"> dept d </code><code class="notion-inline-code"><b>ON</b></code><code class="notion-inline-code"> e.dept_id = d.id;</code></li></ul><li><b>外连接</b></li><ul class="notion-list notion-list-disc notion-block-2e569776e204804ea586e0e78bc30707"><li>查询左表/右表所有数据和交集部分，常用左外连接，因为两表顺序可以反过来</li><li><code class="notion-inline-code">SELECT * FROM 表1 LEFT JOIN 表2 ON 条件...</code> </li><li>返回左表的所有条目和右表的部分条目，如果左表的条目在右表没有，则返回null</li></ul><details class="notion-toggle notion-block-2e569776e2048064bfa5c567e4cc6d33"><summary>例</summary><div><div class="notion-text notion-block-2e569776e204807fa7cbe4f6edd0c4ef">先建两个表，一个是 <code class="notion-inline-code"><b>beauty</b></code><b>（女神表）</b>，一个是 <code class="notion-inline-code"><b>car</b></code><b>（豪车表）</b>。</div><div class="notion-text notion-block-2e569776e204801a946ff47ac1ec3d62"><code class="notion-inline-code"><b>beauty</b></code><b> 表（女神）：</b></div><table class="notion-simple-table notion-block-2e569776e204806f8acbfe34af96e634"><tbody><tr class="notion-simple-table-row notion-simple-table-header-row notion-block-2e569776e2048088b72bed7cac31743f"><td class="" style="width:120px"><div class="notion-simple-table-cell">id</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">name (名字)</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e204808199c6d53d2a178c22"><td class="" style="width:120px"><div class="notion-simple-table-cell">1</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">冰冰</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480b88c13dd8887e1fec8"><td class="" style="width:120px"><div class="notion-simple-table-cell">2</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">圆圆</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480ec8084e543b182f318"><td class="" style="width:120px"><div class="notion-simple-table-cell">3</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">凤姐</div></td></tr></tbody></table><div class="notion-text notion-block-2e569776e20480c7a858de436ad048bc"><code class="notion-inline-code"><b>car</b></code><b> 表（豪车）：</b></div><table class="notion-simple-table notion-block-2e569776e20480b5b6fcd6df3b29849a"><tbody><tr class="notion-simple-table-row notion-simple-table-header-row notion-block-2e569776e204809cb8ffc8b35f42a59e"><td class="" style="width:120px"><div class="notion-simple-table-cell">owner_id</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">car_name (车名)</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480979fc3c5cfceea062c"><td class="" style="width:120px"><div class="notion-simple-table-cell">1</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">劳斯莱斯</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480d790b4fec40bb9e1e0"><td class="" style="width:120px"><div class="notion-simple-table-cell">2</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">法拉利</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e2048059a9b6ef270cd1725e"><td class="" style="width:120px"><div class="notion-simple-table-cell">4</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">五菱宏光</div></td></tr></tbody></table><div class="notion-text notion-block-2e569776e204804caf5fc81da7e42138"><em>注：你看好了，凤姐（id 3）没车，五菱宏光（owner_id 4）的主人不在女神表里。</em></div><hr class="notion-hr notion-block-2e569776e2048066b4f5dc2e279a7934"/><h4 class="notion-h notion-h3 notion-block-2e569776e204808b9206c658d42c500b" data-id="2e569776e204808b9206c658d42c500b"><span><div id="2e569776e204808b9206c658d42c500b" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e569776e204808b9206c658d42c500b" title="1. 内连接 (INNER JOIN) —— 返回的是“门当户对”"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">1. 内连接 (INNER JOIN) —— 返回的是“门当户对”</span></span></h4><div class="notion-text notion-block-2e569776e20480d681d9eb18a688879f"><b>返回结果：</b> 只有两边都能匹配上的数据。</div><div class="notion-text notion-block-2e569776e204808da2c5f641b4731c0b"><b>SQL语句：</b></div><div class="notion-text notion-block-2e569776e2048045a858e278c7f4cad2"><b>返回什么？</b></div><table class="notion-simple-table notion-block-2e569776e204801994f9e7b88ef0e40d"><tbody><tr class="notion-simple-table-row notion-simple-table-header-row notion-block-2e569776e204806f8919ca91f317ca9e"><td class="" style="width:120px"><div class="notion-simple-table-cell">name</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">car_name</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480a8a1bcedc57346edec"><td class="" style="width:120px"><div class="notion-simple-table-cell">冰冰</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">劳斯莱斯</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e2048005b7e8cc55b1843e5e"><td class="" style="width:120px"><div class="notion-simple-table-cell">圆圆</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">法拉利</div></td></tr></tbody></table><ul class="notion-list notion-list-disc notion-block-2e569776e20480408b38c43b0f691af8"><li><b>凤姐呢？</b> 凤姐没车，滚蛋了。</li></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480de8a09d6d083fbc740"><li><b>五菱宏光呢？</b> 没主人的破车，数据库理都不理。</li></ul><hr class="notion-hr notion-block-2e569776e2048033b7bae1f1bec48f52"/><h4 class="notion-h notion-h3 notion-block-2e569776e2048046b899fa0a61fc3bf1" data-id="2e569776e2048046b899fa0a61fc3bf1"><span><div id="2e569776e2048046b899fa0a61fc3bf1" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e569776e2048046b899fa0a61fc3bf1" title="2. 左外连接 (LEFT JOIN) —— 返回的是“女神及其挂件”"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">2. 左外连接 (LEFT JOIN) —— 返回的是“女神及其挂件”</span></span></h4><div class="notion-text notion-block-2e569776e20480df9517cdc8649ccf65"><b>返回结果：</b> 左表（beauty）的所有人，加上右表能匹配上的数据。匹配不上的，右边填 <code class="notion-inline-code">NULL</code>。</div><div class="notion-text notion-block-2e569776e20480c0a91ee5310a48645a"><b>SQL语句：</b></div><div class="notion-text notion-block-2e569776e20480daa335c9845bbc803f"><b>返回什么？</b></div><table class="notion-simple-table notion-block-2e569776e20480238b53f4a4e4d05eb0"><tbody><tr class="notion-simple-table-row notion-simple-table-header-row notion-block-2e569776e20480d7b4cac07b64caa639"><td class="" style="width:120px"><div class="notion-simple-table-cell">name</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">car_name</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e2048078afafcd9e448cffe1"><td class="" style="width:120px"><div class="notion-simple-table-cell">冰冰</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">劳斯莱斯</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480e2957be3800d97b25d"><td class="" style="width:120px"><div class="notion-simple-table-cell">圆圆</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">法拉利</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e204802683fac0db8d2913a2"><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>凤姐</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>NULL</b></div></td></tr></tbody></table><ul class="notion-list notion-list-disc notion-block-2e569776e2048007a167e60117ec747d"><li><b>凤姐怎么出来了？</b> 因为她是左表的人，她是主子。虽然她没车，但数据库得给她留个位置，没车就给她分配个 <code class="notion-inline-code">NULL</code>（空气）。</li></ul><ul class="notion-list notion-list-disc notion-block-2e569776e204809995b8d6d16f893e02"><li><b>五菱宏光呢？</b> 依然没戏，因为它在右表，没人领它，它就得滚。</li></ul><hr class="notion-hr notion-block-2e569776e20480d3a96cfb0101f76331"/><h4 class="notion-h notion-h3 notion-block-2e569776e2048061b414f6e13b5b9706" data-id="2e569776e2048061b414f6e13b5b9706"><span><div id="2e569776e2048061b414f6e13b5b9706" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e569776e2048061b414f6e13b5b9706" title="3. 右外连接 (RIGHT JOIN) —— 返回的是“名车博览会”"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3. 右外连接 (RIGHT JOIN) —— 返回的是“名车博览会”</span></span></h4><div class="notion-text notion-block-2e569776e20480e0b85fc3c522967bb4"><b>返回结果：</b> 右表（car）的所有车，加上左表能匹配上的主人。匹配不上的，左边填 <code class="notion-inline-code">NULL</code>。</div><div class="notion-text notion-block-2e569776e204805ea6decc30ad0475f6"><b>SQL语句：</b></div><div class="notion-text notion-block-2e569776e20480909701d4e7c7361fb0"><b>返回什么？</b></div><table class="notion-simple-table notion-block-2e569776e204800fbbf4f9b439b42a59"><tbody><tr class="notion-simple-table-row notion-simple-table-header-row notion-block-2e569776e20480ad897dd7b2b18d7878"><td class="" style="width:120px"><div class="notion-simple-table-cell">name</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">car_name</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e204804d8d64cfa75c7a3d17"><td class="" style="width:120px"><div class="notion-simple-table-cell">冰冰</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">劳斯莱斯</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480839210d81ff504b395"><td class="" style="width:120px"><div class="notion-simple-table-cell">圆圆</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">法拉利</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480a6a4c9c7231492339b"><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>NULL</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>五菱宏光</b></div></td></tr></tbody></table><ul class="notion-list notion-list-disc notion-block-2e569776e204806cbf0fc754c30e5e5a"><li><b>五菱宏光怎么出来了？</b> 因为它是右表的，主子必须全部露脸。虽然它没主人，但数据库会在主人那一栏给它写个 <code class="notion-inline-code">NULL</code>。</li></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480aca8aed91dd1fab5a9"><li><b>凤姐呢？</b> 凤姐没车，在这种“名车博览会”的逻辑下，没车的女人连进门扫码的资格都没有！</li></ul></div></details><li><b>自连接</b></li><ul class="notion-list notion-list-disc notion-block-2e569776e2048012bb6afcff8abd689b"><li>自连接可以是内连接也可以是外连接</li><li>解决的就是那种<b>“老子表里某个人是我表里另一个人他爹”</b>这种狗屁倒灶的层级关系</li><li><b>一张表拆成两张虚表 + 起两个不一样的外号 + 按逻辑连起来</b></li><details class="notion-toggle notion-block-2e569776e204802eae03c8e5cc10a284"><summary>例</summary><div><div class="notion-text notion-block-2e569776e204806a97bdf2fb84c1af5d">有一张员工表 <code class="notion-inline-code">emp</code>：</div><table class="notion-simple-table notion-block-2e569776e20480ea9676dd3491733623"><tbody><tr class="notion-simple-table-row notion-simple-table-header-row notion-block-2e569776e20480a1acd4f954a191e0c1"><td class="" style="width:120px"><div class="notion-simple-table-cell">id</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">name (员工名)</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">manager_id (领导的ID)</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480f2af2bffe246f27d26"><td class="" style="width:120px"><div class="notion-simple-table-cell">1</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">老王 (大BOSS)</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">NULL</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e204800497cdf888dc073f11"><td class="" style="width:120px"><div class="notion-simple-table-cell">2</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">张三</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">1</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480a5b119cca1a833a7c8"><td class="" style="width:120px"><div class="notion-simple-table-cell">3</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">李四</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">1</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e20480dfa251c1f4de33c9b1"><td class="" style="width:120px"><div class="notion-simple-table-cell">4</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">王五</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">2</div></td></tr></tbody></table><div class="notion-text notion-block-2e569776e20480dab427c04e55b00112">查询谁是谁的领导。</div><hr class="notion-hr notion-block-2e569776e2048001b0bdc2155b3f155a"/><div class="notion-text notion-block-2e569776e204807fb258c9872e5eb075"><b>返回结果：</b></div><table class="notion-simple-table notion-block-2e569776e20480bd9118dec8bb97e9d8"><tbody><tr class="notion-simple-table-row notion-simple-table-header-row notion-block-2e569776e20480b2a772d97836e341b1"><td class="" style="width:120px"><div class="notion-simple-table-cell">员工</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">领导</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e2048029b489d58aaff46ac1"><td class="" style="width:120px"><div class="notion-simple-table-cell">张三</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">老王</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e2048046b416f3a3dc0f6393"><td class="" style="width:120px"><div class="notion-simple-table-cell">李四</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">老王</div></td></tr><tr class="notion-simple-table-row notion-block-2e569776e204809e969bd538bf25b78a"><td class="" style="width:120px"><div class="notion-simple-table-cell">王五</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">张三</div></td></tr></tbody></table><ul class="notion-list notion-list-disc notion-block-2e569776e20480d8a8b5c153fe06d778"><li>老王因为是顶级BOSS，他没有领导（manager_id是NULL），所以在 <code class="notion-inline-code">INNER JOIN</code> 下，老王直接被踢出名单了。你要是想把老王也查出来，就得用 <code class="notion-inline-code"><b>LEFT JOIN</b></code>！</li></ul></div></details></ul><li>联合查询</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480699563dc981191ffd8"><li>合并多次查询结果形成新表。</li><li>要求行属性一致，列数相等</li><li>UNION：删掉重复项</li><li>UNION ALL：直接拼合</li></ul><li>子查询（嵌套查询）</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480848a0bcf1b45138d40"><li>SQL语句中嵌套 <code class="notion-inline-code">SELECT</code> 语句称为嵌套查询或子查询.</li><ol start="1" class="notion-list notion-list-numbered notion-block-2e569776e20480bbabe1eb45139eaea8" style="list-style-type:decimal"><li>子查询外部的语句可以是 <code class="notion-inline-code">INSERT</code>、<code class="notion-inline-code">UPDATE</code>、<code class="notion-inline-code">DELETE</code>、<code class="notion-inline-code">SELECT</code> 等.</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-2e569776e204802ba2cdd0c03b562152" style="list-style-type:decimal"><li>按子查询的结果分类:</li></ol><div class="notion-text notion-block-2e569776e204809d922cfbaee8f32427">① 标量子查询: 子查询的结果是一个值.</div><div class="notion-text notion-block-2e569776e204801ca170f6155eb2bfe2">② 列子查询: 子查询结果为一列.</div><div class="notion-text notion-block-2e569776e20480449e2df8bc11ecc870">③ 行子查询: 子查询结果为一行.</div><div class="notion-text notion-block-2e569776e2048094a118fa1493c70fc4">④ 表子查询: 子查询结果为多行多列的表.</div><ol start="3" class="notion-list notion-list-numbered notion-block-2e569776e204800ba02fe2b510709ae0" style="list-style-type:decimal"><li> 按子查询的位置分类:</li></ol><div class="notion-text notion-block-2e569776e2048021a7d3d716d2f6ed4a">① <code class="notion-inline-code">WHERE</code> 之后.</div><div class="notion-text notion-block-2e569776e204808483e1faf3c6390078">② <code class="notion-inline-code">FROM</code> 之后.</div><div class="notion-text notion-block-2e569776e20480f0bb8efced89a99eef">③ <code class="notion-inline-code">SELECT</code> 之后.</div></ul><li>存在判定EXIST()</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480f3bdb9ef03dccac1e4"><li>可以替换掉子查询操作，性能更优</li><li>子查询：<b>以“结果集”为中心，</b>把表作为单位查询</li><li>存在判定：<b>以“逻辑判定”为中心，</b>把条目作为单位查询</li><details class="notion-toggle notion-block-2e569776e20480368b06e4739149bd20"><summary>例</summary><div><div class="notion-text notion-block-2e569776e2048043aebefecd1eba5c09">在学生表 &quot;students&quot; 和修课表 &quot;enrollment&quot; 中进行如下查询:</div><div class="notion-text notion-block-2e569776e2048091b8bdc5675e7a8d1f">(1) 查询至少修了一门课的学生的信息.</div><div class="notion-text notion-block-2e569776e204804bb0c8e1c0f82f9fa4">(2) 查询没有修编号 &quot;21003001&quot; 的课程的学生的信息.</div></div></details></ul></ul></ul></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e569776e204802abe91c43b88344be6" data-id="2e569776e204802abe91c43b88344be6"><span><div id="2e569776e204802abe91c43b88344be6" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e569776e204802abe91c43b88344be6" title="视图"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">视图</span></span></h3><ul class="notion-list notion-list-disc notion-block-2e569776e204803aa97aca39e4ad0f80"><li>虚拟的表，数据存在，但不是真实存在库里的表，实际上是一个动态生成的查询结果</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e569776e20480719df4cb49304064eb" data-id="2e569776e20480719df4cb49304064eb"><span><div id="2e569776e20480719df4cb49304064eb" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e569776e20480719df4cb49304064eb" title="增删改查"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">增删改查</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e569776e20480ecbdbce1e634cea043"><li>创建</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480ecbdbce1e634cea043"><li> <code class="notion-inline-code"><b>OR REPLACE</b></code>：要是视图已经存在了，数据库不会给你报错，而是覆盖</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e204804589e3dec8b31969c0"><li>查询: 同查询数据表.</li><ul class="notion-list notion-list-disc notion-block-2e569776e204804589e3dec8b31969c0"></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480328f87d4fe5967cec9"><li>修改.</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480328f87d4fe5967cec9"><div class="notion-text notion-block-2e569776e20480d2b063ef627d7e13bd">或</div></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480518e2ddcc7bc210c17"><li>删除.</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480518e2ddcc7bc210c17"><li>删视图不会影响原始数据</li></ul></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e569776e20480c0966ffe8efe6e0331" data-id="2e569776e20480c0966ffe8efe6e0331"><span><div id="2e569776e20480c0966ffe8efe6e0331" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e569776e20480c0966ffe8efe6e0331" title="检查选项"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">检查选项</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e569776e20480759d59c1ca3a8556d2"><li><code class="notion-inline-code"><b>WITH ... CHECK OPTION</b></code><b> ：过滤网</b></li><ul class="notion-list notion-list-disc notion-block-2e569776e20480759d59c1ca3a8556d2"><li>检查更改的每一行是否符合视图定义</li><ul class="notion-list notion-list-disc notion-block-2e569776e2048057ade9c0b0b5c958e6"><li>对视图进行修改的时候，如果修改的项不符合视图的条件，则报错</li><li>直接对源表进行修改，不生效</li></ul></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e204805ba7bdf0e4733992ad"><li>两种选项</li><ul class="notion-list notion-list-disc notion-block-2e569776e204805ba7bdf0e4733992ad"><li><code class="notion-inline-code">CASCADED</code> 级联（默认）</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480afbcb7da9bfd92c17d"><li><b>逻辑：</b> 如果视图 B 是基于视图 A 建的，而视图 B 加了 <code class="notion-inline-code">WITH CASCADED CHECK OPTION</code>。</li><li><b>连坐：</b> 当你往 B 里塞数据时，它不仅要检查 B 的条件，还会<b>强行</b>检查 A 的条件，<b>不管 A 当初建的时候有没有加检查选项！</b></li></ul><li><code class="notion-inline-code">LOCAL</code> 本地</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480bbae5fc1fdb40445d0"><li>如果只有B加了检查选项，只会检查B，不会检查A</li></ul></ul></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e569776e2048021bfeffa643bb8865d" data-id="2e569776e2048021bfeffa643bb8865d"><span><div id="2e569776e2048021bfeffa643bb8865d" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e569776e2048021bfeffa643bb8865d" title="视图的更新"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">视图的更新</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e569776e204802cbf26c1ced7a3ef85"><li>只有当视图的条目和基础表中的条目一一对应才能改，不然数据库不知道要改什么</li></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480a5a048e97fcb456df5"><li>如果包含下面这些项之一都不能更新：</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480a5a048e97fcb456df5"><li>聚合函数或窗口函数, 如 <code class="notion-inline-code">SUM</code>、<code class="notion-inline-code">MIN</code>、<code class="notion-inline-code">MAX</code>、<code class="notion-inline-code">COUNT</code>.</li><li><code class="notion-inline-code">DISTINCT</code>.</li><li><code class="notion-inline-code">GROUP BY</code>.</li><li><code class="notion-inline-code">HAVING</code>.</li><li><code class="notion-inline-code">UNION</code> 或 <code class="notion-inline-code">UNION ALL</code>.</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e204803ea5f3e3f90e4aacfd"><li><b>能更新的：</b> <code class="notion-inline-code">SELECT * FROM table WHERE ...</code>（原封不动，只是加了过滤）。</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e569776e20480819700fb2740f0cae1" data-id="2e569776e20480819700fb2740f0cae1"><span><div id="2e569776e20480819700fb2740f0cae1" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e569776e20480819700fb2740f0cae1" title="视图的优缺点"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">视图的优缺点</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e569776e204804e93b9d8e9e026ad8c"><li>优点</li><ul class="notion-list notion-list-disc notion-block-2e569776e204804e93b9d8e9e026ad8c"><li>简化查询语句</li><li>安全</li><li>数据独立性</li><li>提高编程效率</li><li>存储占用小</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480e2a7f2edb3534c2629"><li>确定</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480e2a7f2edb3534c2629"><li>引用视图花时间</li><li>可能不被直接更新（LOCAL）</li></ul></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e569776e2048060bd42c261f013130f" data-id="2e569776e2048060bd42c261f013130f"><span><div id="2e569776e2048060bd42c261f013130f" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e569776e2048060bd42c261f013130f" title="其他存储对象"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">其他存储对象</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e569776e2048057b8fae27e764144f8"><li>存储过程</li><ul class="notion-list notion-list-disc notion-block-2e569776e2048057b8fae27e764144f8"><li>存放在数据库中的完成特定功能的SQL语句集</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480ccab68ce7f26a17a77"><li>存储函数</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480ccab68ce7f26a17a77"><li>返回计算结果，可以直接被SQL表达式调用</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e2048074b4d6c1387ed52948"><li>触发器</li><ul class="notion-list notion-list-disc notion-block-2e569776e2048074b4d6c1387ed52948"><li>自动检测某表的改变</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e2048080ac7ef3372adf2340"><li>事件</li><ul class="notion-list notion-list-disc notion-block-2e569776e2048080ac7ef3372adf2340"><li>定时任务</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480bf92c6ec0bb247ec27"><li>游标</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480bf92c6ec0bb247ec27"><li>用于遍历结果集，每次返回一行</li></ul></ul><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-2e569776e20480558c1cdeb2d06c50bc" data-id="2e569776e20480558c1cdeb2d06c50bc"><span><div id="2e569776e20480558c1cdeb2d06c50bc" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e569776e20480558c1cdeb2d06c50bc" title="四、关系代数"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">四、关系代数</span></span></h2><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e569776e2048008ba04e15e99a6b1d2" data-id="2e569776e2048008ba04e15e99a6b1d2"><span><div id="2e569776e2048008ba04e15e99a6b1d2" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e569776e2048008ba04e15e99a6b1d2" title=" —— 选择 (Select)"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"> —— 选择 (Select)</span></span></h3><ul class="notion-list notion-list-disc notion-block-2e569776e20480b899f8cc06637fc1ce"><li>取行</li></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480e18eefd48264086087"><li>对应WHERE</li></ul><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e569776e204800888ade59912b2c9cb"><div style="position:relative;display:flex;justify-content:center;align-self:start;width:398.984375px;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A68d2ed6c-41f3-4f96-a8c9-f809348c4180%3Aimage.png?table=block&amp;id=2e569776-e204-8008-88ad-e59912b2c9cb&amp;t=2e569776-e204-8008-88ad-e59912b2c9cb" alt="notion image" loading="lazy" decoding="async"/></div></figure><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e569776e204809aa135df89378decba" data-id="2e569776e204809aa135df89378decba"><span><div id="2e569776e204809aa135df89378decba" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e569776e204809aa135df89378decba" title="π —— 投影 (Projection)"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">π —— 投影 (Projection)</span></span></h3><ul class="notion-list notion-list-disc notion-block-2e569776e2048023acc8f664ee41ee2d"><li>取列</li></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480c89048d15386c7ecd0"><li>对应SELECT</li></ul><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e569776e20480188770fc3d316803f3"><div style="position:relative;display:flex;justify-content:center;align-self:start;width:283.97918701171875px;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A4233b56c-9f10-4dbc-8191-74f992e96b8f%3Aimage.png?table=block&amp;id=2e569776-e204-8018-8770-fc3d316803f3&amp;t=2e569776-e204-8018-8770-fc3d316803f3" alt="notion image" loading="lazy" decoding="async"/></div></figure><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e569776e20480d686ded6ddbe8cd565" data-id="2e569776e20480d686ded6ddbe8cd565"><span><div id="2e569776e20480d686ded6ddbe8cd565" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e569776e20480d686ded6ddbe8cd565" title=" ——连接（Join）"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"> ——连接（Join）</span></span></h3><ul class="notion-list notion-list-disc notion-block-2e569776e204809a8cc8c9807d07c195"><li></li><ul class="notion-list notion-list-disc notion-block-2e569776e204809a8cc8c9807d07c195"><li>即原本<!-- -->条关系中，真实的n条一一对应的关系</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480618a0dd8a8cf86b272"><li></li><ul class="notion-list notion-list-disc notion-block-2e569776e20480618a0dd8a8cf86b272"><li>先乱交，再筛选</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e20480e8abf8ec70fad88739"><li>θ 连接</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480e8abf8ec70fad88739"><li>只要你的配对条件里用的是 <code class="notion-inline-code">&gt;</code>、<code class="notion-inline-code">&lt;</code>、<code class="notion-inline-code">&gt;=</code>、<code class="notion-inline-code">&lt;=</code>、<code class="notion-inline-code">!=</code> 这些玩意儿，它都叫 Theta 连接。</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e204802cab1ac26e3eca2178"><li>等值连接</li><ul class="notion-list notion-list-disc notion-block-2e569776e204802cab1ac26e3eca2178"><li>条件里<b>必须</b>且<b>只能</b>是等号（=）。</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e2048042a820d026c28247c0"><li>自然连接</li><ul class="notion-list notion-list-disc notion-block-2e569776e2048042a820d026c28247c0"><li>两表中的相同属性对应的值也相等的条目会自动连接</li><li>本质上是内连接</li><li>你来个 <code class="notion-inline-code">NATURAL JOIN</code>，数据库会默默在后台给你加上：<code class="notion-inline-code">WHERE A.id = B.id AND A.name = B.name AND A.status = B.status</code>。两表属性连一起且自动去重</li><li>省略join-condition的join成为自然连接</li><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e569776e2048056b5a9c52af4f2c9a3"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:370.921875px;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A4d7d5ec1-1310-494a-9043-313359353c71%3Aimage.png?table=block&amp;id=2e569776-e204-8056-b5a9-c52af4f2c9a3&amp;t=2e569776-e204-8056-b5a9-c52af4f2c9a3" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e569776e204804a9f5fe2ff12cb9ef6"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:370.984375px;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A2180ee06-3a27-4564-9cf2-3263ecef3de2%3Aimage.png?table=block&amp;id=2e569776-e204-804a-9f5f-e2ff12cb9ef6&amp;t=2e569776-e204-804a-9f5f-e2ff12cb9ef6" alt="notion image" loading="lazy" decoding="async"/></div></figure></ul></ul><ul class="notion-list notion-list-disc notion-block-2e569776e2048055b770e72cba2059f8"><li>外连接</li><ul class="notion-list notion-list-disc notion-block-2e569776e2048055b770e72cba2059f8"><li>左右连接</li><li>全连接</li><ul class="notion-list notion-list-disc notion-block-2e569776e20480798d89e0c792bb4d81"><li>同时保留左右表的全部数据</li><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e569776e20480be9abef5dcad1ac404"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Af537056e-feee-4877-a1aa-6ec1077d5264%3Aimage.png?table=block&amp;id=2e569776-e204-80be-9abe-f5dcad1ac404&amp;t=2e569776-e204-80be-9abe-f5dcad1ac404" alt="notion image" loading="lazy" decoding="async"/></div></figure></ul></ul></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e569776e20480e087ddda0633828523" data-id="2e569776e20480e087ddda0633828523"><span><div id="2e569776e20480e087ddda0633828523" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e569776e20480e087ddda0633828523" title="除法"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">除法</span></span></h3><ul class="notion-list notion-list-disc notion-block-2e569776e20480ea8c6fead3a0431211"><li><b>核心逻辑：</b> 给你两张表 A 和 B，查出 A 表里哪些货色<b>“完全涵盖了”</b> B 表里的所有要求。</li></ul><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e569776e20480549ca3fcdcb255a8ae"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:398.984375px;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A7ba481ba-940a-4498-b3c9-f553c8beb849%3Aimage.png?table=block&amp;id=2e569776-e204-8054-9ca3-fcdcb255a8ae&amp;t=2e569776-e204-8054-9ca3-fcdcb255a8ae" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e569776e204804ba83de2e7c6b71e81"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A08e62d9d-5f6a-4bc9-b52d-e8104e127eab%3Aimage.png?table=block&amp;id=2e569776-e204-804b-a83d-e2e7c6b71e81&amp;t=2e569776-e204-804b-a83d-e2e7c6b71e81" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e569776e204801b9843e75822a80ed7"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Ac4db4881-d56e-4247-aca6-cf5c68e54210%3Aimage.png?table=block&amp;id=2e569776-e204-801b-9843-e75822a80ed7&amp;t=2e569776-e204-801b-9843-e75822a80ed7" alt="notion image" loading="lazy" decoding="async"/></div></figure><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-2e569776e20480388ff6e92e9f1a2a74" data-id="2e569776e20480388ff6e92e9f1a2a74"><span><div id="2e569776e20480388ff6e92e9f1a2a74" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e569776e20480388ff6e92e9f1a2a74" title="五、关系型数据库理论"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">五、关系型数据库理论</span></span></h2><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e669776e204803c8602dda6afc95b59" data-id="2e669776e204803c8602dda6afc95b59"><span><div id="2e669776e204803c8602dda6afc95b59" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e669776e204803c8602dda6afc95b59" title="确定性"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">确定性</span></span></h3><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e669776e2048059ae50f12446025af2" data-id="2e669776e2048059ae50f12446025af2"><span><div id="2e669776e2048059ae50f12446025af2" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e669776e2048059ae50f12446025af2" title="三大症状"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">三大症状</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e669776e2048058bf36c8d2b1f1fe6b"><li>更新异常：漏</li></ul><ul class="notion-list notion-list-disc notion-block-2e669776e20480b59f38ef6ef575bc12"><li>删除异常：删多了</li></ul><ul class="notion-list notion-list-disc notion-block-2e669776e20480de9c46d39985054abd"><li>插入异常：没主键</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e669776e204808ea6daf955abcf7254" data-id="2e669776e204808ea6daf955abcf7254"><span><div id="2e669776e204808ea6daf955abcf7254" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e669776e204808ea6daf955abcf7254" title="决定因素"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">决定因素</span></span></h4><div class="notion-text notion-block-2e669776e204802d852ffb07696d84b6">如果属性 X 的每一个值，都对应属性 Y 里的唯一一个值，那 X 就叫 <b>Determinant（决定因素）</b>。</div><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e669776e2048005874dcf99806dff51" data-id="2e669776e2048005874dcf99806dff51"><span><div id="2e669776e2048005874dcf99806dff51" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e669776e2048005874dcf99806dff51" title="函数依赖"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">函数依赖</span></span></h4><div class="notion-text notion-block-2e669776e20480059627fe79c3a0f0c4"><b>FD 的本质：</b> <!-- -->。也就是 Y <b>函数依赖于</b> X。</div><ul class="notion-list notion-list-disc notion-block-2e669776e204804ea805d270200e8de4"><li>自反律：你包含我，那我肯定能确定你</li></ul><ul class="notion-list notion-list-disc notion-block-2e669776e2048027bfdac4f90a1403ad"><li>扩充率：<!-- -->，那给两边都加个 Z，逻辑依然成立。</li></ul><ul class="notion-list notion-list-disc notion-block-2e669776e204803b9c55dcafd6da296a"><li>传递率：<!-- --> 且 <!-- -->，那 A 就能跨过 B 直接搞定 C</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e669776e20480478acbf618647c573a" data-id="2e669776e20480478acbf618647c573a"><span><div id="2e669776e20480478acbf618647c573a" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e669776e20480478acbf618647c573a" title="闭包"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">闭包</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e669776e20480a7a96cced7f92abf68"><li><b>闭包（Closure）：</b> 给你一个属性集 X，让你去推导在这个表里，X 到底能间接确定多少个属性。推出来的那个大集合就叫 <!-- -->。</li><ul class="notion-list notion-list-disc notion-block-2e669776e20480a7a96cced7f92abf68"><li>搜索</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e669776e20480569c0feb9374a750fb"><li><b>超级键（Superkey）：</b> 只要你的闭包能覆盖整张表的所有属性，你就是超级键。</li></ul><ul class="notion-list notion-list-disc notion-block-2e669776e20480aa91a0e9894f9c2bf3"><li><b>候选键（Candidate Key）：</b> 如果你是超级键，而且你还没法再精简了（去掉任何一个字段就罩不住全场了），那你就是候选键。</li></ul><div class="notion-text notion-block-2e669776e20480bc82ead23dcb0f5dc7">可以用图解法</div><ul class="notion-list notion-list-disc notion-block-2e669776e20480e6ad49f17c0ebfd3ba"><li>起点：<!-- -->，只有向外的箭头</li></ul><ul class="notion-list notion-list-disc notion-block-2e669776e204801fa8e3f7f9dfff750d"><li>终点：<!-- -->，只有箭头指向它</li></ul><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e669776e2048075bd17fa88eb3fdad5"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:398.953125px;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A294a457e-baef-4288-ad80-8128ceb98eca%3Aimage.png?table=block&amp;id=2e669776-e204-8075-bd17-fa88eb3fdad5&amp;t=2e669776-e204-8075-bd17-fa88eb3fdad5" alt="notion image" loading="lazy" decoding="async"/></div></figure><ul class="notion-list notion-list-disc notion-block-2e669776e20480ae9a68ed93b71a9377"><li>任何候选键中必须含有<!-- -->的所有项，但不能有<!-- -->的任何项</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e669776e2048093b049c548034308a1" data-id="2e669776e2048093b049c548034308a1"><span><div id="2e669776e2048093b049c548034308a1" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e669776e2048093b049c548034308a1" title="关系分解"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">关系分解</span></span></h4><div class="notion-text notion-block-2e669776e20480b0a026c5e48c82e0a0">为了解决三大症状，将数据库拆小</div><div class="notion-text notion-block-2e669776e20480ffbcc1c913c596e991"><b>1. Lossless Join Decomposition (LLJD) —— “无损连接分解”</b></div><div class="notion-text notion-block-2e669776e20480f98aead749048f9404">它的意思是：<b>你把表拆开后再连回去（Join），结果必须跟原来一模一样。</b></div><ul class="notion-list notion-list-disc notion-block-2e669776e20480639272fceafbc09ff7"><li>数据库里的无损是指“没产生幻觉”。如果拆得不对，拼回来的时候会多出一些原本不存在的、莫名其妙的“幽灵行”（Wrong tuples）。这就叫有损。</li><ul class="notion-list notion-list-disc notion-block-2e669776e20480639272fceafbc09ff7"><li>Join的共同属性不能是多对多，需要是某个表的<b>候选键</b>。</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e669776e2048048b4b0d9e0c19b83ef"><li><b>判别绝招（Theorem 5.1.3.1）：</b>
如果你把一张表拆成 <!-- -->和 <!-- -->，只要满足：</li><ul class="notion-list notion-list-disc notion-block-2e669776e2048048b4b0d9e0c19b83ef"><li>（公共属性是 <!-- -->的键）</li><li><b>或者</b> <!-- --> （公共属性是 <!-- --> 的键）</li><li>只要满足其中一个，你这次拆迁就是“无损”的。</li><ul class="notion-list notion-list-disc notion-block-2e669776e20480608125e743f7481be2"><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e669776e20480a68d84ece334f85d0f"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A62498a2c-06c7-4919-af05-9a72e12f2fef%3Aimage.png?table=block&amp;id=2e669776-e204-80a6-8d84-ece334f85d0f&amp;t=2e669776-e204-80a6-8d84-ece334f85d0f" alt="notion image" loading="lazy" decoding="async"/></div></figure></ul></ul></ul><div class="notion-text notion-block-2e669776e2048057894dd4ef8ca19da1"><b>2. Dependency Preserving Decomposition (DPD) —— “保持依赖分解”</b></div><div class="notion-text notion-block-2e669776e20480449caae31f1fb1f8e6">这个是说：<b>原来的那些规矩（函数依赖 FD），在拆完之后，是不是还能在这些小表里找得着？</b></div><ul class="notion-list notion-list-disc notion-block-2e669776e2048075b7cfdf213ad8f73b"><li><b>后果：</b> 如果没保持依赖，你以后往表里塞数据的时候，数据库就没法自动帮你检查逻辑错误了。这简直就是给未来的程序 Bug 留了个后门。</li></ul><ul class="notion-list notion-list-disc notion-block-2e669776e204809389fcdd95c18aa109"><li><b>算法：</b> <b>XYGP 算法</b>。就是让你在拆开的小表里反复求闭包，看看能不能把原表所有的 FD 都给推导出来。</li><ul class="notion-list notion-list-disc notion-block-2e669776e204809389fcdd95c18aa109"><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e669776e20480e6a761d3a312b1f09f"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A048e9a01-a122-4816-8110-369139ef57f0%3Aimage.png?table=block&amp;id=2e669776-e204-80e6-a761-d3a312b1f09f&amp;t=2e669776-e204-80e6-a761-d3a312b1f09f" alt="notion image" loading="lazy" decoding="async"/></div></figure></ul></ul><div class="notion-blank notion-block-2e669776e20480ce9fcafa24a1c96b93"> </div><ol start="1" class="notion-list notion-list-numbered notion-block-2e669776e20480be8f99ce28e085cccd" style="list-style-type:decimal"><li><b>拆表是为了治病</b>（去冗余）。</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-2e669776e20480318d7cee3836f5d92b" style="list-style-type:decimal"><li><b>无损连接（LLJD）是底线</b>。如果你拆完连不回去了，那你就直接回炉重造吧，别tm当程序员了。</li></ol><ol start="3" class="notion-list notion-list-numbered notion-block-2e669776e20480e18faaf36fe34c3cef" style="list-style-type:decimal"><li><b>保持依赖（DPD）是理想</b>。有些时候（比如为了达到 BCNF），我们不得不牺牲一点点依赖保持，这是权衡。</li></ol><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e669776e2048033b993e4a101b923db" data-id="2e669776e2048033b993e4a101b923db"><span><div id="2e669776e2048033b993e4a101b923db" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e669776e2048033b993e4a101b923db" title="范式"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">范式</span></span></h3><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e669776e2048050a4a1dcc7c8c19327" data-id="2e669776e2048050a4a1dcc7c8c19327"><span><div id="2e669776e2048050a4a1dcc7c8c19327" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e669776e2048050a4a1dcc7c8c19327" title="1NF"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">1NF</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e669776e20480ff8c2bf9ec6047e945"><li>表中的每一个格子（属性），都必须是<b>原子性 (Atomic)</b> 的</li></ul><ul class="notion-list notion-list-disc notion-block-2e669776e2048072937df024a83acd4f"><li>一个格子里只能放<b>一个</b>东西</li></ul><ul class="notion-list notion-list-disc notion-block-2e669776e2048052a525de15c4677e07"><li><b>非主键必须依赖主键</b></li></ul><div class="notion-text notion-block-2e669776e204808990f8cb30cef092a1">总结：多拆，尽量不要一对多</div><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e669776e204808e959dc0453ad2d40e" data-id="2e669776e204808e959dc0453ad2d40e"><span><div id="2e669776e204808e959dc0453ad2d40e" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e669776e204808e959dc0453ad2d40e" title="2NF"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">2NF</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e669776e204802d986df97442f575e7"><li>只有组合主键才要考虑是不是2NF，如果主键就一列，一定是2NF</li></ul><ul class="notion-list notion-list-disc notion-block-2e669776e2048067840bcacd8d2afa7e"><li>其他属性必须同时依赖主键的所有属性</li><ul class="notion-list notion-list-disc notion-block-2e669776e2048067840bcacd8d2afa7e"><li>假设你建了一张<b>选课成绩表</b>，主键是 <code class="notion-inline-code"><b>(学号, 课程号)</b></code>。</li><li><code class="notion-inline-code">成绩</code> 同时依赖 <code class="notion-inline-code">(学号, 课程号)</code>。这没问题</li><li><code class="notion-inline-code">学生姓名</code> 只依赖主键的一半 <code class="notion-inline-code">(学号)</code>，把另一半 <code class="notion-inline-code">(课程号)</code> 当空气。这不行。</li><li>所以不符合2NF</li></ul></ul><div class="notion-text notion-block-2e669776e20480e69fd5e9f6a089c2da">如果不符合2NF，会出现数据冗余。</div><div class="notion-text notion-block-2e669776e204800c91f3cf50ed9ab83e">解决：</div><ul class="notion-list notion-list-disc notion-block-2e669776e20480839048cae1b11410a1"><li>将只依赖部分主键属性的分出去。</li></ul><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e669776e20480f4894dee3055354a76"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:398.96875px;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Ac86acb38-a8ab-4234-b579-dd430e478bef%3Aimage.png?table=block&amp;id=2e669776-e204-80f4-894d-ee3055354a76&amp;t=2e669776-e204-80f4-894d-ee3055354a76" alt="notion image" loading="lazy" decoding="async"/></div></figure><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e669776e20480f2a387f27680049cf9" data-id="2e669776e20480f2a387f27680049cf9"><span><div id="2e669776e20480f2a387f27680049cf9" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e669776e20480f2a387f27680049cf9" title="3NF"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">3NF</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e669776e20480129602d2ba83eb181d"><li>只有两个属性的一定满足</li></ul><ul class="notion-list notion-list-disc notion-block-2e669776e2048055b0c2c781a2e98420"><li>非主键之间也存在依赖，造成传递依赖</li></ul><div class="notion-text notion-block-2e669776e2048044a4b3e9d0f6ad8a68">解决：</div><ul class="notion-list notion-list-disc notion-block-2e669776e204804b965bfde88b4df2ac"><li>将非主键的依赖分成小表</li></ul><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e669776e20480f2ae4bfdf906de3eef"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A8fed8c24-7620-40cd-97b9-da674ecbe577%3Aimage.png?table=block&amp;id=2e669776-e204-80f2-ae4b-fdf906de3eef&amp;t=2e669776-e204-80f2-ae4b-fdf906de3eef" alt="notion image" loading="lazy" decoding="async"/></div></figure><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e669776e2048054a794fedebdb98c0c" data-id="2e669776e2048054a794fedebdb98c0c"><span><div id="2e669776e2048054a794fedebdb98c0c" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e669776e2048054a794fedebdb98c0c" title="BCNF"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">BCNF</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e669776e20480b8aac1d71f18ba98b8"><li>所有的决定者都必须是候选键</li></ul><div class="notion-text notion-block-2e669776e20480eb8fc9dcafbb97b13d"><b>3NF 和 BCNF 的区别：</b></div><ul class="notion-list notion-list-disc notion-block-2e669776e2048017931bc282042e04b2"><li><b>3NF</b>：非主键的小弟不能互相搞基（传递依赖）。</li></ul><ul class="notion-list notion-list-disc notion-block-2e669776e2048004a360df9569f2d897"><li><b>BCNF</b>：<b>哪怕你是主键的一部分，只要你敢决定别人，你就必须是候选键！</b> </li></ul><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e669776e20480efaa15e1fab20e8817"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A59d3d473-5136-41bc-8bf4-4b3d15432605%3Aimage.png?table=block&amp;id=2e669776-e204-80ef-aa15-e1fab20e8817&amp;t=2e669776-e204-80ef-aa15-e1fab20e8817" alt="notion image" loading="lazy" decoding="async"/></div></figure><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e669776e20480a895e5d9da7eb3c129" data-id="2e669776e20480a895e5d9da7eb3c129"><span><div id="2e669776e20480a895e5d9da7eb3c129" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e669776e20480a895e5d9da7eb3c129" title="算法"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">算法</span></span></h3><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e669776e2048041aec6dc0ed04defcd" data-id="2e669776e2048041aec6dc0ed04defcd"><span><div id="2e669776e2048041aec6dc0ed04defcd" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e669776e2048041aec6dc0ed04defcd" title="LLJD-BCNF（保持无损连接的 BCNF 分解）"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>LLJD-BCNF（保持无损连接的 BCNF 分解）</b></span></span></h4><div class="notion-text notion-block-2e669776e20480cb8749d8f84f5f11e2">它的Input和Output写得装模作样的，其实就干一件事：<b>暴力拆迁</b>。</div><ul class="notion-list notion-list-disc notion-block-2e669776e20480f1a800faf2781e1971"><li><b>Input（原料）：</b> 一张可能充满病毒（非 BCNF）的大表 R，和一堆规则 F。</li></ul><ul class="notion-list notion-list-disc notion-block-2e669776e20480c28a4cc4d9d9ba6787"><li><b>Process（屠宰）：</b></li><ul class="notion-list notion-list-disc notion-block-2e669776e20480c28a4cc4d9d9ba6787"><ol start="1" class="notion-list notion-list-numbered notion-block-2e669776e204804ea2b8d51bd56b911e" style="list-style-type:decimal"><li>检查这张表是不是 BCNF？</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-2e669776e20480d986bcfca04acfcb9d" style="list-style-type:decimal"><li>如果是，收工。</li></ol><ol start="3" class="notion-list notion-list-numbered notion-block-2e669776e20480d8aa8be88a2fdb06c1" style="list-style-type:decimal"><li>如果不是（比如发现 X→Y 这个二五仔），那就一刀切下去！把表拆成两半。</li></ol><ol start="4" class="notion-list notion-list-numbered notion-block-2e669776e20480fda37ec07a1b6c5bf2" style="list-style-type:decimal"><li>对拆出来的两半，<b>重复步骤 1</b>。直到所有的表都干净为止。</li></ol></ul></ul><ul class="notion-list notion-list-disc notion-block-2e669776e2048000978efceabcf6362d"><li><b>Output（成品）：</b> 一堆小表 D，每一张都必须是 <b>BCNF</b>，而且拼起来还能还原。</li></ul><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e669776e2048066af8ee6220a223284"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Aebd8606b-bc12-4861-aa81-687c6e47b506%3Aimage.png?table=block&amp;id=2e669776-e204-8066-af8e-e6220a223284&amp;t=2e669776-e204-8066-af8e-e6220a223284" alt="notion image" loading="lazy" decoding="async"/></div></figure><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e669776e204807583fcebb23a0b7238" data-id="2e669776e204807583fcebb23a0b7238"><span><div id="2e669776e204807583fcebb23a0b7238" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e669776e204807583fcebb23a0b7238" title="Cover覆盖"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">Cover覆盖</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e669776e20480278420e08376c8e28a"><li>F 说：<code class="notion-inline-code">只要你有钱，就能买车；只要有车，就能载妹。</code></li></ul><ul class="notion-list notion-list-disc notion-block-2e669776e204806abe9ceca812cdb187"><li>G 说：<code class="notion-inline-code">只要你有钱，就能载妹。</code></li></ul><ul class="notion-list notion-list-disc notion-block-2e669776e2048070b737d1a116b37868"><li>虽然 F 没直接说有钱能载妹，但通过 F 的逻辑能推导出 G。所以 F 覆盖了 G。</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e669776e2048069b429c690e65e2fba" data-id="2e669776e2048069b429c690e65e2fba"><span><div id="2e669776e2048069b429c690e65e2fba" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e669776e2048069b429c690e65e2fba" title="等价"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">等价</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e669776e20480ba8457ed46710d519d"><li>F 能推导出 G 的所有屁话。</li></ul><ul class="notion-list notion-list-disc notion-block-2e669776e20480ffa74fe216f73247ce"><li>G 也能推导出 F 的所有废话。</li></ul><ul class="notion-list notion-list-disc notion-block-2e669776e2048077bf9ef16f36305a46"><li>FG等价</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e669776e20480dab538c0d872235a71" data-id="2e669776e20480dab538c0d872235a71"><span><div id="2e669776e20480dab538c0d872235a71" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e669776e20480dab538c0d872235a71" title="最小覆盖"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">最小覆盖</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e669776e2048064a9b6c2e7aeaf6d29"><li>能够覆盖原fd</li></ul><ul class="notion-list notion-list-disc notion-block-2e669776e204804ba099d139994d4111"><li>fd一一对应</li></ul><ul class="notion-list notion-list-disc notion-block-2e669776e20480e5a026f1e82effdcb6"><li>不要有多余的语句</li><ul class="notion-list notion-list-disc notion-block-2e669776e20480e5a026f1e82effdcb6"><li>如果一条规矩删了，剩下的规矩还是能推导出它，那这条规矩就是<b>多余的废话。</b></li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e669776e20480ccb80ad6d02c4e6489"><li><b>左边不准带拖油瓶 (No extraneous)：</b> 如果 AB→C，而老子发现光靠 A→C 就够了，那 B 就没用。</li></ul><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e669776e20480d6b216ce0537b0972b"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A8abf51bd-ad53-44de-b202-a26f21d383c6%3Aimage.png?table=block&amp;id=2e669776-e204-80d6-b216-ce0537b0972b&amp;t=2e669776-e204-80d6-b216-ce0537b0972b" alt="notion image" loading="lazy" decoding="async"/></div></figure><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e669776e204802eb49be72609c3cf38" data-id="2e669776e204802eb49be72609c3cf38"><span><div id="2e669776e204802eb49be72609c3cf38" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e669776e204802eb49be72609c3cf38" title="LLJD-DPD-3NF"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>LLJD-DPD-3NF</b></span></span></h4><ul class="notion-list notion-list-disc notion-block-2e669776e2048056996ae373e7c89003"><li><b>LLJD (无损连接)：</b> 拼回去的时候别给我多出一块肉，也别少个腰子。</li></ul><ul class="notion-list notion-list-disc notion-block-2e669776e20480ffb065c47aceaea84a"><li><b>DPD (依赖保持)：</b> 以前的帮规（函数依赖）在新表里还得管用，别拆完之后没人守规矩了。</li></ul><ul class="notion-list notion-list-disc notion-block-2e669776e20480c3abe2e137a4a7a878"><li><b>3NF (第三范式)：</b> 别有那些小弟勾搭小弟的破事（传递依赖）。</li></ul><ol start="1" class="notion-list notion-list-numbered notion-block-2e669776e2048008a638e7f53a846623" style="list-style-type:decimal"><li><b>去废话：</b> 先把函数依赖变成 </li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-2e669776e20480ac9c5ff3f2d7920e83" style="list-style-type:decimal"><li><b>拉小圈子：</b> 对于 Fmin 里的每一条规矩 X→Y，直接建一张新表，把 X 和 Y 关在一起。</li></ol><ol start="3" class="notion-list notion-list-numbered notion-block-2e669776e20480d1a1abfccce55be0da" style="list-style-type:decimal"><li><b>找老大：</b> 看看这些新表里，有没有哪张表包含了原表的老大（候选键 CK）？</li><ol class="notion-list notion-list-numbered notion-block-2e669776e20480d1a1abfccce55be0da" style="list-style-type:lower-alpha"><li>如果有，恭喜，你已经完事了。</li><li>如果没有，<b>硬塞一个！</b> 专门建一张只包含候选键的表。</li></ol></ol><ol start="4" class="notion-list notion-list-numbered notion-block-2e669776e20480cb94bdc92f1e27871e" style="list-style-type:decimal"><li><b>删多余：</b> 如果一张小表是另一张表的“子集”，直接把它踢了。</li></ol><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-2e669776e204804c9c5aef1686183085" data-id="2e669776e204804c9c5aef1686183085"><span><div id="2e669776e204804c9c5aef1686183085" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e669776e204804c9c5aef1686183085" title="ER图"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">ER图</span></span></h2><ul class="notion-list notion-list-disc notion-block-2e669776e204802d8eb2dcaf2657868f"><li><b>矩形（方块）：</b> 实体（Entity）。就是表里的<b>“主角”</b></li></ul><ul class="notion-list notion-list-disc notion-block-2e669776e204807e9c09c642d46bff99"><li><b>椭圆（圆圈）：</b> 属性（Attribute）。就是主角身上的<b>“零件”</b></li></ul><ul class="notion-list notion-list-disc notion-block-2e669776e204805193afdb04d18d800a"><li><b>菱形：</b> 关系（Relationship）。就是主角们是怎么<b>“勾搭”</b>在一起的</li></ul><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e669776e20480e7a90dd9464429c10c"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A23c597ca-0cc8-4c5b-84aa-6121e09aee4d%3Aimage.png?table=block&amp;id=2e669776-e204-80e7-a90d-d9464429c10c&amp;t=2e669776-e204-80e7-a90d-d9464429c10c" alt="notion image" loading="lazy" decoding="async"/></div></figure><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e669776e20480d297fac778a336ec3b" data-id="2e669776e20480d297fac778a336ec3b"><span><div id="2e669776e20480d297fac778a336ec3b" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e669776e20480d297fac778a336ec3b" title="多对多/三元关系"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">多对多/三元关系</span></span></h3><ul class="notion-list notion-list-disc notion-block-2e669776e2048025b183ed74b0c43193"><li>要R表中转，其他的分别建表</li></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e669776e204805e84b8f5e9859ddece" data-id="2e669776e204805e84b8f5e9859ddece"><span><div id="2e669776e204805e84b8f5e9859ddece" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e669776e204805e84b8f5e9859ddece" title="二元关系"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">二元关系</span></span></h3><ul class="notion-list notion-list-disc notion-block-2e669776e20480e7b4d7fd7bf3b78a33"><li>方块 E 和方块 F 通过 R 勾搭。</li></ul><ul class="notion-list notion-list-disc notion-block-2e669776e2048059a153fb6bcf3cecaa"><li>x 和 y 是基数（Cardinality），表示一个人能勾搭几个。</li></ul><ol start="1" class="notion-list notion-list-numbered notion-block-2e669776e20480ae9599dd465ef3fddb" style="list-style-type:decimal"><li><b>一对多：</b> 众多的“小弟”兜里揣着“大哥”的身份证<b>（1端主键做n端外键）</b>。</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-2e669776e20480f19b0af96954990084" style="list-style-type:decimal"><li><b>一对一：</b> 别让表里出现太多空位（NULL），谁必须参与就让谁带着对方的 ID。</li></ol><ol start="3" class="notion-list notion-list-numbered notion-block-2e669776e20480fc865bcfd6c9b881f0" style="list-style-type:decimal"><li><b>多对多：</b> 别瞎折腾，直接建个“案发现场”表（中介表），把双方的 ID 都拽过来。</li></ol><div class="notion-blank notion-block-2e669776e204800ab8ddd404faa6e3de"> </div><div class="notion-blank notion-block-2e669776e204804a8cd9ca62fa1bf682"> </div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-2e669776e204809b9645ceec024e62f1" data-id="2e669776e204809b9645ceec024e62f1"><span><div id="2e669776e204809b9645ceec024e62f1" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e669776e204809b9645ceec024e62f1" title="孩子复习不完了……就这样吧"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">孩子复习不完了……就这样吧</span></span></h2><div class="notion-blank notion-block-2e669776e2048082921dc45c10cbddf6"> </div></div><div class="notion-callout notion-gray_background_co notion-block-2e669776e20480c2af77ced5b3ff3b91"><div class="notion-page-icon-inline notion-page-icon-span"><span class="notion-page-icon" role="img" aria-label="😀">😀</span></div><div class="notion-callout-text">这里写文章的前言：
一个简单的开头,简述这篇文章讨论的问题、目标、人物、背景是什么？并简述你给出的答案。<div class="notion-text notion-block-2e669776e204806eba7bd1285b27955c">可以说说你的故事：阻碍、努力、结果成果，意外与转折。</div></div></div><div class="notion-blank notion-block-2e669776e2048099ba95e56ea870bdfe"> </div></main></div>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[计算机系统3笔记]]></title>
            <link>http://richardliuda.top/article/CS3</link>
            <guid>http://richardliuda.top/article/CS3</guid>
            <pubDate>Sun, 11 Jan 2026 00:00:00 GMT</pubDate>
            <content:encoded><![CDATA[<div id="notion-article" class="mx-auto overflow-hidden "><main class="notion light-mode notion-page notion-block-2e469776e20480ea9494ef7b73038ea7"><div class="notion-viewport"></div><div class="notion-collection-page-properties"></div><div class="notion-callout notion-gray_background_co notion-block-2e469776e20480769edddbdc7e8f865f"><div class="notion-page-icon-inline notion-page-icon-span"><span class="notion-page-icon" role="img" aria-label="⚙">⚙</span></div><div class="notion-callout-text">Fork from Hytidel.
Show respect!</div></div><div class="notion-sync-block notion-block-2e469776e2048035865ae54486d0b7df"><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-2da69776e20480b08ec8f93c6ca0610c" data-id="2da69776e20480b08ec8f93c6ca0610c"><span><div id="2da69776e20480b08ec8f93c6ca0610c" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2da69776e20480b08ec8f93c6ca0610c" title="第一章概要"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">第一章概要</span></span></h2><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2da69776e20480ffa79ed7110b2e2717" data-id="2da69776e20480ffa79ed7110b2e2717"><span><div id="2da69776e20480ffa79ed7110b2e2717" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2da69776e20480ffa79ed7110b2e2717" title="概念"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">概念</span></span></h3><ul class="notion-list notion-list-disc notion-block-2e269776e20480529a2ff912c23596b2"><li>性能衡量</li><ul class="notion-list notion-list-disc notion-block-2e269776e20480529a2ff912c23596b2"><li>响应时间：计算机完成某任务的时间</li><li>吞吐率：单位时间内完成的任务数</li><li>性能： 1 / 执行时间</li></ul></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2da69776e204804297bbcd990800b17f" data-id="2da69776e204804297bbcd990800b17f"><span><div id="2da69776e204804297bbcd990800b17f" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2da69776e204804297bbcd990800b17f" title="计算"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">计算</span></span></h3><ul class="notion-list notion-list-disc notion-block-2e269776e204803ba343ef3ad652b287"><li>CPU时间</li><ul class="notion-list notion-list-disc notion-block-2e269776e204803ba343ef3ad652b287"><div class="notion-text notion-block-2e269776e20480e1a180ff4f8426153f">=CPU时钟周期数×时钟周期时间</div><div class="notion-text notion-block-2e269776e20480fdb1b1c235b93b5551"><b>=CPU时钟周期数/时钟频率</b></div><div class="notion-text notion-block-2e269776e2048085841ee3a205e51b1c"><b>=指令数 </b>×<b> CPI </b>×<b> 时钟周期时间</b></div></ul></ul><div class="notion-callout notion-gray_background_co notion-block-2e269776e204809aadccd80405611047"><div class="notion-page-icon-inline notion-page-icon-span"><span class="notion-page-icon" role="img" aria-label="💡">💡</span></div><div class="notion-callout-text"><div class="notion-text notion-block-2e269776e20480e883b1fa9febf86d96"> <b>CPI（每条指令的时钟周期数）：由CPU硬件决定</b></div></div></div><ul class="notion-list notion-list-disc notion-block-2e269776e204807d9871cc4c6709434d"><li>加权平均CPI——综合效率指标</li><ul class="notion-list notion-list-disc notion-block-2e269776e204807d9871cc4c6709434d"></ul></ul><ul class="notion-list notion-list-disc notion-block-2e269776e204808a9851e7f899ae2842"><li>功耗</li></ul><div class="notion-blank notion-block-2e269776e20480f19012cb87b486740d"> </div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-2da69776e2048091883cd181e4bf42c6" data-id="2da69776e2048091883cd181e4bf42c6"><span><div id="2da69776e2048091883cd181e4bf42c6" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2da69776e2048091883cd181e4bf42c6" title="第二章MIPS汇编"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">第二章MIPS汇编</span></span></h2><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e269776e20480319a23cc3b15a30d3d" data-id="2e269776e20480319a23cc3b15a30d3d"><span><div id="2e269776e20480319a23cc3b15a30d3d" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e269776e20480319a23cc3b15a30d3d" title="四大基本原则（面向流水线的ISA设计）"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">四大基本原则（面向流水线的ISA设计）</span></span></h3><ul class="notion-list notion-list-disc notion-block-2e269776e204807ba0d9f1178b4d7e77"><li>简单源于规整 </li><ul class="notion-list notion-list-disc notion-block-2e269776e204807ba0d9f1178b4d7e77"><li>算术运算总是有三个操作数</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e269776e2048089bbf7c07a900af707"><li>越小越快</li><ul class="notion-list notion-list-disc notion-block-2e269776e2048089bbf7c07a900af707"><li>MIPS只有32个寄存器</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e269776e2048077b798c98c26409eff"><li>加速经常性事件</li><ul class="notion-list notion-list-disc notion-block-2e269776e2048077b798c98c26409eff"><li>立即数操作</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e269776e20480318514df1ede6d93d6"><li>优秀的设计需要折中</li><ul class="notion-list notion-list-disc notion-block-2e269776e20480318514df1ede6d93d6"><li>为了保持指令长度一致（32位），MIPS采用了多种指令格式</li></ul></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2da69776e20480379b86c88230bc4837" data-id="2da69776e20480379b86c88230bc4837"><span><div id="2da69776e20480379b86c88230bc4837" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2da69776e20480379b86c88230bc4837" title="寄存器约定"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">寄存器约定</span></span></h3><table class="notion-simple-table notion-block-2e269776e204809e8cf7f59b314c36b7"><tbody><tr class="notion-simple-table-row notion-simple-table-header-row notion-block-2e269776e20480afae9dc7f676960434"><td class="" style="width:120px"><div class="notion-simple-table-cell">寄存器名</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">寄存器编号</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">用途</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">是否调用者保存</div></td></tr><tr class="notion-simple-table-row notion-block-2e269776e20480daabbae3c65cfbc087"><td class="" style="width:120px"><div class="notion-simple-table-cell">$zero</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">常数 0</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">不可重写</div></td></tr><tr class="notion-simple-table-row notion-block-2e269776e20480bda1eac8b560954df2"><td class="" style="width:120px"><div class="notion-simple-table-cell">$v0, $v1</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">2, 3</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">返回值</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">否</div></td></tr><tr class="notion-simple-table-row notion-block-2e269776e2048028896bd090991334b5"><td class="" style="width:120px"><div class="notion-simple-table-cell">$a0 ~ $a3</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">4 ~ 7</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">函数传参的前 4 个参数</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">否</div></td></tr><tr class="notion-simple-table-row notion-block-2e269776e2048087b775ec132449ddf5"><td class="" style="width:120px"><div class="notion-simple-table-cell">$t0 ~ $t7</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">8 ~ 15</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">临时变量</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">否</div></td></tr><tr class="notion-simple-table-row notion-block-2e269776e2048043b3d0db49ff6fa7f4"><td class="" style="width:120px"><div class="notion-simple-table-cell">$s0 ~ $s7</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">16 ~ 23</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">保存参数</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">是</div></td></tr><tr class="notion-simple-table-row notion-block-2e269776e204809d8b49c6779e1b5b76"><td class="" style="width:120px"><div class="notion-simple-table-cell">$t8, $t9</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">24, 25</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">临时变量</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">否</div></td></tr><tr class="notion-simple-table-row notion-block-2e269776e20480228345e36b127e4843"><td class="" style="width:120px"><div class="notion-simple-table-cell">$gp</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">28</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">Global Pointer, 静态数据的全局指针</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">是</div></td></tr><tr class="notion-simple-table-row notion-block-2e269776e2048032a317f762f9b93a28"><td class="" style="width:120px"><div class="notion-simple-table-cell">$sp</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">29</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">Stack Pointer, 堆栈指针</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">是</div></td></tr><tr class="notion-simple-table-row notion-block-2e269776e204804d8665ed337338d5eb"><td class="" style="width:120px"><div class="notion-simple-table-cell">$fp</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">30</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">Frame Pointer, 帧指针, 保存过程帧的第一个字</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">是</div></td></tr><tr class="notion-simple-table-row notion-block-2e269776e2048005b13fda13195f9246"><td class="" style="width:120px"><div class="notion-simple-table-cell">$ra</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">31</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">Return Address, 返回地址</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">是</div></td></tr></tbody></table><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e269776e204801ba740f8dfc48771c7" data-id="2e269776e204801ba740f8dfc48771c7"><span><div id="2e269776e204801ba740f8dfc48771c7" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e269776e204801ba740f8dfc48771c7" title="汇编指令"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">汇编指令</span></span></h3><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e269776e20480eeb407c1c25d8f1ac1" data-id="2e269776e20480eeb407c1c25d8f1ac1"><span><div id="2e269776e20480eeb407c1c25d8f1ac1" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e269776e20480eeb407c1c25d8f1ac1" title="算术运算"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">算术运算</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e269776e204807aa2a1dd7de16e894c"><li>加减</li></ul><ul class="notion-list notion-list-disc notion-block-2e269776e20480cc8a12fe46b39a1553"><li>逻辑运算</li><ul class="notion-list notion-list-disc notion-block-2e269776e20480cc8a12fe46b39a1553"><li>and：按位与，将数据的某些位保留下来</li><li>or：按位或，将某些位强行变1</li><li>xor：按位异或</li><ul class="notion-list notion-list-disc notion-block-2e269776e20480da8d6ae2a603f6c6b1"><li>与1异或：取反</li><li>与自己异或：为0，可判断两数是否相等，也可以快速清零</li></ul><li>nor：按位或非，先or再取反</li><ul class="notion-list notion-list-disc notion-block-2e269776e20480c2b425e0709e54e4af"><li>实现单独的取反功能</li><ul class="notion-list notion-list-disc notion-block-2e269776e20480618834eafd9bdc2189"><div class="notion-text notion-block-2e269776e204809fa510dfb410b02645"><code class="notion-inline-code">nor $t0, $t1, $zero</code></div></ul></ul></ul></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e269776e20480b9b2c4e344b9f26c15" data-id="2e269776e20480b9b2c4e344b9f26c15"><span><div id="2e269776e20480b9b2c4e344b9f26c15" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e269776e20480b9b2c4e344b9f26c15" title="内存操作"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">内存操作</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e269776e20480548ecef2998d8b4f31"><li>读写</li><ul class="notion-list notion-list-disc notion-block-2e269776e20480548ecef2998d8b4f31"><li>lw/lb</li><ul class="notion-list notion-list-disc notion-block-2e269776e20480f7b901efd84b290de7"><li><code class="notion-inline-code">$rt</code> = <code class="notion-inline-code">$rs</code> 里的值 + <code class="notion-inline-code">offset</code>（偏移量）</li></ul><li>sw/sb</li><ul class="notion-list notion-list-disc notion-block-2e269776e2048006966ce4971d2b17bd"><li>把 <code class="notion-inline-code">$rt</code> 的值存到地址 <code class="notion-inline-code">$rs + offset</code> 的地方</li></ul></ul></ul><ul class="notion-list notion-list-disc notion-block-2e269776e20480be8e62c425ad262043"><li>寻址</li><ul class="notion-list notion-list-disc notion-block-2e269776e20480be8e62c425ad262043"><li>字节寻址：连续读数，地址+4字节</li><li>内存对齐：w必须是4的倍数，h必须是2的倍数</li><li>大端：高位字节低地址，从左往右写（MIPS默认）</li><li>小端：低位字节低地址，从右往左写，倒着来</li></ul></ul><div class="notion-callout notion-gray_background_co notion-block-2e269776e20480c48cc8f973ae98a2f8"><div class="notion-page-icon-inline notion-page-icon-span"><span class="notion-page-icon" role="img" aria-label="💡">💡</span></div><div class="notion-callout-text"><div class="notion-text notion-block-2e269776e20480e5ade3d0cfe23a9cea">寄存器的值要跟内存里的值进行操作时，需要先读内存加载到寄存器再操作</div></div></div><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e269776e204804b95d1f09bfe82d526" data-id="2e269776e204804b95d1f09bfe82d526"><span><div id="2e269776e204804b95d1f09bfe82d526" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e269776e204804b95d1f09bfe82d526" title="跳转指令"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">跳转指令</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e369776e2048013a59df9ff97f1dd0c"><li>beq：<code class="notion-inline-code">$rs</code>，<code class="notion-inline-code">$rt</code>相等跳转</li></ul><ul class="notion-list notion-list-disc notion-block-2e369776e2048033b03deada001d52a8"><li>bne：不相等跳转</li></ul><ul class="notion-list notion-list-disc notion-block-2e369776e204803bb371e63cfa008ac8"><li>j：无条件跳转</li><ul class="notion-list notion-list-disc notion-block-2e369776e204803bb371e63cfa008ac8"><li>target为26位，可在256MB范围内跳转</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e369776e204803b93a0ff874c9bcf3b"><li>jal：函数调用专用跳转</li><ul class="notion-list notion-list-disc notion-block-2e369776e204803b93a0ff874c9bcf3b"><li>将下一条指令的地址（pc+4）塞进<code class="notion-inline-code">$ra</code>中，方便跳回来</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e369776e20480269b34f7e1bd2941ff"><li>jr：R型指令，调到<code class="notion-inline-code">$rs</code>里面存的地址中</li></ul><div class="notion-callout notion-gray_background_co notion-block-2e369776e2048078b9dbcc5d463a926a"><div class="notion-page-icon-inline notion-page-icon-span"><span class="notion-page-icon" role="img" aria-label="💡">💡</span></div><div class="notion-callout-text"><div class="notion-text notion-block-2e369776e20480e79aa9cf21534f9cad">if写法：</div><div class="notion-text notion-block-2e369776e20480128538f0a7aa22f61c">for写法：</div></div></div><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e369776e2048016ab31c0f55e628f11" data-id="2e369776e2048016ab31c0f55e628f11"><span><div id="2e369776e2048016ab31c0f55e628f11" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e369776e2048016ab31c0f55e628f11" title="32bit立即数"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">32bit立即数</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e369776e2048096a745c19b697003a2"><li>两步走</li><ul class="notion-list notion-list-disc notion-block-2e369776e2048096a745c19b697003a2"><li><b>加载高16位</b>：先用 <code class="notion-inline-code">lui</code> (Load Upper Immediate) 指令，把立即数的高 16 位塞进目标寄存器，这时候低 16 位会自动补零。</li><li><b>合并低16位</b>：然后再用 <code class="notion-inline-code">ori</code> (Or Immediate) 指令，把剩下的低 16 位常数“或”进去，这才凑成一个完整的 32 位立即数。</li></ul></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e369776e204806caf8af4e270dd652a" data-id="2e369776e204806caf8af4e270dd652a"><span><div id="2e369776e204806caf8af4e270dd652a" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e369776e204806caf8af4e270dd652a" title="伪指令"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">伪指令</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e369776e204800fb054d2bd5e0e4529"><li>伪指令就是汇编指令的变种。除了这些玩意儿，大部分汇编指令跟机器指令都是一对一的</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e369776e20480cf8f18cd53a111a78b" data-id="2e369776e20480cf8f18cd53a111a78b"><span><div id="2e369776e20480cf8f18cd53a111a78b" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e369776e20480cf8f18cd53a111a78b" title="指令后缀"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">指令后缀</span></span></h4><table class="notion-simple-table notion-block-2e269776e204803781a7f436374c3150"><tbody><tr class="notion-simple-table-row notion-simple-table-header-row notion-block-2e269776e2048000aa81c507f6602f80"><td class="" style="width:120px"><div class="notion-simple-table-cell">后缀</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">他妈的含义</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">典型指令</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">老子的白话解释</div></td></tr><tr class="notion-simple-table-row notion-block-2e269776e2048083ab76d1b755758d89"><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>i</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>Immediate</b> (立即数)</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><code class="notion-inline-code">addi</code>, <code class="notion-inline-code">ori</code>, <code class="notion-inline-code">lui</code></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">指令里自带一个 16 位的<b>常数</b>，不用去别处抠数据，自给自足。</div></td></tr><tr class="notion-simple-table-row notion-block-2e269776e2048012900ed173276d4871"><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>u</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>Unsigned</b> (无符号/不报溢出)</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><code class="notion-inline-code">addu</code>, <code class="notion-inline-code">lbu</code>, <code class="notion-inline-code">sltu</code></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">1. 算术里指**“爆了也别报警”<b>；2. 读内存/比较里指</b>“把数据当成纯正数”**，高位补 0。</div></td></tr><tr class="notion-simple-table-row notion-block-2e269776e204802bba1cd7f4d76c21bd"><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>b</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>Byte (字节)</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><code class="notion-inline-code"><b>lb</b></code><b>, </b><code class="notion-inline-code"><b>sb</b></code></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>每次只吞吐 8 位 数据。胃口小，专门吃零碎玩意儿。</b></div></td></tr><tr class="notion-simple-table-row notion-block-2e269776e20480f7a737dddf2a2f6c1a"><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>h</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>Half-word (半字)</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><code class="notion-inline-code"><b>lh</b></code><b>, </b><code class="notion-inline-code"><b>sh</b></code></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>每次吞吐 16 位 数据。比上不足比下有余的货。</b></div></td></tr><tr class="notion-simple-table-row notion-block-2e269776e20480e1a748fc772cd99d3a"><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>w</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>Word (字)</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><code class="notion-inline-code"><b>lw</b></code><b>, </b><code class="notion-inline-code"><b>sw</b></code></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>每次吞吐 32 位 数据。MIPS 的标准套餐，一次吃一满口。</b></div></td></tr><tr class="notion-simple-table-row notion-block-2e269776e2048065be5ac3fd707c8351"><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>v</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>Variable</b> (变量/动态)</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><code class="notion-inline-code">sllv</code>, <code class="notion-inline-code">srav</code></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">移位多少位不是定死的，得看<b>寄存器里的脸色</b>。</div></td></tr><tr class="notion-simple-table-row notion-block-2e269776e20480d688f9eac7be31c7bb"><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>a</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>Arithmetic</b> (算术/带符号)</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><code class="notion-inline-code">sra</code>, <code class="notion-inline-code">srav</code></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">专门用于右移，<b>高位补符号位</b>。为了保证负数移位完还是负数。</div></td></tr><tr class="notion-simple-table-row notion-block-2e269776e20480f888cefd3b695c7dde"><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>l</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>Link</b> (保存返回地址)</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><code class="notion-inline-code">jal</code>, <code class="notion-inline-code">bal</code></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">跳走之前，先把下一条指令地址存进 <code class="notion-inline-code">$ra</code>。<b>记着回家的路</b>。</div></td></tr><tr class="notion-simple-table-row notion-block-2e269776e2048020adf9f543a892c200"><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>r</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>Register</b> (寄存器)</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><code class="notion-inline-code">jr</code></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">跳转地址不是写死的，是存放在<b>寄存器</b>里的。</div></td></tr><tr class="notion-simple-table-row notion-block-2e269776e2048044b80de014614fc170"><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>.s</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>Single</b> (单精度浮点)</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><code class="notion-inline-code">add.s</code>, <code class="notion-inline-code">lwc1</code></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">搞 32 位浮点数算的。算这种数的 CPU 单元都他妈是另开的小灶（协处理器）。</div></td></tr><tr class="notion-simple-table-row notion-block-2e269776e2048009a897fcf6c8578e11"><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>.d</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>Double</b> (双精度浮点)</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><code class="notion-inline-code">add.d</code>, <code class="notion-inline-code">ldc1</code></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">搞 64 位浮点数算的。精度更高，也更吃资源。</div></td></tr></tbody></table><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e269776e204801d9fe8dbe7d6364e17" data-id="2e269776e204801d9fe8dbe7d6364e17"><span><div id="2e269776e204801d9fe8dbe7d6364e17" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e269776e204801d9fe8dbe7d6364e17" title="指令分类"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">指令分类</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e269776e20480a9b09cd4d9cd26d9ae"><li><b>R型</b>是干苦力的，算数的。</li><ul class="notion-list notion-list-disc notion-block-2e269776e20480a9b09cd4d9cd26d9ae"><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e369776e204800bbc7fcca3562ee85a"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A88e48e1b-f321-4b09-a3d2-3ceccc049131%3Aimage.png?table=block&amp;id=2e369776-e204-800b-bc7f-cca3562ee85a&amp;t=2e369776-e204-800b-bc7f-cca3562ee85a" alt="notion image" loading="lazy" decoding="async"/></div></figure><li><code class="notion-inline-code"><b>op</b></code><b> (6 bits)</b>：操作码。在 R-型指令里，这 6 位<b>全是他妈的 0</b>！</li><li><code class="notion-inline-code"><b>rs</b></code><b> (5 bits)</b>：第一个源寄存器。</li><li><code class="notion-inline-code"><b>rt</b></code><b> (5 bits)</b>：第二个源寄存器。</li><li><code class="notion-inline-code"><b>rd</b></code><b> (5 bits)</b>：<b>Destination (目的地)</b>！也就是算完的结果往哪儿塞。</li><li><code class="notion-inline-code"><b>shamt</b></code><b> (5 bits)</b>：全称是 <b>Shift Amount</b>（移位量）。</li><ul class="notion-list notion-list-disc notion-block-2e369776e20480ab9d1bc2b6ae8b2b73"><li>只有在做 <code class="notion-inline-code">sll</code>（左移）或者 <code class="notion-inline-code">srl</code>（右移）的时候才用。</li><li>普通的加减法，这里就是<b>纯种的 0</b>，没它什么屁事儿。</li></ul><li><code class="notion-inline-code"><b>funct</b></code><b> (6 bits)</b>：<b>功能码</b>。这才是决定“干什么活”的关键！</li><ul class="notion-list notion-list-disc notion-block-2e369776e20480c39e12f5c4a3725517"><li>同样是 <code class="notion-inline-code">op=0</code>，如果这里是 <b>32</b> (二进制 <code class="notion-inline-code">100000</code>)，那就是 <b>add</b>。</li><li>如果是别的数，可能就是减法、乘法或者逻辑运算。</li></ul><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e369776e2048057aae0c0886aa8d30d"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A496e7bd5-447b-45d2-a40d-c1c266fdc9ed%3Aimage.png?table=block&amp;id=2e369776-e204-8057-aae0-c0886aa8d30d&amp;t=2e369776-e204-8057-aae0-c0886aa8d30d" alt="notion image" loading="lazy" decoding="async"/></div></figure></ul></ul><ul class="notion-list notion-list-disc notion-block-2e269776e2048017b438eb2891594a02"><li><b>I型</b>是灵活的，搞立即数、读写内存、条件判断。</li><ul class="notion-list notion-list-disc notion-block-2e269776e2048017b438eb2891594a02"><li>将R-型指令中的rd、shamt、funct字段合并为constant or address字段</li><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e369776e204802ea2afce5a73876c37"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A89caa2b0-292a-4eec-a648-8fea1e49ab43%3Aimage.png?table=block&amp;id=2e369776-e204-802e-a2af-ce5a73876c37&amp;t=2e369776-e204-802e-a2af-ce5a73876c37" alt="notion image" loading="lazy" decoding="async"/></div></figure><li><code class="notion-inline-code"><b>op</b></code><b> (6位)</b>：这次<b>绝对不是 0</b> 了！每一个 I-型指令都有自己唯一的编号（比如 <code class="notion-inline-code">addi</code> 是 8，<code class="notion-inline-code">lw</code> 是 35）。CPU 一看前 6 位，就知道这活儿是加法、读内存还是跳槽。</li><li><code class="notion-inline-code"><b>rs</b></code><b> (5位)</b>：还是老样子，<b>源寄存器</b>。</li><li><code class="notion-inline-code"><b>rt</b></code><b> (5位)</b>：<b>这是最他妈坑爹的地方！</b></li><ul class="notion-list notion-list-disc notion-block-2e369776e2048092ab22c3194dd3776c"><li>在 <code class="notion-inline-code">addi</code> 或者 <code class="notion-inline-code">lw</code>（读内存）里，它是<b>目的地</b>（结果存这儿）。</li><li>在 <code class="notion-inline-code">beq</code>（相等跳转）或者 <code class="notion-inline-code">sw</code>（存内存）里，它是<b>第二个源</b>。</li><li>这 <code class="notion-inline-code">rt</code> 就是个“两面派”，一会儿当爹一会儿当儿子，你得看前面的 <code class="notion-inline-code">op</code> 才知道它是个什么货色。</li></ul><li><code class="notion-inline-code"><b>immediate</b></code><b> (16位)</b>：这就是传说中的<b>立即数</b>！</li><ul class="notion-list notion-list-disc notion-block-2e369776e20480d38ec6df91f4164976"><li>它占据了指令整整一半的长度（16 位）。</li><li>它可以存一个常数，或者一个内存地址的偏移量。</li><li><b>范围</b>：因为它带符号，所以能存 <b>32768 到 32767</b> 之间的数。你要是想存个 100 万？去你妈的，存不下！</li></ul></ul></ul><ul class="notion-list notion-list-disc notion-block-2e269776e20480868577ec625ce289a2"><li><b>J型</b>是到处飞的。</li></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e369776e2048029b326fb2e5eb3d027" data-id="2e369776e2048029b326fb2e5eb3d027"><span><div id="2e369776e2048029b326fb2e5eb3d027" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e369776e2048029b326fb2e5eb3d027" title="寻址"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">寻址</span></span></h3><table class="notion-simple-table notion-block-2e369776e20480208026ec7cc967c8d4"><tbody><tr class="notion-simple-table-row notion-simple-table-header-row notion-block-2e369776e204807ea2a6c5c0eeb8a711"><td class="" style="width:120px"><div class="notion-simple-table-cell">寻址模式</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">英文全称</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">核心逻辑</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">典型指令</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">解释</div></td></tr><tr class="notion-simple-table-row notion-block-2e369776e2048047b52be82f4b592bcf"><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>1. 立即数寻址</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>Immediate Addressing</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">数据就在指令里揣着</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><code class="notion-inline-code">addi</code>, <code class="notion-inline-code">ori</code></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">兜里揣着现钱，想花直接掏，不用去银行。</div></td></tr><tr class="notion-simple-table-row notion-block-2e369776e20480748952e704d949f26b"><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>2. 寄存器寻址</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>Register Addressing</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">数据在寄存器里存着</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><code class="notion-inline-code">add</code>, <code class="notion-inline-code">sub</code></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">钱在碗里，直接伸手抓，快得一批。</div></td></tr><tr class="notion-simple-table-row notion-block-2e369776e20480e89dcfffcc7c118248"><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>3. 基址寻址</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>Base Addressing</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">寄存器地址 + 偏移量</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><code class="notion-inline-code">lw</code>, <code class="notion-inline-code">sw</code></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">按图索骥。拿着仓库地址（寄存器）再数几个坑位（偏移量）去找货。</div></td></tr><tr class="notion-simple-table-row notion-block-2e369776e20480ed81e0eb9af026d75e"><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>4. PC相对寻址</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>PC-Relative Addressing</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">当前行号 + 偏移量</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><code class="notion-inline-code">beq</code>, <code class="notion-inline-code">bne</code></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">就在附近。从你站的地方往前或往后跑几步。</div></td></tr><tr class="notion-simple-table-row notion-block-2e369776e204800a8f5befc2b9c299e8"><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>5. 伪直接寻址</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><b>Pseudodirect Addressing</b></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">指令里存了大半个地址</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell"><code class="notion-inline-code">j</code>, <code class="notion-inline-code">jal</code></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">瞬间移动。虽然不能满地图乱飞，但在同一个大区里想去哪去哪。</div></td></tr></tbody></table><div class="notion-text notion-block-2e369776e204800fb67df32dfc0662c7"><code class="notion-inline-code">jr</code>是R型指令，跳转范围是32位。</div><div class="notion-text notion-block-2e369776e20480f7b866e3d51247e75e">PC相对寻址只有16位，可以转化为伪直接寻址拓展为26位。</div><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-2da69776e20480179616f7213c5f1c11" data-id="2da69776e20480179616f7213c5f1c11"><span><div id="2da69776e20480179616f7213c5f1c11" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2da69776e20480179616f7213c5f1c11" title="第三章运算"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">第三章运算</span></span></h2><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2da69776e20480068cedf206e3e13a16" data-id="2da69776e20480068cedf206e3e13a16"><span><div id="2da69776e20480068cedf206e3e13a16" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2da69776e20480068cedf206e3e13a16" title="整数"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">整数</span></span></h3><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e369776e20480be92fcc3309a0b9c5c" data-id="2e369776e20480be92fcc3309a0b9c5c"><span><div id="2e369776e20480be92fcc3309a0b9c5c" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e369776e20480be92fcc3309a0b9c5c" title="加"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">加</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e369776e2048034b928db083ca06d44"><li>溢出</li><ul class="notion-list notion-list-disc notion-block-2e369776e2048034b928db083ca06d44"><li>正正相加，符号位1溢出</li><li>负负相加，符号位0溢出</li><li>正负相加，不溢出</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e369776e2048011bc27f9a27943347f"><li>全加器</li><ul class="notion-list notion-list-disc notion-block-2e369776e2048011bc27f9a27943347f"><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e369776e2048021a8bdf1f2c88e92f7"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:546px;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A56f58ca3-a354-4a26-b221-a7cab5beae58%3Aimage.png?table=block&amp;id=2e369776-e204-8021-a8bd-f1f2c88e92f7&amp;t=2e369776-e204-8021-a8bd-f1f2c88e92f7" alt="notion image" loading="lazy" decoding="async"/></div></figure><li><b>输入</b>：A（加数1）、B（加数2）、Cin（上一位传过来的进位）。</li><li><b>输出</b>：S（这一位的和）、Co（给下一位的进位）。</li></ul></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e369776e20480989a09cfbffb1e1a69" data-id="2e369776e20480989a09cfbffb1e1a69"><span><div id="2e369776e20480989a09cfbffb1e1a69" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e369776e20480989a09cfbffb1e1a69" title="减"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">减</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e369776e2048089a8fef4c766a345d0"><li>溢出</li><ul class="notion-list notion-list-disc notion-block-2e369776e2048089a8fef4c766a345d0"><li>正 + 正 = 负？（炸了）</li><li>负 + 负 = 正？（炸了）</li><li>正 - 负（即正+正）= 负？（炸了）</li><li>负 - 正（即负+负）= 正？（炸了）</li><li>MIPS中，指令 add 检测溢出，addu 忽略溢出</li></ul></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e369776e20480a4ba2dcbbd23228f36" data-id="2e369776e20480a4ba2dcbbd23228f36"><span><div id="2e369776e20480a4ba2dcbbd23228f36" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e369776e20480a4ba2dcbbd23228f36" title="乘"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">乘</span></span></h4><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e369776e20480ca80c9e93c0dbdcaa8"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:341px;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A30fa7d2f-b1fd-471a-8b04-e740555ac5bc%3Aimage.png?table=block&amp;id=2e369776-e204-80ca-80c9-e93c0dbdcaa8&amp;t=2e369776-e204-80ca-80c9-e93c0dbdcaa8" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e369776e20480ac923ddc4b515f7774"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Affc3e66c-7a3f-4ded-8938-e6e0ee45abca%3Aimage.png?table=block&amp;id=2e369776-e204-80ac-923d-dc4b515f7774&amp;t=2e369776-e204-80ac-923d-dc4b515f7774" alt="notion image" loading="lazy" decoding="async"/></div></figure><div class="notion-text notion-block-2e369776e20480fb8419e92c9d15934d">这张图最精妙的地方，就是下面那个 <b>64 位的 Product 寄存器</b>。</div><ul class="notion-list notion-list-disc notion-block-2e369776e2048078b7cbceaa4600397b"><li><b>开局状态</b>：</li><ul class="notion-list notion-list-disc notion-block-2e369776e2048078b7cbceaa4600397b"><li><b>左边 32 位</b>：全是 0（准备用来存加法的结果）。</li><li><b>右边 32 位</b>：塞进去的是 <b>Multiplier (乘数)</b>。</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e369776e204800ebff9ca887889eaf6"><li><b>运算过程</b>：</li><ul class="notion-list notion-list-disc notion-block-2e369776e204800ebff9ca887889eaf6"><ol start="1" class="notion-list notion-list-numbered notion-block-2e369776e20480dfabf9eacea0ec82be" style="list-style-type:decimal"><li>看最右边那一位（乘数的最低位）。</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-2e369776e204800bbb6fefc4822d645d" style="list-style-type:decimal"><li>如果是 <b>1</b>，就把 <b>32 位被乘数</b> 加到 <b>64 位寄存器的左半边</b>。</li></ol><ol start="3" class="notion-list notion-list-numbered notion-block-2e369776e2048072aaa8daa21abac88a" style="list-style-type:decimal"><li>如果是 <b>0</b>，屁也不加。</li></ol><ol start="4" class="notion-list notion-list-numbered notion-block-2e369776e2048006968bf605c2e02e94" style="list-style-type:decimal"><li><b>关键动作来了</b>：<b>整个 64 位寄存器一起向右移一位！</b></li></ol><li><b>逻辑</b>：效果是一样的！你把数往左挪，和你把底下的纸往右抽，相对位移他妈的一模一样。</li><li><b>骚点</b>：随着计算的进行，右边的<b>乘数</b>被一位位挤出去了（因为不需要了），而左边算出来的<b>乘积</b>正好一位位占领了右边的坑位。</li><li><b>最后结果</b>：等 32 次循环跑完，原来的乘数被彻底“扫地出门”，整个 64 位寄存器里装的全是算好的<b>乘积</b>。</li></ul></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e369776e204807e9be7e417b8516ed9" data-id="2e369776e204807e9be7e417b8516ed9"><span><div id="2e369776e204807e9be7e417b8516ed9" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e369776e204807e9be7e417b8516ed9" title="除"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">除</span></span></h4><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e369776e204801db6fde362e8bae1a6"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:518px;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Afa6aec4c-966e-4d17-8e3d-c09336adba6f%3Aimage.png?table=block&amp;id=2e369776-e204-801d-b6fd-e362e8bae1a6&amp;t=2e369776-e204-801d-b6fd-e362e8bae1a6" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e369776e204800bac6ae821b008eb67"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A70e78ebf-c006-4f3c-85be-d7ca45a6a2a9%3Aimage.png?table=block&amp;id=2e369776-e204-800b-ac6a-e821b008eb67&amp;t=2e369776-e204-800b-ac6a-e821b008eb67" alt="notion image" loading="lazy" decoding="async"/></div></figure><ul class="notion-list notion-list-disc notion-block-2e369776e2048024bdf7c5e6efb26e43"><li><b>余数的符号必须跟被除数（Dividend）保持一致！</b></li><ul class="notion-list notion-list-disc notion-block-2e369776e2048024bdf7c5e6efb26e43"><li>7÷(−2)=−3……1 （被除数 7 是正，余数 1 就是正）</li><li>(−7)÷2=−3……(−1)（被除数 -7 是负，余数 -1 必须是负）</li><li>(−7)÷(−2)=3……(−1)（被除数 -7 是负，余数还是 -1）</li></ul></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e369776e204801883a6e623b763ade3" data-id="2e369776e204801883a6e623b763ade3"><span><div id="2e369776e204801883a6e623b763ade3" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e369776e204801883a6e623b763ade3" title="浮点数"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">浮点数</span></span></h3><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e369776e20480ef9414f2d71aaa3256" data-id="2e369776e20480ef9414f2d71aaa3256"><span><div id="2e369776e20480ef9414f2d71aaa3256" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e369776e20480ef9414f2d71aaa3256" title="加减法"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">加减法</span></span></h4><ol start="1" class="notion-list notion-list-numbered notion-block-2e369776e2048020ad41f248e9731400" style="list-style-type:decimal"><li>对阶</li><ol class="notion-list notion-list-numbered notion-block-2e369776e2048020ad41f248e9731400" style="list-style-type:lower-alpha"><ul class="notion-list notion-list-disc notion-block-2e369776e20480d69a9fed84f54443c8"><li>小阶向大阶看齐，向右移位直到指数相同</li></ul><ul class="notion-list notion-list-disc notion-block-2e369776e2048050beaeec53ce5e3dcd"><li>丢精度</li></ul></ol></ol><ol start="2" class="notion-list notion-list-numbered notion-block-2e369776e20480fbbc04dedc619c85d0" style="list-style-type:decimal"><li>尾数加减</li><ol class="notion-list notion-list-numbered notion-block-2e369776e20480fbbc04dedc619c85d0" style="list-style-type:lower-alpha"><ul class="notion-list notion-list-disc notion-block-2e369776e2048003bc63e1c693c267e1"><li></li></ul></ol></ol><ol start="3" class="notion-list notion-list-numbered notion-block-2e369776e2048008a775d1911e1ad4a4" style="list-style-type:decimal"><li>规格化为IEEE格式</li></ol><ol start="4" class="notion-list notion-list-numbered notion-block-2e369776e204803fb5cbf7bed8992ff8" style="list-style-type:decimal"><li>舍入</li><ol class="notion-list notion-list-numbered notion-block-2e369776e204803fb5cbf7bed8992ff8" style="list-style-type:lower-alpha"><ul class="notion-list notion-list-disc notion-block-2e369776e20480f48b21e2c5b64391bc"><li>四舍五入，是5就保偶数</li></ul></ol></ol><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e369776e2048086a83cce934a2675a7"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Af8a0dd64-8389-4e3a-9c14-4b1e33c02efd%3Aimage.png?table=block&amp;id=2e369776-e204-8086-a83c-ce934a2675a7&amp;t=2e369776-e204-8086-a83c-ce934a2675a7" alt="浮点加法器" loading="lazy" decoding="async"/><figcaption class="notion-asset-caption">浮点加法器</figcaption></div></figure><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e369776e20480f9b2ffd08f5c3fdfa3" data-id="2e369776e20480f9b2ffd08f5c3fdfa3"><span><div id="2e369776e20480f9b2ffd08f5c3fdfa3" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e369776e20480f9b2ffd08f5c3fdfa3" title="乘法"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">乘法</span></span></h4><ol start="1" class="notion-list notion-list-numbered notion-block-2e369776e20480e28d48f98f2c51c6f3" style="list-style-type:decimal"><li>指数相加</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-2e369776e20480f29b7df9f9ab84a403" style="list-style-type:decimal"><li>尾数相乘</li><ol class="notion-list notion-list-numbered notion-block-2e369776e20480f29b7df9f9ab84a403" style="list-style-type:lower-alpha"><ul class="notion-list notion-list-disc notion-block-2e369776e2048010a064f8a20c9ebdff"><li>硬乘</li></ul></ol></ol><ol start="3" class="notion-list notion-list-numbered notion-block-2e369776e20480d1b605ea7c270eb841" style="list-style-type:decimal"><li>规格化</li></ol><ol start="4" class="notion-list notion-list-numbered notion-block-2e369776e2048029b8e9e4fd9b96ad32" style="list-style-type:decimal"><li>舍入</li></ol><ol start="5" class="notion-list notion-list-numbered notion-block-2e369776e2048016aaaad5309e52c50e" style="list-style-type:decimal"><li>确定符号</li></ol><details class="notion-toggle notion-block-2e369776e20480eb9f5fe9ec0d3c598f"><summary>例子</summary><div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e369776e20480db9b70ca7c13afaeb6"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Ab96319f1-b2a8-418b-a4cc-0b2aecee3aa6%3Aimage.png?table=block&amp;id=2e369776-e204-80db-9b70-ca7c13afaeb6&amp;t=2e369776-e204-80db-9b70-ca7c13afaeb6" alt="notion image" loading="lazy" decoding="async"/></div></figure></div></details><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-2da69776e20480f49d18e7095b38befe" data-id="2da69776e20480f49d18e7095b38befe"><span><div id="2da69776e20480f49d18e7095b38befe" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2da69776e20480f49d18e7095b38befe" title="第四章处理器"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">第四章处理器</span></span></h2><ul class="notion-list notion-list-disc notion-block-2da69776e204801f971ff3beb97a8705"><li>7/11/32/54/133</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480469fa5f0de3c3633bf"><li>不含流水线的处理器</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e204808cbd1dd6e18b1c8a3e"><li>ALU</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480c288b1c02b7938c75d"><li>流水线性能计算</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480f2be97e9bdcd97f902"><li>流水线冒险</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e204802e9992dfdfb3f6d8c7"><li>分支冒险气泡插入</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480168266f2d408415f05"><li>多发射</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e204802eb63ac1baf03dd85b"><li>延迟槽，分支预测，分支检测前移</li></ul><details class="notion-toggle notion-block-2e469776e20480bd9223de1f964bcb3a"><summary><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e469776e20480bd9223de1f964bcb3a" data-id="2e469776e20480bd9223de1f964bcb3a"><span><div id="2e469776e20480bd9223de1f964bcb3a" class="notion-header-anchor"></div><span class="notion-h-title">重点的图</span></span></h3></summary><div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e2048081a485dda9551abf75"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A07ed2f82-0237-435c-aa5f-196f7dd488f7%3Aimage.png?table=block&amp;id=2e469776-e204-8081-a485-dda9551abf75&amp;t=2e469776-e204-8081-a485-dda9551abf75" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e204802c9e44f76639411d19"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A73486e53-2238-4513-b110-81c23539168d%3Aimage.png?table=block&amp;id=2e469776-e204-802c-9e44-f76639411d19&amp;t=2e469776-e204-802c-9e44-f76639411d19" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e20480aab799d5ed2da254a6"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A8397e8f7-9bfe-456d-b628-6b69e8536a17%3Aimage.png?table=block&amp;id=2e469776-e204-80aa-b799-d5ed2da254a6&amp;t=2e469776-e204-80aa-b799-d5ed2da254a6" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e20480beae1ff032892858b5"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Ae5f875ed-9a03-48be-bad5-cf402fa2591d%3Aimage.png?table=block&amp;id=2e469776-e204-80be-ae1f-f032892858b5&amp;t=2e469776-e204-80be-ae1f-f032892858b5" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e20480d3b28ae8fff36a9a3c"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Af3ef025c-ba36-449c-8ae6-1bfb0b6a218c%3Aimage.png?table=block&amp;id=2e469776-e204-80d3-b28a-e8fff36a9a3c&amp;t=2e469776-e204-80d3-b28a-e8fff36a9a3c" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e204806e8e8dd2da74471aa2"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A321ba176-8b89-4693-b20c-38d976770ebf%3Aimage.png?table=block&amp;id=2e469776-e204-806e-8e8d-d2da74471aa2&amp;t=2e469776-e204-806e-8e8d-d2da74471aa2" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e20480279e4fff884826534c"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A1c36472f-6e4c-4feb-ac74-17934efb3adc%3Aimage.png?table=block&amp;id=2e469776-e204-8027-9e4f-ff884826534c&amp;t=2e469776-e204-8027-9e4f-ff884826534c" alt="notion image" loading="lazy" decoding="async"/></div></figure><div class="notion-blank notion-block-2e469776e204806dbd42fdb4c459511b"> </div><div class="notion-blank notion-block-2e469776e2048060aad5d2595d1bc7f0"> </div><div class="notion-blank notion-block-2e469776e20480588552e919c83c3cde"> </div><div class="notion-blank notion-block-2e469776e204809b9bd0da7b197371ca"> </div></div></details><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e469776e2048046970cf479966a09bc" data-id="2e469776e2048046970cf479966a09bc"><span><div id="2e469776e2048046970cf479966a09bc" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e2048046970cf479966a09bc" title="指令的执行"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">指令的执行</span></span></h3><ul class="notion-list notion-list-disc notion-block-2e469776e204804885c9e98c9b86ca55"><li>取指令</li><ul class="notion-list notion-list-disc notion-block-2e469776e204804885c9e98c9b86ca55"><li>PC从存储单元取指</li><li>根据寄存器编号从寄存器中读取内容</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e469776e204809daa8fed4cb621aeae"><li>后续操作与指令类型相关</li><ul class="notion-list notion-list-disc notion-block-2e469776e204809daa8fed4cb621aeae"><li>除跳转指令，其他都要经过ALU</li><ul class="notion-list notion-list-disc notion-block-2e469776e2048025870be7117f2e884c"><li>计算</li><li>方寸地址</li><li>分支条件</li></ul><li>存储访问指令读写内存</li><li>将ALU运算结果写入寄存器</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480939c45c8d18f146793"><li>PC跳到下一条指令</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e20480f19cf7dd66fe30dffc" data-id="2e469776e20480f19cf7dd66fe30dffc"><span><div id="2e469776e20480f19cf7dd66fe30dffc" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480f19cf7dd66fe30dffc" title="流水图"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">流水图</span></span></h4><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e204809f8e02f16394b126db"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A131036b0-3024-431c-a8a0-b883b6eb8687%3Aimage.png?table=block&amp;id=2e469776-e204-809f-8e02-f16394b126db&amp;t=2e469776-e204-809f-8e02-f16394b126db" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e20480ab9786fd7dacf8fc8e"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A73486e53-2238-4513-b110-81c23539168d%3Aimage.png?table=block&amp;id=2e469776-e204-80ab-9786-fd7dacf8fc8e&amp;t=2e469776-e204-80ab-9786-fd7dacf8fc8e" alt="notion image" loading="lazy" decoding="async"/></div></figure><ol start="1" class="notion-list notion-list-numbered notion-block-2e469776e20480e49592cc729f040082" style="list-style-type:decimal"><li><b>寄存器写入口的 MUX (RegDest)</b>：</li><ol class="notion-list notion-list-numbered notion-block-2e469776e20480e49592cc729f040082" style="list-style-type:lower-alpha"><ul class="notion-list notion-list-disc notion-block-2e469776e204800bbdd2f7e39d31080b"><li>决定是把结果写进 rt 寄存器还是 rd 寄存器。</li></ul></ol></ol><ol start="2" class="notion-list notion-list-numbered notion-block-2e469776e20480b8a0e0fe0d8dae2762" style="list-style-type:decimal"><li><b>ALU 输入端的 MUX (ALUSrc)</b>：</li><ol class="notion-list notion-list-numbered notion-block-2e469776e20480b8a0e0fe0d8dae2762" style="list-style-type:lower-alpha"><ul class="notion-list notion-list-disc notion-block-2e469776e20480239682eacc6f83f643"><li><b>0</b> 代表选寄存器里的值，<b>1</b> 代表选指令里的“立即数”（就是那根从 Instruction 连出来的线）。</li></ul></ol></ol><ol start="3" class="notion-list notion-list-numbered notion-block-2e469776e204801f9590f5544a025982" style="list-style-type:decimal"><li><b>Data Memory 后面的 MUX (MemtoReg)</b>：</li><ol class="notion-list notion-list-numbered notion-block-2e469776e204801f9590f5544a025982" style="list-style-type:lower-alpha"><ul class="notion-list notion-list-disc notion-block-2e469776e20480cdb7deed39308b06c1"><li><b>0</b> 代表把 ALU 算出来的结果写回寄存器，<b>1</b> 代表把从内存捞出来的宝贝写回寄存器。</li></ul></ol></ol><ol start="4" class="notion-list notion-list-numbered notion-block-2e469776e20480c1827ed5b63e784100" style="list-style-type:decimal"><li><b>PC 顶上的那个 MUX (PCSrc)</b>：</li><ol class="notion-list notion-list-numbered notion-block-2e469776e20480c1827ed5b63e784100" style="list-style-type:lower-alpha"><ul class="notion-list notion-list-disc notion-block-2e469776e204801fbbcef70c7f60d156"><li><b>最牛逼的一个</b>：它由 <b>Branch 信号</b> 和 ALU 的 <b>Zero 信号</b> 共同控制（看到那个 <b>AND 门</b> 没？）。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480cbbe15e9dbf2938ecd"><li>只有当这是一条分支指令（Branch=1）且两个数相等（Zero=1）时，才让 PC 跳到新的地址。</li></ul></ol></ol><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e469776e204807386b1ff9bc0b62917" data-id="2e469776e204807386b1ff9bc0b62917"><span><div id="2e469776e204807386b1ff9bc0b62917" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e204807386b1ff9bc0b62917" title="数据通路"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">数据通路</span></span></h3><div class="notion-text notion-block-2e469776e2048070b058daee156f2927">部件：</div><ol start="1" class="notion-list notion-list-numbered notion-block-2e469776e2048064b03fc9840c0273ed" style="list-style-type:decimal"><li><b>算术逻辑单元 (ALU)</b>：这就是工厂里的<b>核心加工机</b>。不管是加减乘除还是比大小，东西扔进去，出来就是结果。这是干活最猛的地方。</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-2e469776e20480758cafe9fd0f99bf94" style="list-style-type:decimal"><li><b>寄存器堆 (Register File)</b>：这是工人的<b>随身工具包</b>。常用的数据得放在这儿，伸手就能拿，速度贼快。</li></ol><ol start="3" class="notion-list notion-list-numbered notion-block-2e469776e20480f4b3b8d20aed9bbe75" style="list-style-type:decimal"><li><b>存储器 (Memory)</b>：这是<b>大仓库</b>。指令仓库（Instruction Memory）放的是“施工图纸”，数据仓库（Data Memory）放的是“原材料”和“成品”。</li></ol><ol start="4" class="notion-list notion-list-numbered notion-block-2e469776e2048006bdb2f766718c0aab" style="list-style-type:decimal"><li><b>程序计数器 (PC)</b>：这是那个<b>看大门的</b>。他手里拿着个名单，决定下一秒该去哪儿掏指令。</li></ol><ol start="5" class="notion-list notion-list-numbered notion-block-2e469776e20480b9b2b4f238d9c9aaa0" style="list-style-type:decimal"><li><b>多路复选器 (MUX)</b>：这是路口的<b>红绿灯和分叉路</b>。不同指令的数据来源不同时，需用复选器。</li></ol><ol start="6" class="notion-list notion-list-numbered notion-block-2e469776e20480dbb3ffdf705338c583" style="list-style-type:decimal"><li><b>各种连接线 (Buses/Wires)</b>：这就是<b>传送带</b>。数据（电信号）顺着这些线从寄存器跑到 ALU，再从 ALU 跑到仓库。</li></ol><div class="notion-text notion-block-2e469776e20480ca97f3f67225aa3454">信号：</div><ul class="notion-list notion-list-disc notion-block-2e469776e204805982c1e658e7db6823"><li><b>读写使能类</b> (<code class="notion-inline-code">RegWrite</code>, <code class="notion-inline-code">MemRead</code>, <code class="notion-inline-code">MemWrite</code>)：决定谁能动，谁不能动。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e2048087a06ee0cc00fa297c"><li><b>路径选择类</b> (<code class="notion-inline-code">RegDst</code>, <code class="notion-inline-code">ALUSrc</code>, <code class="notion-inline-code">MemtoReg</code>, <code class="notion-inline-code">Branch</code>)：决定数据走哪条路。</li></ul><details class="notion-toggle notion-block-2e469776e20480be826ec13ba7617266"><summary>详细</summary><div><h4 class="notion-h notion-h3 notion-block-2e469776e20480b7ae21f5a137c3742f" data-id="2e469776e20480b7ae21f5a137c3742f"><span><div id="2e469776e20480b7ae21f5a137c3742f" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480b7ae21f5a137c3742f" title="1. RegDst (寄存器目标位)"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>1. RegDst (寄存器目标位)</b></span></span></h4><ul class="notion-list notion-list-disc notion-block-2e469776e2048017a15fc277f42e5c9e"><li><b>干嘛的</b>：决定谁是“接盘侠”。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480c6a0ccf1e2e9f87db9"><li><b>逻辑</b>：</li><ul class="notion-list notion-list-disc notion-block-2e469776e20480c6a0ccf1e2e9f87db9"><li><b>0</b>：把结果写进指令的 <code class="notion-inline-code">rt</code> 段（[20:16] 位）。（通常是 Load 指令）</li><li><b>1</b>：把结果写进指令的 <code class="notion-inline-code">rd</code> 段（[15:11] 位）。（通常是 R 型算术指令）</li></ul></ul><h4 class="notion-h notion-h3 notion-block-2e469776e204806fa36ec99ae62af78a" data-id="2e469776e204806fa36ec99ae62af78a"><span><div id="2e469776e204806fa36ec99ae62af78a" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e204806fa36ec99ae62af78a" title="2. RegWrite (寄存器写使能)"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>2. RegWrite (寄存器写使能)</b></span></span></h4><ul class="notion-list notion-list-disc notion-block-2e469776e20480aabe74c5fc74611651"><li><b>干嘛的</b>：终极“准生证”。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e204804bb993eada3c5623b2"><li><b>逻辑</b>：</li><ul class="notion-list notion-list-disc notion-block-2e469776e204804bb993eada3c5623b2"><li><b>1</b>：允许把数据写进寄存器。</li><li><b>0</b>：别碰！哪怕算出来的结果再香，也不准存。</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480e3ad31cd336201b166"><li>这就是个保险栓。如果不该写的时候你开了 1，你那寄存器里存的宝贝分分钟变成一坨不可名状的屎。</li></ul><h4 class="notion-h notion-h3 notion-block-2e469776e2048050897eedc618a14d01" data-id="2e469776e2048050897eedc618a14d01"><span><div id="2e469776e2048050897eedc618a14d01" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e2048050897eedc618a14d01" title="3. ALUSrc (ALU 源操作数选择)"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>3. ALUSrc (ALU 源操作数选择)</b></span></span></h4><ul class="notion-list notion-list-disc notion-block-2e469776e20480f8a70af5b5c978a937"><li><b>干嘛的</b>：选“下酒菜”。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480bf9d40d8ce73481419"><li><b>逻辑</b>：</li><ul class="notion-list notion-list-disc notion-block-2e469776e20480bf9d40d8ce73481419"><li><b>0</b>：选寄存器里的第二个值（<code class="notion-inline-code">Read data 2</code>）。（R 型指令干架用）</li><li><b>1</b>：选指令里的那 16 位立即数（经过符号扩展后的）。（<code class="notion-inline-code">addi</code>, <code class="notion-inline-code">lw</code>, <code class="notion-inline-code">sw</code> 这种带数字的用）</li></ul></ul><h4 class="notion-h notion-h3 notion-block-2e469776e20480a19eabf12a038ffea5" data-id="2e469776e20480a19eabf12a038ffea5"><span><div id="2e469776e20480a19eabf12a038ffea5" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480a19eabf12a038ffea5" title="4. Branch (分支控制)"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>4. Branch (分支控制)</b></span></span></h4><ul class="notion-list notion-list-disc notion-block-2e469776e20480838ba1cdbf1b1fad0c"><li><b>干嘛的</b>：决定要不要“反复横跳”。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e2048009b7cdf28bec917a32"><li><b>逻辑</b>：</li><ul class="notion-list notion-list-disc notion-block-2e469776e2048009b7cdf28bec917a32"><li><b>1</b>：这是一条分支指令（比如 <code class="notion-inline-code">beq</code>），如果此时 ALU 算出来两个数相等（Zero=1），那就跳！</li><li><b>0</b>：给老子老老实实走下一条路。</li></ul></ul><h4 class="notion-h notion-h3 notion-block-2e469776e204809db999eaa48e368bb5" data-id="2e469776e204809db999eaa48e368bb5"><span><div id="2e469776e204809db999eaa48e368bb5" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e204809db999eaa48e368bb5" title="5. MemRead (存储器读使能)"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>5. MemRead (存储器读使能)</b></span></span></h4><ul class="notion-list notion-list-disc notion-block-2e469776e20480049a41f3dbb4bbfc0f"><li><b>干嘛的</b>：翻大仓库。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e2048055a8d1e4f81cb71057"><li><b>逻辑</b>：</li><ul class="notion-list notion-list-disc notion-block-2e469776e2048055a8d1e4f81cb71057"><li><b>1</b>：把数据存储器（Data Memory）里的东西掏出来。</li><li><b>0</b>：仓库大门锁死，不准看。</li></ul></ul><h4 class="notion-h notion-h3 notion-block-2e469776e2048078a258d5e45e95e336" data-id="2e469776e2048078a258d5e45e95e336"><span><div id="2e469776e2048078a258d5e45e95e336" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e2048078a258d5e45e95e336" title="6. MemWrite (存储器写使能)"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>6. MemWrite (存储器写使能)</b></span></span></h4><ul class="notion-list notion-list-disc notion-block-2e469776e20480bc817fde0abe106229"><li><b>干嘛的</b>：往仓库倒垃圾。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480839baccf0b8c7df20f"><li><b>逻辑</b>：</li><ul class="notion-list notion-list-disc notion-block-2e469776e20480839baccf0b8c7df20f"><li><b>1</b>：把寄存器里的东西写进数据存储器。</li><li><b>0</b>：不准往仓库里乱塞东西。</li></ul></ul><h4 class="notion-h notion-h3 notion-block-2e469776e20480b68caad05d3974bf6d" data-id="2e469776e20480b68caad05d3974bf6d"><span><div id="2e469776e20480b68caad05d3974bf6d" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480b68caad05d3974bf6d" title="7. MemtoReg (存储器到寄存器选择)"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>7. MemtoReg (存储器到寄存器选择)</b></span></span></h4><ul class="notion-list notion-list-disc notion-block-2e469776e20480ffaee2f8285c311477"><li><b>干嘛的</b>：决定谁才是“真成果”。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e2048001a739dae2bcacdf35"><li><b>逻辑</b>：</li><ul class="notion-list notion-list-disc notion-block-2e469776e2048001a739dae2bcacdf35"><li><b>0</b>：把 ALU 算出来的结果写回寄存器。</li><li><b>1</b>：把从存储器里捞出来的东西写回寄存器。</li></ul></ul></div></details><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e20480f09a4dd1d9b22b4112" data-id="2e469776e20480f09a4dd1d9b22b4112"><span><div id="2e469776e20480f09a4dd1d9b22b4112" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480f09a4dd1d9b22b4112" title="单周期数据通路"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">单周期数据通路</span></span></h4><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e20480e9bd7dcd79edeb1afa"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Add849ff1-79bf-48c8-a975-c71bd9d5582f%3Aimage.png?table=block&amp;id=2e469776-e204-80e9-bd7d-cd79edeb1afa&amp;t=2e469776-e204-80e9-bd7d-cd79edeb1afa" alt="notion image" loading="lazy" decoding="async"/></div></figure><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e469776e2048012aef6eb15aca9cb4b" data-id="2e469776e2048012aef6eb15aca9cb4b"><span><div id="2e469776e2048012aef6eb15aca9cb4b" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e2048012aef6eb15aca9cb4b" title="控制单元"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">控制单元</span></span></h3><ul class="notion-list notion-list-disc notion-block-2e469776e20480f19162e05bb173c701"><li>ALU的控制信号</li></ul><table class="notion-simple-table notion-block-2e469776e204809a91b2e9503cbbb205"><tbody><tr class="notion-simple-table-row notion-simple-table-header-row notion-block-2e469776e204807b921ad19cab5eccfd"><td class="" style="width:120px"><div class="notion-simple-table-cell">ALU的控制信号</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">功能 F (运算操作)</div></td></tr><tr class="notion-simple-table-row notion-block-2e469776e204805dacd5e8254bf3ba76"><td class="" style="width:120px"><div class="notion-simple-table-cell">0000</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">and</div></td></tr><tr class="notion-simple-table-row notion-block-2e469776e20480ae9291d345cc21daf7"><td class="" style="width:120px"><div class="notion-simple-table-cell">0001</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">or</div></td></tr><tr class="notion-simple-table-row notion-block-2e469776e2048030aca9f4ad4aea9034"><td class="" style="width:120px"><div class="notion-simple-table-cell">0010</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">add</div></td></tr><tr class="notion-simple-table-row notion-block-2e469776e2048010b6f1e6002986453d"><td class="" style="width:120px"><div class="notion-simple-table-cell">0110</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">sub</div></td></tr><tr class="notion-simple-table-row notion-block-2e469776e2048045874acf87ea6d874e"><td class="" style="width:120px"><div class="notion-simple-table-cell">0111</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">slt</div></td></tr><tr class="notion-simple-table-row notion-block-2e469776e2048017a03dccea98d8c909"><td class="" style="width:120px"><div class="notion-simple-table-cell">1100</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">nor</div></td></tr></tbody></table><table class="notion-simple-table notion-block-2e469776e20480caa3e1c0107ebebd23"><tbody><tr class="notion-simple-table-row notion-simple-table-header-row notion-block-2e469776e2048076a142f84dc2cb47bb"><td class="" style="width:120px"><div class="notion-simple-table-cell">op</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">ALU op</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">operation</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">funct</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">ALU function</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">ALU control</div></td></tr><tr class="notion-simple-table-row notion-block-2e469776e2048094a894cc520d47ffff"><td class="" style="width:120px"><div class="notion-simple-table-cell"><code class="notion-inline-code">lw</code></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">00</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">load word</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">xxxxxx</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">add</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0010</div></td></tr><tr class="notion-simple-table-row notion-block-2e469776e204804a8a26d2a8dbfcf701"><td class="" style="width:120px"><div class="notion-simple-table-cell"><code class="notion-inline-code">sw</code></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">00</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">store word</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">xxxxxx</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">add</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0010</div></td></tr><tr class="notion-simple-table-row notion-block-2e469776e2048058b3b1dc4cc38a4009"><td class="" style="width:120px"><div class="notion-simple-table-cell"><code class="notion-inline-code">beq</code></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">01</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">branch equal</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">xxxxxx</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">subtract</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0110</div></td></tr><tr class="notion-simple-table-row notion-block-2e469776e204805bab6fd387f127bcdc"><td class="" style="width:120px"><div class="notion-simple-table-cell">R型指令</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">10</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">add</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">100000</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">add</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0010</div></td></tr><tr class="notion-simple-table-row notion-block-2e469776e20480da83c2c8ee11d5390e"><td class="" style="width:120px"><div class="notion-simple-table-cell">R型指令</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">10</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">subtract</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">100010</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">subtract</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0110</div></td></tr><tr class="notion-simple-table-row notion-block-2e469776e20480e7b85bfa58d619b8c2"><td class="" style="width:120px"><div class="notion-simple-table-cell">R型指令</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">10</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">and</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">100100</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">and</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0000</div></td></tr><tr class="notion-simple-table-row notion-block-2e469776e2048071886fccaeb9acf2b8"><td class="" style="width:120px"><div class="notion-simple-table-cell">R型指令</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">10</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">or</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">100101</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">or</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0001</div></td></tr><tr class="notion-simple-table-row notion-block-2e469776e204809a9c52db63a8c27094"><td class="" style="width:120px"><div class="notion-simple-table-cell">R型指令</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">10</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">slt</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">101010</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">slt</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0111</div></td></tr></tbody></table><table class="notion-simple-table notion-block-2e469776e204807db839f3a172bf2a2f"><tbody><tr class="notion-simple-table-row notion-simple-table-header-row notion-block-2e469776e2048092933be72b38b06bc8"><td class="" style="width:120px"><div class="notion-simple-table-cell">指令</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">RegDst</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">ALUSrc</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">Mem-toReg</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">Reg-Write</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">Mem-Read</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">Mem-Write</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">Branch</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">ALUOp1</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">ALUOp2</div></td></tr><tr class="notion-simple-table-row notion-block-2e469776e204804eb004c069f3a0d5a0"><td class="" style="width:120px"><div class="notion-simple-table-cell">R型指令</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">1</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">1</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">1</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0</div></td></tr><tr class="notion-simple-table-row notion-block-2e469776e204803ca89bff5219cbca38"><td class="" style="width:120px"><div class="notion-simple-table-cell"><code class="notion-inline-code">lw</code></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">1</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">1</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">1</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">1</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0</div></td></tr><tr class="notion-simple-table-row notion-block-2e469776e20480879d89d9982a1d7fe9"><td class="" style="width:120px"><div class="notion-simple-table-cell"><code class="notion-inline-code">sw</code></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">$x$</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">1</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">$x$</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">1</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0</div></td></tr><tr class="notion-simple-table-row notion-block-2e469776e204804681d0c1e9da9d0e31"><td class="" style="width:120px"><div class="notion-simple-table-cell"><code class="notion-inline-code">beq</code></div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">$x$</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">$x$</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">1</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">0</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">1</div></td></tr></tbody></table><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e469776e20480f2a8eefd3616cda53b" data-id="2e469776e20480f2a8eefd3616cda53b"><span><div id="2e469776e20480f2a8eefd3616cda53b" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480f2a8eefd3616cda53b" title="流水线"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">流水线</span></span></h3><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e204800a9d6bd84b499ff385" data-id="2e469776e204800a9d6bd84b499ff385"><span><div id="2e469776e204800a9d6bd84b499ff385" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e204800a9d6bd84b499ff385" title="五级流水线"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">五级流水线</span></span></h4><ol start="1" class="notion-list notion-list-numbered notion-block-2e469776e20480f09448d71758450419" style="list-style-type:decimal"><li>IF：从内存取指</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-2e469776e204803db2c2fedb931e8df4" style="list-style-type:decimal"><li>ID：译码，读寄存器</li></ol><ol start="3" class="notion-list notion-list-numbered notion-block-2e469776e20480378397df6aa8191b39" style="list-style-type:decimal"><li>EX：执行运算</li></ol><ol start="4" class="notion-list notion-list-numbered notion-block-2e469776e20480bab094f96c6777e717" style="list-style-type:decimal"><li>MEM：访存</li></ol><ol start="5" class="notion-list notion-list-numbered notion-block-2e469776e20480c1bae9e6f34beb0722" style="list-style-type:decimal"><li>WB：回写到寄存器</li></ol><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e20480e69670f73715e45b06" data-id="2e469776e20480e69670f73715e45b06"><span><div id="2e469776e20480e69670f73715e45b06" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480e69670f73715e45b06" title="流水线加速比"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">流水线加速比</span></span></h4><div class="notion-text notion-block-2e469776e204808b83cbef1ec89064ee">理想为4-5，但达不到</div><div class="notion-text notion-block-2e469776e2048052953dc145982c042e">因为</div><ul class="notion-list notion-list-disc notion-block-2e469776e20480bb89c6c1d986102e9c"><li>各级不等长</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480fabfbafe91586e3ddd"><li>存在数据冒险</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e20480669e6ff030fa924ea6" data-id="2e469776e20480669e6ff030fa924ea6"><span><div id="2e469776e20480669e6ff030fa924ea6" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480669e6ff030fa924ea6" title="冒险"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">冒险</span></span></h4><ol start="1" class="notion-list notion-list-numbered notion-block-2e469776e2048036a10bd941ddaa675b" style="list-style-type:decimal"><li><b>结构冒险</b>：两个工人都想抢同一个扳手，打起来了，流水线得停。</li><ol class="notion-list notion-list-numbered notion-block-2e469776e2048036a10bd941ddaa675b" style="list-style-type:lower-alpha"><ul class="notion-list notion-list-disc notion-block-2e469776e20480738f03c34566e80b24"><li>解决：把内存拆成“指令内存”和“数据内存”</li></ul></ol></ol><ol start="2" class="notion-list notion-list-numbered notion-block-2e469776e20480c49435e2c1d4d73859" style="list-style-type:decimal"><li><b>数据冒险</b>：后面的人要等前面的人算出结果才能动，<b>停顿 (Stall)</b> 了。</li><ol class="notion-list notion-list-numbered notion-block-2e469776e20480c49435e2c1d4d73859" style="list-style-type:lower-alpha"><ul class="notion-list notion-list-disc notion-block-2e469776e204800c92c0e2b7446d88a1"><li>解决：</li><ul class="notion-list notion-list-disc notion-block-2e469776e204800c92c0e2b7446d88a1"><li>塞气泡</li><ul class="notion-list notion-list-disc notion-block-2e469776e20480628d66f2cdf31e2dd6"><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e20480b2acd4d7986d54d75e"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Aafc5068f-659f-47bc-8c22-4ee7847b166d%3Aimage.png?table=block&amp;id=2e469776-e204-80b2-acd4-d7986d54d75e&amp;t=2e469776-e204-80b2-acd4-d7986d54d75e" alt="notion image" loading="lazy" decoding="async"/></div></figure></ul><li>旁路/前推</li><ul class="notion-list notion-list-disc notion-block-2e469776e20480d492d4eb7f8ff6e006"><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e2048031ab18ee9946ff029e"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Aa1ecdf55-8264-4fda-b730-9c81727f9e53%3Aimage.png?table=block&amp;id=2e469776-e204-8031-ab18-ee9946ff029e&amp;t=2e469776-e204-8031-ab18-ee9946ff029e" alt="notion image" loading="lazy" decoding="async"/></div></figure><div class="notion-text notion-block-2e469776e204800eadb8d6e2aaf8cca5">避免访存，提高效率。</div><div class="notion-text notion-block-2e469776e2048084a763dd9b6788e35b">但是<b>取数-使用型冒险</b>不能用前推解决：</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e204806d98a4fb39c9ddc0b0"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Acdd74a1d-c312-401f-aed1-0bc145161cc3%3Aimage.png?table=block&amp;id=2e469776-e204-806d-98a4-fb39c9ddc0b0&amp;t=2e469776-e204-806d-98a4-fb39c9ddc0b0" alt="notion image" loading="lazy" decoding="async"/></div></figure><div class="notion-text notion-block-2e469776e20480a0af82cea826a9d012">前推只能推一步，如果涉及到访存，只能通过<b>塞气泡</b>或者<b>指令调度</b>（将无关紧要的指令插进来执行）来解决。</div></ul></ul></ul></ol></ol><ol start="3" class="notion-list notion-list-numbered notion-block-2e469776e20480abacc6e768bc3af780" style="list-style-type:decimal"><li><b>控制冒险</b>：需根据前面某条指令的结果才能确定分支的执行。</li><ol class="notion-list notion-list-numbered notion-block-2e469776e20480abacc6e768bc3af780" style="list-style-type:lower-alpha"><ul class="notion-list notion-list-disc notion-block-2e469776e204801c888af311c7afd6e2"><li>解决：</li><ul class="notion-list notion-list-disc notion-block-2e469776e204801c888af311c7afd6e2"><li>ID（译码）阶段加入硬件比较寄存器和计算分支地址，在这一步就决定要不要跳转。</li><li>分支预测：默认不跳转，直接取紧跟着的指令</li><ul class="notion-list notion-list-disc notion-block-2e469776e20480739080e9044a237ace"><li>向后的分支：猜发生，直接跳</li><li>向前的分支：猜不发生，直接执行下一条</li></ul></ul></ul></ol></ol><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e469776e2048047a32ec8acdeab50ec" data-id="2e469776e2048047a32ec8acdeab50ec"><span><div id="2e469776e2048047a32ec8acdeab50ec" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e2048047a32ec8acdeab50ec" title="流水线数据通路"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">流水线数据通路</span></span></h3><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e20480e2af8bfe2bb3f29832"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:690px;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Af4a604bd-833f-4827-b726-7af1add02d1b%3Aimage.png?table=block&amp;id=2e469776-e204-80e2-af8b-fe2bb3f29832&amp;t=2e469776-e204-80e2-af8b-fe2bb3f29832" alt="notion image" loading="lazy" decoding="async"/></div></figure><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e2048061b044d8c925726522" data-id="2e469776e2048061b044d8c925726522"><span><div id="2e469776e2048061b044d8c925726522" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e2048061b044d8c925726522" title="流水线寄存器"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">流水线寄存器</span></span></h4><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e2048068b59adb4642091a89"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A88ea0a5d-66a1-4954-809a-813ae6c666ad%3Aimage.png?table=block&amp;id=2e469776-e204-8068-b59a-db4642091a89&amp;t=2e469776-e204-8068-b59a-db4642091a89" alt="notion image" loading="lazy" decoding="async"/></div></figure><ul class="notion-list notion-list-disc notion-block-2e469776e204808790e1d10bb9296b05"><li>作用：数据隔断，按照时钟周期执行，确保数据同步</li></ul><div class="notion-text notion-block-2e469776e204802397f8ccc773dbc583">红圈里的名字，全是两级名字的组合（如 <b>IF/ID</b>）：</div><ul class="notion-list notion-list-disc notion-block-2e469776e204801ea6dbc5e9d0bcd0c4"><li><b>PC 寄存器</b>：这是<b>“发令枪”</b>。每一秒钟它都指着下一条该死指令的地址。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e204803a98a7d54190e37fc0"><li><b>IF/ID</b>：存的是刚取出来的<b>指令码</b>和 PC+4 的地址。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480f9981adf17f3d37910"><li><b>ID/EX</b>：这里面存的东西最杂！有从寄存器里掏出来的<b>操作数</b>，有符号扩展后的<b>立即数</b>，还有主控单元发出的<b>控制信号</b>。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480a188f4e19a89cdecd4"><li><b>EX/MEM</b>：存的是 ALU 算出来的<b>结果</b>，以及要存入内存的数（如果是 <code class="notion-inline-code">sw</code> 指令的话）。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e204808d8986d53541fb647c"><li><b>MEM/WB</b>：存的是从内存里捞出来的<b>数</b>，或者 ALU 的结果，等着最后写回寄存器。</li></ul><div class="notion-text notion-block-2e469776e20480e3b647c7056a1de1ee">这些寄存器不仅是存数的，它们还在帮指令<b>“传话”</b>。指令每走一级，它的“身份证明”和“控制参数”都要跟着寄存器一起往下挪。这就是同步。</div><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e204803ca308e0a56077f5f3" data-id="2e469776e204803ca308e0a56077f5f3"><span><div id="2e469776e204803ca308e0a56077f5f3" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e204803ca308e0a56077f5f3" title="带阻塞的流水线"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">带阻塞的流水线</span></span></h4><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e204809c887be263a530cebc"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A23d9e1e1-fed5-4604-95dd-25b2bfe0a31f%3Aimage.png?table=block&amp;id=2e469776-e204-809c-887b-e263a530cebc&amp;t=2e469776-e204-809c-887b-e263a530cebc" alt="notion image" loading="lazy" decoding="async"/></div></figure><details class="notion-toggle notion-block-2e469776e20480c48e40fbe2994ef4c8"><summary>解读</summary><div><h4 class="notion-h notion-h3 notion-block-2e469776e2048055bee5d56f2a633b62" data-id="2e469776e2048055bee5d56f2a633b62"><span><div id="2e469776e2048055bee5d56f2a633b62" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e2048055bee5d56f2a633b62" title="1. 物理结构：那四根蓝色的“通天柱”"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>1. 物理结构：那四根蓝色的“通天柱”</b></span></span></h4><div class="notion-text notion-block-2e469776e204804e91f7f53770b7fae5">这四根柱子（IF/ID, ID/EX, EX/MEM, MEM/WB）是<b>时间的围墙</b>。</div><ul class="notion-list notion-list-disc notion-block-2e469776e2048084aaaef4478b4f55b9"><li>它们把整个 CPU 强行切成了五个房间（IF, ID, EX, MEM, WB）。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480d5908af12ace5f7b0e"><li><b>怎么看？</b> 每一级房间只干自己的活，干完就把结果和指令的“档案袋”（那堆控制信号和编号）扔进柱子里。下一秒，下一级房间从柱子里捡起档案继续干。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480e8aa1bc95921d6f40f"><li>这就是<b>“按劳分配，到点下班”</b>。柱子就是为了保证每一级指令互不干扰，谁也别想抢跑。</li></ul><hr class="notion-hr notion-block-2e469776e204800e9c0afba6b81fe74f"/><h4 class="notion-h notion-h3 notion-block-2e469776e20480aab498f6b5326bc3ca" data-id="2e469776e20480aab498f6b5326bc3ca"><span><div id="2e469776e20480aab498f6b5326bc3ca" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480aab498f6b5326bc3ca" title="2. 处理矛盾：两个“大内高手”的博弈"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>2. 处理矛盾：两个“大内高手”的博弈</b></span></span></h4><div class="notion-text notion-block-2e469776e2048041852bfe748df01b1f">这是整张图最硬核的地方，专门对付<b>“数据冒险”</b>：</div><ul class="notion-list notion-list-disc notion-block-2e469776e2048072832fdded6541016c"><li><b>前推单元 (Forwarding Unit)</b>：它是<b>“截胡专家”</b>。</li><ul class="notion-list notion-list-disc notion-block-2e469776e2048072832fdded6541016c"><li><b>它的逻辑</b>：能不停车就不停车！只要后面的指令算出了结果，它就通过那些蓝色的“秘密通道”直接把数送到 ALU 门口。</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e469776e204803a8fbec0c71828450f"><li><b>冒险检测单元 (Hazard Detection Unit)</b>：它是<b>“紧急刹车”</b>。</li><ul class="notion-list notion-list-disc notion-block-2e469776e204803a8fbec0c71828450f"><li><b>它的逻辑</b>：有些数（比如 <code class="notion-inline-code">lw</code> 拿的数）实在太慢，截胡都来不及，它就果断拉手刹（PCWrite=0），让后面的人原地罚站，顺便在流水线里吐个<b>“肥皂泡（NOP）”</b>。</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e469776e2048022ae05c071c5d4e94f"><li>这就是<b>“能私了就私了（前推），私了不了就报警（阻塞）”</b>。</li></ul><hr class="notion-hr notion-block-2e469776e2048090bcfbc2ac3b2507ec"/><h4 class="notion-h notion-h3 notion-block-2e469776e204801085d4d1f27afefea2" data-id="2e469776e204801085d4d1f27afefea2"><span><div id="2e469776e204801085d4d1f27afefea2" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e204801085d4d1f27afefea2" title="3. 控制信号：三组“随身锦囊” (EX, M, WB)"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>3. 控制信号：三组“随身锦囊” (EX, M, WB)</b></span></span></h4><div class="notion-text notion-block-2e469776e2048006ac9dc53c6a888b7f">你得盯着那些从 <code class="notion-inline-code">Control</code> 单元流出来的蓝虚线。</div><ul class="notion-list notion-list-disc notion-block-2e469776e20480068a73da47e309b7b8"><li><b>怎么看？</b> 这些信号不是一次性用完的，它们像剥洋葱一样，每过一级柱子，就丢掉一层。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e2048074a162ff9ea99caaa4"><li><b>EX</b> 在执行级用掉，<b>M</b> 在访存级用掉，<b>WB</b> 熬到最后写回级才用。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e2048044912aec6d395ea7e5"><li>这叫<b>“带着锦囊上路”</b>。指令跑到哪，规矩就跟到哪，谁也别想乱来。</li></ul><hr class="notion-hr notion-block-2e469776e20480dda6ebc6124e0df640"/><h4 class="notion-h notion-h3 notion-block-2e469776e20480138b7dea7d826c25c6" data-id="2e469776e20480138b7dea7d826c25c6"><span><div id="2e469776e20480138b7dea7d826c25c6" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480138b7dea7d826c25c6" title="4. 整体节奏：被“最慢者”支配的恐惧"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title"><b>4. 整体节奏：被“最慢者”支配的恐惧</b></span></span></h4><ul class="notion-list notion-list-disc notion-block-2e469776e20480c78472c2c81f385ca5"><li><b>怎么看？</b> 整个流水线的时钟周期，必须迁就那个最慢的房间（通常是 MEM 级读内存）。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e2048053bb2be8864eff571a"><li>这就好比一个宿舍的人一起去食堂，只要有一个人在那儿<b>磨叽拉稀（访存延迟）</b>，全宿舍的人都得在门口等着他！这就是流水线的代价：虽然吞吐量大了，但单条指令的耗时其实变长了。</li></ul></div></details><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e2048072a837c02ba6f037c8" data-id="2e469776e2048072a837c02ba6f037c8"><span><div id="2e469776e2048072a837c02ba6f037c8" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e2048072a837c02ba6f037c8" title="带冒险检测的流水线"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">带冒险检测的流水线</span></span></h4><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e2048030a989c9dc6064a1e3"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A4d900628-165e-4b0e-8839-37d99044d55c%3Aimage.png?table=block&amp;id=2e469776-e204-8030-a989-c9dc6064a1e3&amp;t=2e469776-e204-8030-a989-c9dc6064a1e3" alt="notion image" loading="lazy" decoding="async"/></div></figure><ul class="notion-list notion-list-disc notion-block-2e469776e20480fea4ecc53ea5053264"><li>主要目的：分支预测，尽量早跳转减少空转</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480af8b16c487914b514d"><li>核心区别：</li><ul class="notion-list notion-list-disc notion-block-2e469776e20480af8b16c487914b514d"><li>ID阶段直接比较寄存器中的两值是否相等，如果相等，Control直接发送IF.Flush信号，将下一条的控制指令清零，杀掉这条操作。</li><li>在ID阶段同时预先算好跳转地址，如果满足跳转条件直接有地址可以跳转。</li><li>PC的值需要先经过一个Mux，决定下面的指令是否跳转。</li></ul></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e469776e204800fa072c8527fc0c30d" data-id="2e469776e204800fa072c8527fc0c30d"><span><div id="2e469776e204800fa072c8527fc0c30d" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e204800fa072c8527fc0c30d" title="异常与中断"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">异常与中断</span></span></h3><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e2048012bc93da69f3b608ba" data-id="2e469776e2048012bc93da69f3b608ba"><span><div id="2e469776e2048012bc93da69f3b608ba" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e2048012bc93da69f3b608ba" title="来源"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">来源</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e469776e204805ea108ce14ea06da32"><li>异常：源于CPU内部事件</li><ul class="notion-list notion-list-disc notion-block-2e469776e204805ea108ce14ea06da32"><li>无效指令，溢出，系统调用指令</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e469776e2048041abe5cba565fc7622"><li>中断：源于外部IO控制器</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e204805ba8ccdca8eea063a1" data-id="2e469776e204805ba8ccdca8eea063a1"><span><div id="2e469776e204805ba8ccdca8eea063a1" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e204805ba8ccdca8eea063a1" title="处理"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">处理</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e469776e20480be8a0ed54f0c6d82e0"><li>MIPS的异常通常由协处理器CP0处理</li><ul class="notion-list notion-list-disc notion-block-2e469776e20480be8a0ed54f0c6d82e0"><ol start="1" class="notion-list notion-list-numbered notion-block-2e469776e2048021a399c0c725c457fa" style="list-style-type:decimal"><li>保存报错指令地址PC，存进寄存器EPC<b> </b>(Exception Program Counter)</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-2e469776e204805e9816ea41fbcc4c49" style="list-style-type:decimal"><li>记录引发异常的原因到状态寄存器Cause</li></ol><ol start="3" class="notion-list notion-list-numbered notion-block-2e469776e204807eadadffc8e12cf225" style="list-style-type:decimal"><li>强制跳到异常处理入口<code class="notion-inline-code"><b>0x 8000 0180</b></code></li></ol></ul></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480898b67f71ed40c2033"><li>指令撤销</li><ul class="notion-list notion-list-disc notion-block-2e469776e20480898b67f71ed40c2033"><li><code class="notion-inline-code"><b>IF-Flush</b></code><b> </b>与分支预测的一样。把刚取出来的指令变成 0。</li><li><code class="notion-inline-code"><b>ID-Flush</b></code><b> </b>如果 EX 级的指令出事了，处于 ID 级的指令也变0。</li><li><code class="notion-inline-code"><b>EX-Flush</b></code><b> </b>同理，如果是在更后面出事，EX 级的指令也变0。</li><li>假设不在 MEM/WB 发生异常。其实生活里很常见，如页表失效。</li></ul></ul><h2 class="notion-h notion-h1 notion-h-indent-0 notion-block-2da69776e204803096a8dc323868524c" data-id="2da69776e204803096a8dc323868524c"><span><div id="2da69776e204803096a8dc323868524c" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2da69776e204803096a8dc323868524c" title="第五章缓存"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">第五章缓存</span></span></h2><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e469776e204808b84a0d35d53dd1fe9" data-id="2e469776e204808b84a0d35d53dd1fe9"><span><div id="2e469776e204808b84a0d35d53dd1fe9" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e204808b84a0d35d53dd1fe9" title="Cache"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">Cache</span></span></h3><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e20480328033d83bce30698c" data-id="2e469776e20480328033d83bce30698c"><span><div id="2e469776e20480328033d83bce30698c" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480328033d83bce30698c" title="直接映射"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">直接映射</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e469776e20480728419c10dc271a272"><li>每个存储器地址对应一个唯一的Cache地址</li></ul><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e2048035a802f19a2337f464"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:576px;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A2b0fee5b-c4a2-4e48-b48c-b7dcd770375a%3Aimage.png?table=block&amp;id=2e469776-e204-8035-a802-f19a2337f464&amp;t=2e469776-e204-8035-a802-f19a2337f464" alt="notion image" loading="lazy" decoding="async"/></div></figure><ul class="notion-list notion-list-disc notion-block-2e469776e2048063b564d2f4a940fd37"><li>MIPS按字对齐，只查找4的倍数，Byte Offset没用，用来找字里面的字节</li></ul><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e20480bfab76e172ce37f659"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:615px;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A202988c8-05aa-44a3-b494-a649074dbbc4%3Aimage.png?table=block&amp;id=2e469776-e204-80bf-ab76-e172ce37f659&amp;t=2e469776-e204-80bf-ab76-e172ce37f659" alt="notion image" loading="lazy" decoding="async"/></div></figure><ul class="notion-list notion-list-disc notion-block-2e469776e20480848035ef502a044760"><li>[11,2]用来计算index（<!-- -->）</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e2048062a4b7e61fccbcdaae"><li>只有index和tag都对上且valid=1才算命中</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e20480e9ad53c40e933354fa" data-id="2e469776e20480e9ad53c40e933354fa"><span><div id="2e469776e20480e9ad53c40e933354fa" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480e9ad53c40e933354fa" title="FastMATH"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">FastMATH</span></span></h4><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e20480108875f7af6f7e0d16"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A67773b5c-c122-4d5c-a16f-118caefb5540%3Aimage.png?table=block&amp;id=2e469776-e204-8010-8875-f7af6f7e0d16&amp;t=2e469776-e204-8010-8875-f7af6f7e0d16" alt="notion image" loading="lazy" decoding="async"/></div></figure><ul class="notion-list notion-list-disc notion-block-2e469776e20480d9b396c767f87299f0"><li>将横轴拉长</li></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e20480f3a73ff7a4ec3037f6" data-id="2e469776e20480f3a73ff7a4ec3037f6"><span><div id="2e469776e20480f3a73ff7a4ec3037f6" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480f3a73ff7a4ec3037f6" title="Cache的性能"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">Cache的性能</span></span></h4><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e20480709bebf943e062e086" data-id="2e469776e20480709bebf943e062e086"><span><div id="2e469776e20480709bebf943e062e086" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480709bebf943e062e086" title="相联"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">相联</span></span></h4><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e2048058bf89e32a50850608"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Affc8726c-bf3f-4fa3-9cf9-59adba3073a5%3Aimage.png?table=block&amp;id=2e469776-e204-8058-bf89-e32a50850608&amp;t=2e469776-e204-8058-bf89-e32a50850608" alt="notion image" loading="lazy" decoding="async"/></div></figure><div class="notion-text notion-block-2e469776e204804a9dbec813d9d8faea">将原本匹配的块编成组，组内有多少个块就多少个比较器，匹配上其中一个就行</div><ul class="notion-list notion-list-disc notion-block-2e469776e204806d8951ffe8674a2fa5"><li>全相联</li><ul class="notion-list notion-list-disc notion-block-2e469776e204806d8951ffe8674a2fa5"><li>一个块可被放在任何地方</li><li>需要查找Cache的所有项</li><li>每个项都要一个比较器</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e469776e204800c8d28f6b7cd3732fd"><li>多路组相联</li><ul class="notion-list notion-list-disc notion-block-2e469776e204800c8d28f6b7cd3732fd"><li>每个组包含n块</li><li>块可以被放在组内的任何一块</li><li>只需要n个比较器</li><ul class="notion-list notion-list-disc notion-block-2e469776e20480ec9430dbc67b9cc842"><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e20480ba8133de4501eba934"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A2f778ecf-a771-4663-b836-8019ed22c2b4%3Aimage.png?table=block&amp;id=2e469776-e204-80ba-8133-de4501eba934&amp;t=2e469776-e204-80ba-8133-de4501eba934" alt="notion image" loading="lazy" decoding="async"/></div></figure></ul></ul></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480379ab7f169abbab2ad"><li>增加相联度，可降低缺失率，但收益递减</li></ul><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e204806f8a3ec373d076456c"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A653cea69-38b8-4a0c-9acb-e15a753ebbb9%3Aimage.png?table=block&amp;id=2e469776-e204-806f-8a3e-c373d076456c&amp;t=2e469776-e204-806f-8a3e-c373d076456c" alt="notion image" loading="lazy" decoding="async"/></div></figure><div class="notion-text notion-block-2e469776e20480f08134f4bd8d9f9618"><b>替换策略</b></div><ul class="notion-list notion-list-disc notion-block-2e469776e20480a7bbc9eccfc98ffe27"><li>优先存没有信息的块</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e2048052832de337fdf77de7"><li>LRU算法，选择最长时间没使用的替换掉</li><ul class="notion-list notion-list-disc notion-block-2e469776e2048052832de337fdf77de7"><li>电路复杂，超过四路难以实现</li><li>目前主流是近似LRU</li></ul></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e20480f79bbdf4ed381a093e" data-id="2e469776e20480f79bbdf4ed381a093e"><span><div id="2e469776e20480f79bbdf4ed381a093e" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480f79bbdf4ed381a093e" title="多级Cache"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">多级Cache</span></span></h4><div class="notion-text notion-block-2e469776e20480db8f6fe6facdd0429b">Cache缺失原因</div><ul class="notion-list notion-list-disc notion-block-2e469776e20480959b3ffa730212ee77"><li>强制缺失：第一次访问</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e204804ba69ee38fd87482b1"><li>容量缺失：Cache太小不足以容纳所有块引起的缺失</li><ul class="notion-list notion-list-disc notion-block-2e469776e204804ba69ee38fd87482b1"><li>例：一个被替换的块又被访问</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e469776e204801b9bdbed1e762ee236"><li>冲突缺失：映射规则约束太死，导致数据发生资源争夺</li><ul class="notion-list notion-list-disc notion-block-2e469776e204801b9bdbed1e762ee236"><li>全相联不会有这个问题</li></ul></ul><table class="notion-simple-table notion-block-2e469776e2048044b55eec935e1b8983"><tbody><tr class="notion-simple-table-row notion-simple-table-header-row notion-block-2e469776e20480e99912daea9e213709"><td class="" style="width:120px"><div class="notion-simple-table-cell">设计变化</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">对缺失率的影响</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">可能对性能的负面影响</div></td></tr><tr class="notion-simple-table-row notion-block-2e469776e20480139c44d65d5abf51b4"><td class="" style="width:120px"><div class="notion-simple-table-cell">增加 cache 容量</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">减少容量缺失，降低缺失率</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">增加访问时间</div></td></tr><tr class="notion-simple-table-row notion-block-2e469776e2048089ba48fcc94240f255"><td class="" style="width:120px"><div class="notion-simple-table-cell">提高相联度</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">减少冲突缺失，降低缺失率</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">增加访问时间</div></td></tr><tr class="notion-simple-table-row notion-block-2e469776e204801796f7e9e7703aa5f6"><td class="" style="width:120px"><div class="notion-simple-table-cell">增加块容量</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">因空间局部性，对宽范围内变化的块大小都能降低缺失率</div></td><td class="" style="width:120px"><div class="notion-simple-table-cell">① 增加缺失代价  ② 块过大时增加缺失率</div></td></tr></tbody></table><div class="notion-text notion-block-2e469776e20480c5a449eb23624cc16b">为了在这些缺陷中取得较优解，对Cache进行分级</div><ul class="notion-list notion-list-disc notion-block-2e469776e20480fbb6d9d12d870076be"><li>L1-Cache：直连CPU，容量小速度快</li><ul class="notion-list notion-list-disc notion-block-2e469776e20480fbb6d9d12d870076be"><li>追求最小化命中时间</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e469776e204807d9caef646cc574c44"><li>L2-Cache：处理L1的miss，容量比L1大但速度慢</li><ul class="notion-list notion-list-disc notion-block-2e469776e204807d9caef646cc574c44"><li>追求低缺失率，避免访问主存</li></ul></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480e9a2a0d714008b9f5b"><li>L2的miss交给主存</li></ul><h3 class="notion-h notion-h2 notion-h-indent-1 notion-block-2e469776e204801f916acabf79b41243" data-id="2e469776e204801f916acabf79b41243"><span><div id="2e469776e204801f916acabf79b41243" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e204801f916acabf79b41243" title="虚拟存储器"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">虚拟存储器</span></span></h3><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e2048002a7dbf000044abf8e"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Ae55dff22-45ff-4d99-967d-d660543a31b7%3Aimage.png?table=block&amp;id=2e469776-e204-8002-a7db-f000044abf8e&amp;t=2e469776-e204-8002-a7db-f000044abf8e" alt="notion image" loading="lazy" decoding="async"/></div></figure><ul class="notion-list notion-list-disc notion-block-2e469776e2048058afc5dd2485d33ce3"><li>地址映射：将虚页号映射到物理页号</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e204804e9c33d3716d1b0017"><li>若缺页，需从硬盘取回，消耗数百万时钟周期</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480518641d87be5e47c05"><li>降低缺页率：</li><ul class="notion-list notion-list-disc notion-block-2e469776e20480518641d87be5e47c05"><li>虚页使用全相联：绝对不能miss</li><li>替换算法智能化，最大程度减少miss发生</li></ul></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e20480e1b782c57dccbd83e6" data-id="2e469776e20480e1b782c57dccbd83e6"><span><div id="2e469776e20480e1b782c57dccbd83e6" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480e1b782c57dccbd83e6" title="页表（PTE）"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">页表（PTE）</span></span></h4><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e2048072abdbd0ce2753be2b"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A00a247ea-1855-4f21-89f8-49576e6f70de%3Aimage.png?table=block&amp;id=2e469776-e204-8072-abdb-d0ce2753be2b&amp;t=2e469776-e204-8072-abdb-d0ce2753be2b" alt="notion image" loading="lazy" decoding="async"/></div></figure><div class="notion-text notion-block-2e469776e20480e3837fe57f012115d7"><b>Virtual Address (32位虚拟地址)</b></div><ul class="notion-list notion-list-disc notion-block-2e469776e20480d1af68c8046401f979"><li>高 <b>20 位</b> 是 <b>Virtual Page Number (VPN)</b>。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480ec9e61c0b1db5580f7"><li>低 <b>12 位</b> 是 <b>Page Offset</b>。这代表一页的大小是 <!-- -->=4KB。</li></ul><div class="notion-text notion-block-2e469776e204804d8883eeb6ed18d571"><b> Valid 位（有效位）</b></div><ul class="notion-list notion-list-disc notion-block-2e469776e2048065ae82edb9ed1f2c75"><li>1：在内存，直接访问</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480ac9c77f1acc10078e5"><li>0：在硬盘，需要访问硬盘</li></ul><div class="notion-blank notion-block-2e469776e2048001ba40de2d31f89a6d"> </div><ul class="notion-list notion-list-disc notion-block-2e469776e20480bebe22dc3ebaf63d17"><li>虚拟内存好处：碎片化管理。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480bb9867fd46d76c315e"><li>在LRU算法（近似）中，PTE的引用位被周期性清零。只有在某页被访问时，引用位才置1。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e2048027bc62f80cf8cd0cc9"><li>每个页表只有一个1bit的引用位。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e2048077ae3bfeb5ce6484df"><li>从磁盘向内存写入数据通常成块写入，不单独写地址</li><ul class="notion-list notion-list-disc notion-block-2e469776e2048077ae3bfeb5ce6484df"><li>采取回写（Write-back）策略，更新的值等在页表中被替换了才写回硬盘。</li></ul></ul><h4 class="notion-h notion-h3 notion-h-indent-2 notion-block-2e469776e20480e59525e8a167ddbd5f" data-id="2e469776e20480e59525e8a167ddbd5f"><span><div id="2e469776e20480e59525e8a167ddbd5f" class="notion-header-anchor"></div><a class="notion-hash-link" href="#2e469776e20480e59525e8a167ddbd5f" title="快表（TLB）"><svg viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg></a><span class="notion-h-title">快表（TLB）</span></span></h4><ul class="notion-list notion-list-disc notion-block-2e469776e2048083b64fc1e948096050"><li>页表的缺陷：地址映射需要额外一次访存</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480a487dbe2bfa18e9bad"><li>解决：现代CPU 在自己身体里（MMU 内部）搞了一个<b>极小、极快</b>的缓存，专门用来存<b>最近用过的页表项</b>，这就是TLB。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e2048051826ff8c13138c89d"><li>就是页表的缓存，快表miss了就去读页表</li></ul><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-2e469776e2048016b35fd20b528c2656"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A2e6b5b33-5bb6-4c60-93f6-ec0c9c434821%3Aimage.png?table=block&amp;id=2e469776-e204-8016-b35f-d20b528c2656&amp;t=2e469776-e204-8016-b35f-d20b528c2656" alt="notion image" loading="lazy" decoding="async"/></div></figure><ul class="notion-list notion-list-disc notion-block-2e469776e20480d6b85cd1ffdab3fc1e"><li><b>Valid (有效位)</b>：这行映射是不是真的。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e20480a090c0f41b6ee1a41e"><li><b>Dirty (脏位)</b>：这页数据被没被改过（回写磁盘时用）。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e204807683a3d4fccae5bdd6"><li><b>Ref (引用位)</b>：用来决定谁该滚出内存。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e204806d84ffc4f91ccba94c"><li><b>Tag</b>：用来匹配虚拟页号的标识。</li></ul><ol start="1" class="notion-list notion-list-numbered notion-block-2e469776e20480329754f65d2f3f8f8b" style="list-style-type:decimal"><li><b>第一步：看 TLB</b>。中了？直接拿物理地址去 Cache 找数据。</li></ol><ol start="2" class="notion-list notion-list-numbered notion-block-2e469776e204806f94c6ff01f1c2c0f7" style="list-style-type:decimal"><li><b>第二步：TLB 没中，看页表 (Page Table)</b>。</li><ol class="notion-list notion-list-numbered notion-block-2e469776e204806f94c6ff01f1c2c0f7" style="list-style-type:lower-alpha"><ul class="notion-list notion-list-disc notion-block-2e469776e20480289aa5c4ae38bec689"><li>页表里有？（Valid=1），赶紧把这一项同步到 TLB 里，然后去内存拿货。</li></ul><ul class="notion-list notion-list-disc notion-block-2e469776e2048077b885fa02af055d47"><li>页表里也没有？（Valid=0），<b>缺页中断！</b> 唤醒操作系统，从硬盘里搬进内存，改页表（如果dirty位为1,则先写回），重新执行指令。</li></ul></ol></ol></div></main></div>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[壁纸分享]]></title>
            <link>http://richardliuda.top/article/wallpaper</link>
            <guid>http://richardliuda.top/article/wallpaper</guid>
            <pubDate>Sat, 14 Jun 2025 00:00:00 GMT</pubDate>
            <content:encoded><![CDATA[<div id="notion-article" class="mx-auto overflow-hidden "><main class="notion light-mode notion-page notion-block-21269776e2048098b221c79f31e49121"><div class="notion-viewport"></div><div class="notion-collection-page-properties"></div><div class="notion-callout notion-gray_background_co notion-block-21269776e20480468799d535776ef9f4"><div class="notion-page-icon-inline notion-page-icon-span"><span class="notion-page-icon" role="img" aria-label="⚠️">⚠️</span></div><div class="notion-callout-text">注：图片来源均为网上收集，鼠鼠我只是个搬运工，请勿商用！
另外，个别图片原图分辨率过低，故利用AI工具超了分辨率。
图片加载不出来的话，可以尝试科学上网，或者直接文末打包下载。
打包的图片体积较大，网页直接保存的体积会小一些。</div></div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-21269776e20480f2b704f0416099c45d"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column"><img style="object-fit:cover" src="https://testingcf.jsdelivr.net/gh/RichardLiuda/MyBlogImages@main/94979903p1.jpg?spaceId=b1106196-e2ad-4338-8aaf-780cd0a84a1b&amp;t=21269776-e204-80f2-b704-f0416099c45d" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-21269776e204804199a0fd437e4bab5e"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A617f5707-e327-4cb3-a21c-07c51c93eb0f%3ANidy-2D-_11670173-gigapixel-cgi-4x.png?table=block&amp;id=21269776-e204-8041-99a0-fd437e4bab5e&amp;t=21269776-e204-8041-99a0-fd437e4bab5e" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-21269776e204802f9e5bf141a9110033"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A97fceffb-4728-497c-9f86-6f0d8319ca01%3AHoneyview_%E4%B8%89%E6%9C%88%E4%B9%9D%E6%97%A5_46230714-gigapixel-standard_v2-4x.jpg?table=block&amp;id=21269776-e204-802f-9e5b-f141a9110033&amp;t=21269776-e204-802f-9e5b-f141a9110033" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-21269776e204802fafa8e1e98e1e2261"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A0f7a5ec4-9200-40b4-8d3e-449762777182%3A%E7%B2%89%E5%B8%AE%E8%80%81%E5%A4%A7_82026799-gigapixel-standard_v2-2x.png?table=block&amp;id=21269776-e204-802f-afa8-e1e98e1e2261&amp;t=21269776-e204-802f-afa8-e1e98e1e2261" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-21269776e2048094adb1ef4f8f2c022d"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A6f835e68-53be-49e4-af5c-54c39f84cc7d%3A%E3%81%93%E3%81%90%E3%81%BE%E3%81%AE%E3%82%B8%E3%83%A7%E3%83%BC%E3%82%A4_93972661-gigapixel-cgi-2x.png?table=block&amp;id=21269776-e204-8094-adb1-ef4f8f2c022d&amp;t=21269776-e204-8094-adb1-ef4f8f2c022d" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-21269776e204809a95dce46040bcd44d"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A26045ce0-58b3-41ae-ac62-f4826d145cf9%3A%E6%99%B0%E6%B1%80_122790865.png?table=block&amp;id=21269776-e204-809a-95dc-e46040bcd44d&amp;t=21269776-e204-809a-95dc-e46040bcd44d" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-21269776e204807293fdffff12be3fcb"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A1e75b0a4-5ab5-49bc-ad6c-9b3f681027f9%3Afb2d8cb5522745d4a6c6486170687e94.jpeg?table=block&amp;id=21269776-e204-8072-93fd-ffff12be3fcb&amp;t=21269776-e204-8072-93fd-ffff12be3fcb" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-21269776e204801db4dad196423ab038"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A2bbd72d8-719a-4224-adc7-4a0e56ca1b99%3A%E8%BE%B0%E6%9A%AEsora_104805436.png?table=block&amp;id=21269776-e204-801d-b4da-d196423ab038&amp;t=21269776-e204-801d-b4da-d196423ab038" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-21269776e204808b9dc4fb02ca976212"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A76e3f75f-7bca-4de5-85e3-ea615994a778%3Amain.webp?table=block&amp;id=21269776-e204-808b-9dc4-fb02ca976212&amp;t=21269776-e204-808b-9dc4-fb02ca976212" alt="notion image" loading="lazy" decoding="async"/></div></figure><div class="notion-blank notion-block-21269776e20480599d1fea6f36e7f489"> </div><div class="notion-text notion-block-21269776e20480b39936c8fe89536802">打包下载：</div><div class="notion-file notion-block-21269776e204805ea1ceef328dfe7be7"><a target="_blank" rel="noopener noreferrer" class="notion-file-link" href="https://file.notion.so/f/f/b1106196-e2ad-4338-8aaf-780cd0a84a1b/b477fba5-5f8b-4ff8-9cc0-69e79837dbe0/package.zip?table=block&amp;id=21269776-e204-805e-a1ce-ef328dfe7be7&amp;spaceId=b1106196-e2ad-4338-8aaf-780cd0a84a1b&amp;expirationTimestamp=1777003200000&amp;signature=XFQ6wig2KLCPCZZzso2DHs-PYa7R2sso9DuhIL_weh0"><svg class="notion-file-icon" viewBox="0 0 30 30"><path d="M22,8v12c0,3.866-3.134,7-7,7s-7-3.134-7-7V8c0-2.762,2.238-5,5-5s5,2.238,5,5v12c0,1.657-1.343,3-3,3s-3-1.343-3-3V8h-2v12c0,2.762,2.238,5,5,5s5-2.238,5-5V8c0-3.866-3.134-7-7-7S6,4.134,6,8v12c0,4.971,4.029,9,9,9s9-4.029,9-9V8H22z"></path></svg><div class="notion-file-info"><div class="notion-file-title">package.zip</div><div class="notion-file-size">84342.4KB</div></div></a></div><div class="notion-blank notion-block-21269776e204805f895ccf1006847cd5"> </div></main></div>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cherry Studio自用CSS]]></title>
            <link>http://richardliuda.top/article/cherry_studio_css</link>
            <guid>http://richardliuda.top/article/cherry_studio_css</guid>
            <pubDate>Sat, 24 May 2025 00:00:00 GMT</pubDate>
            <content:encoded><![CDATA[<div id="notion-article" class="mx-auto overflow-hidden "><main class="notion light-mode notion-page notion-block-1fc69776e20480c28035d4840b1a4a68"><div class="notion-viewport"></div><div class="notion-collection-page-properties"></div><div class="notion-text notion-block-1fc69776e20480069efae116234504a6">纯自用的，属性可以根据自己喜好改。背景<code class="notion-inline-code">--background-image</code>要用图床链接</div><div class="notion-text notion-block-1fc69776e20480c79268d6e413e4bf2d">效果如图所示</div><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-1fc69776e204809c8900fb793582ccf6"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3Aaaa04129-182b-44b6-8f08-eb2f223573ea%3Aimage.png?table=block&amp;id=1fc69776-e204-809c-8900-fb793582ccf6&amp;t=1fc69776-e204-809c-8900-fb793582ccf6" alt="notion image" loading="lazy" decoding="async"/></div></figure><figure class="notion-asset-wrapper notion-asset-wrapper-image notion-block-1fc69776e20480e6aec8c1043a27ac25"><div style="position:relative;display:flex;justify-content:center;align-self:center;width:100%;max-width:100%;flex-direction:column;height:100%"><img style="object-fit:cover" src="https://www.notion.so/image/attachment%3A5a6e7a4b-34ec-4bb7-99fa-c0734890fc17%3Aimage.png?table=block&amp;id=1fc69776-e204-80e6-aec8-c1043a27ac25&amp;t=1fc69776-e204-80e6-aec8-c1043a27ac25" alt="notion image" loading="lazy" decoding="async"/></div></figure><div class="notion-blank notion-block-1fc69776e20480aeafcdfe77c80f417e"> </div></main></div>]]></content:encoded>
        </item>
    </channel>
</rss>