mirror of
https://github.com/Aexiar/c.git
synced 2024-10-22 12:05:45 +00:00
647 lines
28 KiB
Markdown
647 lines
28 KiB
Markdown
# 第一章:Linux
|
||
|
||
## 1.1 Linux 概述
|
||
|
||
* Linux 是一种开源的操作系统,最初由芬兰的林纳斯·托瓦兹(Linus Torvalds)开发。它基于 UNIX 操作系统,并且具有高度的可定制性和灵活性。Linux 操作系统被广泛应用于`服务器`、嵌入式系统和个人计算机等领域。
|
||
* 它具有稳定性、安全性和可靠性,并且支持多用户、多任务和多线程。Linux 拥有众多的发行版,如:Ubuntu、Red Hat、Debian 等,每个发行版都有自己的特点和优势。
|
||
* 由于其开源性质,Linux 操作系统受到了全球开发者的广泛支持和贡献,成为了计算机行业中的重要组成部分。
|
||
|
||
## 1.2 Linux 的发展史
|
||
|
||
* Linux 的发展历史可以分为以下几个阶段:
|
||
|
||
* ① 初始阶段(1991 - 1994 年):林纳斯·托瓦兹在 1991 年发布了 Linux 内核的第一个版本,并将其开源。在接下来的几年里,Linux 逐渐吸引了一些开发者的关注和参与,开始逐步完善和扩展功能。
|
||
* ② 发展阶段(1995 - 2000 年):在这个阶段,Linux 开始受到更多人的关注和认可。许多开发者和公司开始为 Linux 开发应用程序和驱动程序,为其增加更多的功能和兼容性。同时,一些发行版如:Red Hat、Debian 等也开始出现,为用户提供了更方便的安装和使用方式。
|
||
* ③ 商业化阶段(2001 - 至今):随着 Linux 的成熟和广泛应用,越来越多的公司开始将 Linux 作为服务器和嵌入式设备的操作系统。一些大型科技公司,如:IBM、Oracle 等也开始投入大量资源支持 Linux 的发展。同时,一些商业化的 Linux 发行版,如:SUSE、Ubuntu 等也崭露头角,为企业和个人用户提供了更专业的支持和服务。
|
||
* ④ 社区发展阶段:Linux 的发展一直依赖于全球开发者社区的贡献和支持。Linux 社区不断壮大,吸引了众多的开发者和爱好者参与其中。通过社区的力量,Linux 不断更新迭代,推出了许多新的版本和功能。
|
||
|
||
* 总的来说,Linux 的发展历程是一个由个人项目逐渐发展为全球开源社区支持的过程。它的成功得益于开源模式的优势,以及全球开发者的共同努力和贡献。
|
||
|
||
## 1.3 Linux 的组成(⭐)
|
||
|
||
* Linux 主要由以下几个组成部分构成:
|
||
|
||
* ① `Linux 内核`:Linux 内核是整个操作系统的核心,负责管理硬件设备、内存管理、进程调度等核心功能。它是由林纳斯·托瓦兹及其他开发者编写和维护的。
|
||
* ② `Shell 解释器`:Shell 解释器是用户与操作系统交互的接口,它接收用户输入的命令并将其解释执行。常见的 Shell 解释器有 Bash、Zsh 等,它们提供了命令行界面和脚本编程功能。
|
||
* ③ `外围的应用程序`:命令、应用程序、图形化界面……
|
||
|
||
* 其图示如下:
|
||
|
||

|
||
|
||
* 上述的组成部分共同构成了一个完整的 Linux 操作系统,为用户提供了丰富的功能和灵活性。
|
||
|
||
## 1.4 Linux 发行版本(⭐)
|
||
|
||
* Linux `发行版本`就是:`Linux 内核` + `命令解释器` + `应用程序(桌面)`。
|
||
* 主流的 Linux 发行版本如下:
|
||
|
||
| Linux 系统类别 | Linux 系统 | 备注 |
|
||
| -------------- | --------------------------------- | ------------------------------------------------------------ |
|
||
| Debian 系列 | `Debian` | 更新频率较低,较为稳定和安全,可以用于企业生产环境。 |
|
||
| | `Ubuntu` | 桌面美观,使用方便,开发使用。有桌面版和企业版本(服务器版本)。 |
|
||
| Redhat 系列 | RedHat Enterprise Linux(`RHEL`) | 红帽企业版本 |
|
||
| | CentOS | 基于 Red Hat Enterprise Linux(RHEL)源代码构建的免费开源操作系统。它提供了企业级的稳定性和安全性,广泛用于服务器环境。 |
|
||
| | Fedora | Fedora 是由 Red Hat 赞助的社区驱动的 Linux 发行版,注重最新的软件和技术。它提供了一个先进的桌面环境和开发者工具。 |
|
||
| | `Rocky Linux` | 用来替代 CentOS 系统的,也是基于 Red Hat Enterprise Linux(RHEL)源代码构建的免费开源操作系统。 |
|
||
| | `Alma Linux` | 用来替代 CentOS 系统的,也是基于 Red Hat Enterprise Linux(RHEL)源代码构建的免费开源操作系统。 |
|
||
| 其它系列 | SUSE、OpenSUSE | OpenSUSE 是一个用户友好且功能强大的 Linux 发行版,具有稳定性和灵活性。它提供了多种桌面环境和服务器选项。 |
|
||
|
||
* 当然,除了以上几个主流的 Linux 发行版,还有许多其他的发行版,如:Arch Linux、Gentoo、Mageia 等,它们各自有着不同的特点和适用场景。
|
||
|
||
> [!NOTE]
|
||
>
|
||
> 选择适合自己需求的发行版,是根据个人或组织的需求和偏好来决定的!!!
|
||
|
||
## 1.5 Linux 版本选择(⭐)
|
||
|
||
* 推荐选择 `LTS 版`或`稳定版`。
|
||
|
||
> [!NOTE]
|
||
>
|
||
> * LTS 是 `"Long-Term Support"` 的缩写,意为长期支持。在软件开发领域,LTS 通常用来描述某个版本或发行版的长期支持周期。
|
||
> * 对于 Linux 发行版或其他软件,LTS 版本通常会提供更长时间的支持和维护,以确保稳定性和安全性。这意味着在 LTS 版本的支持周期内,开发者会持续提供安全补丁、错误修复和其他重要更新,以保持系统的稳定性和可靠性。
|
||
> * LTS 版本通常适用于企业和组织等需要长期稳定性和可靠性的环境。相比于非 LTS 版本,LTS 版本的更新频率较低,更注重于提供长期支持和维护。
|
||
|
||
* 本次选择:
|
||
|
||
| 系统版本 | 说明 |
|
||
| --------------------------------------------------------- | ------------------------ |
|
||
| [Ubuntu](https://cn.ubuntu.com/) | LTS 18.04 、LTS 20.04 等 |
|
||
| [AlmaLinux](https://almalinux.org/zh-hans/get-almalinux/) | 9.1、9.2、9.3 等 |
|
||
|
||
|
||
|
||
# 第二章:工作和学习环境部署(⭐)
|
||
|
||
## 2.1 服务器
|
||
|
||
* `服务器`是一种专门用于提供服务的计算机系统。它通常具有更高的性能、可靠性和稳定性,以满足处理大量请求和提供持续服务的需求。
|
||
* `服务器`的主要功能是接收来自客户端的请求,并提供相应的服务或资源。这些服务可以包括:网站托管、数据库管理、文件存储、电子邮件、应用程序运行等。服务器通过网络与客户端进行通信,处理请求并返回相应的数据或结果。
|
||
* `服务器`通常采用高性能的硬件配置,如:多核处理器、大容量内存、高速存储设备等,以确保能够处理大量的并发请求。它们还通常具有冗余的设计,包括冗余电源、冗余网络连接和冗余存储等,以提高可靠性和可用性。
|
||
* `服务器`可以是物理服务器,即实际的硬件设备,也可以是虚拟服务器,即在物理服务器上通过虚拟化技术创建的虚拟机。无论是物理服务器还是虚拟服务器,它们都扮演着提供服务和资源的角色。
|
||
* 在企业和组织中,`服务器`通常由专门的 IT 团队进行管理和维护。他们负责服务器的配置、安全性、性能优化、备份和恢复等工作,以确保服务器的稳定运行和数据的安全性。
|
||
|
||
> [!IMPORTANT]
|
||
>
|
||
> 实际工作的时候,`Linux` 系统会运行在`服务器`上。
|
||
|
||
## 2.2 虚拟机概述
|
||
|
||
* 虚拟机(Virtual Machine,简称 VM)是一种软件实现的`计算机系统`,它在物理计算机上`模拟`出一个`完整的计算环境`。虚拟机可以在同一台物理计算机上同时运行多个独立的操作系统和应用程序。
|
||
|
||

|
||
|
||
* 虚拟机的工作原理是通过`虚拟化技术`将物理计算机的硬件资源(如:处理器、内存、存储等)进行抽象和分配,为每个虚拟机提供一部分资源。每个虚拟机都被视为一个独立的计算机系统,可以运行自己的操作系统和应用程序。
|
||
* 虚拟机的优势在于它可以提供`隔离性`和`灵活性`。通过虚拟机,可以在同一台物理计算机上同时运行多个操作系统,实现资源的共享和最大化利用。同时,虚拟机还可以提供快速部署、快速备份和恢复等功能,方便管理和维护。
|
||
* 常见的虚拟机软件,包括:`VMware`、VirtualBox、KVM 等。它们提供了虚拟化平台和管理工具,使用户可以轻松创建、配置和管理虚拟机。虚拟机广泛应用于开发和测试环境、服务器虚拟化、云计算等领域。
|
||
|
||
> [!NOTE]
|
||
>
|
||
> `虚拟机软件`和`虚拟机`的概念不同:
|
||
>
|
||
> * ① `虚拟机软件`是用来模拟硬件一个`软件`,如:VMware 等。
|
||
> * ② 而`虚拟机`是通过虚拟机软件模拟出来的`计算机硬件`,其实是文件的集合,相当于一台没有安装 Windows 操作系统的`裸机`。
|
||
> * ③ 在现实交谈的时候,我们有时候会模糊`虚拟机软件`和`虚拟机`的概念,甚至会使用`虚拟机`来代替`虚拟机软件`;此时,就需要根据`上下文环境(语境)`来判断到底说的是`虚拟机软件`还是`虚拟机`了。
|
||
|
||
## 2.3 安装虚拟机软件
|
||
|
||
### 2.3.1 前提条件
|
||
|
||
* 需要在 BIOS 中开启`虚拟化`功能。
|
||
|
||

|
||
|
||
### 2.3.2 安装步骤
|
||
|
||
* ① 双击打开 VMware 安装程序 。
|
||
|
||

|
||
|
||
* ② 进行下一步安装:
|
||
|
||

|
||
|
||
* ③ 同意许可协议,单击下一步:
|
||
|
||

|
||
|
||
* ④ 根据需要决定是否需要更改软件的安装位置:
|
||
|
||
> [!TIP]
|
||
>
|
||
> 软件的安装位置,最好是除 C 盘以外的任意盘符下!!!
|
||
|
||

|
||
|
||

|
||
|
||
* ⑤ 更改成功后,单击确定,下一步继续安装:
|
||
|
||

|
||
|
||
* ⑥ 用户体验设置,如下图所示,单击下一步继续安装:
|
||
|
||

|
||
|
||
* ⑦ 快捷方式设置,单击下一步继续安装:
|
||
|
||

|
||
|
||
* ⑧ 单击安装按钮:
|
||
|
||

|
||
|
||
* ⑨ VMware 输入许可证密钥:
|
||
|
||
> [!NOTE]
|
||
>
|
||
> * ① VMware Workstation Pro(16.x)的许可证密钥是:`ZF71R-DMX85-08DQY-8YMNC-PPHV8`。
|
||
> * ② VMware Workstation Pro(17.x)的许可证密钥是:`NZ4RR-FTK5H-H81C1-Q30QH-1V2LA`。
|
||
|
||

|
||
|
||

|
||
|
||
* ⑩ 安装结束后,单击完成按钮:
|
||
|
||

|
||
|
||
* ⑪ 检查虚拟机软件是否安装了虚拟网卡:
|
||
|
||

|
||
|
||

|
||
|
||

|
||
|
||
### 2.3.2 配置网络
|
||
|
||
* ① 打开 VMware 虚拟机软件:
|
||
|
||

|
||
|
||
* ② 编辑 → 虚拟网络编辑器:
|
||
|
||

|
||
|
||
* ③ Vmnet8 → 更改设置:
|
||
|
||

|
||
|
||
* ④ 更改子网 IP 地址为 :`192.168.10.0` ,其目的是为了将 IP 固定。
|
||
|
||

|
||
|
||
* ⑤ DHCP 设置:
|
||
|
||

|
||
|
||

|
||
|
||
* ⑤ 通过 `cmd` ,输入 `ipconfig` 命令,查看是否配置成功:
|
||
|
||

|
||
|
||
## 2.4 创建虚拟机
|
||
|
||
### 2.4.1 概述
|
||
|
||
* 创建虚拟机,本质上就类似购买一台没有安装操作系统的`裸机`。
|
||
|
||
### 2.4.2 创建虚拟机
|
||
|
||
* ① 文件 → 新建虚拟机:
|
||
|
||

|
||
|
||
* ② 选择`自定义`类型的配置:
|
||
|
||

|
||
|
||
* ③ 虚拟机硬件兼容性:下一步即可。
|
||
|
||

|
||
|
||
* ④ 选择`稍后安装操作系统`:
|
||
|
||

|
||
|
||
* ⑤ 选择虚拟机将安装的操作系统:
|
||
|
||

|
||
|
||
* ⑥ 设置虚拟机的`名称`和`安装位置`:
|
||
|
||

|
||
|
||
> [!TIP]
|
||
>
|
||
> 每创建一个虚拟机,就保存在自己独立的目录(文件夹)中,防止引起文件冲突等问题!!!
|
||
|
||
* ⑦ 虚拟机中处理器的配置:下一步即可。
|
||
|
||

|
||
|
||
* ⑧ 设置虚拟机的`内存`:至少 2048 MB以上。
|
||
|
||

|
||
|
||
* ⑨ 设置虚拟机的`网络类型`:下一步即可。
|
||
|
||

|
||
|
||
* ⑩ 设置 I/O 控制器类型:下一步即可。
|
||
|
||

|
||
|
||
* ⑪ 选择磁盘类型:下一步即可。
|
||
|
||

|
||
|
||
* ⑫ 选择磁盘:下一步即可。
|
||
|
||

|
||
|
||
* ⑬ 选择磁盘容量:调整为 40 GB。
|
||
|
||

|
||
|
||
* ⑭ 指定磁盘文件:下一步即可。
|
||
|
||

|
||
|
||
* ⑮ 创建虚拟机完毕:
|
||
|
||

|
||
|
||
* ⑯ 查看是否创建虚拟机成功:
|
||
|
||

|
||
|
||
### 2.4.3 虚拟机归类(分类)
|
||
|
||
#### 2.4.3.1 概述
|
||
|
||
* 实际情况下,我们可能会创建很多`虚拟机`,VMware 提供了`文件夹`的功能来管理这些虚拟机。
|
||
|
||
#### 2.4.3.2 步骤
|
||
|
||
* ① 鼠标右键 → 创建文件夹:
|
||
|
||

|
||
|
||
* ② 修改文件夹的名称:
|
||
|
||

|
||
|
||

|
||
|
||

|
||
|
||
* ③ 将刚才创建的虚拟机`拖`到该文件夹下:
|
||
|
||

|
||
|
||
### 2.4.4 设置 CD/DVD
|
||
|
||
#### 2.4.4.1 概述
|
||
|
||
* 以前,我们在一台裸机上安装操作系统的时候,需要通过 DVD 或 U 盘将操作系统的 ISO 镜像文件,安装到裸机上。
|
||
* 同理,在使用虚拟机软件的时候,也需要此类步骤。
|
||
|
||
#### 2.4.4.2 步骤
|
||
|
||
* ① 编辑虚拟机设置:
|
||
|
||

|
||
|
||
* ② 选择硬盘上的 ISO 镜像文件:
|
||
|
||

|
||
|
||
* ③ 查看是否配置成功:
|
||
|
||

|
||
|
||
## 2.5 安装 AlmaLinux
|
||
|
||
* ① `开启此虚拟机`:
|
||
|
||

|
||
|
||
* ② `鼠标点击`进去之后,通过键盘上的`方向键`来选择:
|
||
|
||

|
||
|
||
> [!NOTE]
|
||
>
|
||
> 键盘上的`方向键`在这里,如下所示:
|
||
>
|
||
> 
|
||
|
||
* ③ 设置操作系统的`语言`环境为`中文`:
|
||
|
||

|
||
|
||
* ④ 设置系统`安装目的地`:
|
||
|
||

|
||
|
||
* ⑤ 设置`软件选择`:默认即可
|
||
|
||

|
||
|
||
* ⑥ 设置 `root` 的密码:
|
||
|
||
> [!CAUTION]
|
||
>
|
||
> * ① 学习阶段无所谓设置什么密码,可以设置得非常简单,如:`123456` 。
|
||
>* ② 但是生产环境,必须复杂且经常更换!!!
|
||
|
||

|
||
|
||
* ⑦ 配置`网络和主机名`:
|
||
|
||

|
||
|
||

|
||
|
||

|
||
|
||

|
||
|
||

|
||
|
||

|
||
|
||
* ⑧ 查看`配置网络和主机名`是否成功:
|
||
|
||

|
||
|
||
* ⑨ 点击`开始安装`:漫长的等待……
|
||
|
||

|
||
|
||
* ⑩ 安装完成之后,会提示`重启系统`,那就重启吧:
|
||
|
||

|
||
|
||
## 2.6 远程连接 Linux
|
||
|
||
### 2.6.1 查看 Linux 的 IP 地址
|
||
|
||
* 通过 `ifconfig` 命令来查看 Linux 的 IP 地址:
|
||
|
||
```shell
|
||
ifconfig
|
||
```
|
||
|
||

|
||
|
||
### 2.6.2 远程连接 Linux
|
||
|
||
#### 2.6.2.1 概述
|
||
|
||
* Linux 的远程连接工具太多了,如:`Xshell`、`WindTerm`、`Xterminal` 等。
|
||
|
||
> [!NOTE]
|
||
>
|
||
> * ① `Xshell7` 是个人或学校免费;但是,商用收费。
|
||
> * ② `WindTerm` 是开源项目,个人、商用都免费。
|
||
> * ③ `Xterminal` 分为`本地仓库模式`和`网络仓库模式`。`网络仓库模式`就是比`本地仓库模式`多了个配置同步的功能。
|
||
|
||
> [!IMPORTANT]
|
||
>
|
||
> * ① 看个人喜好,无所谓使用哪个 Linux 远程连接工具,这些工具使用方法几乎差不多。
|
||
> * ② 但是,本人就喜欢使用 `Xshell7`,所以后文几乎都是使用此工具演示 !!!
|
||
|
||
#### 2.6.2.2 步骤
|
||
|
||
* ① 新建会话:
|
||
|
||

|
||
|
||
* ② 输入 Linux 的 IP 地址:
|
||
|
||

|
||
|
||
* ③ 输入用户名和密码:
|
||
|
||

|
||
|
||
* ④ 接收和保存远程 Linux 的密钥:
|
||
|
||

|
||
|
||
* ⑤ 查看是否登录成功:
|
||
|
||

|
||
|
||
## 2.7 安装 Ubuntu
|
||
|
||
### 2.7.1 安装步骤
|
||
|
||
* ① `开启此虚拟机`:
|
||
|
||

|
||
|
||
* ② `鼠标点击`进去之后,通过键盘上的`Enter`来确认:
|
||
|
||

|
||
|
||
> [!NOTE]
|
||
>
|
||
> 键盘上的`Enter`在这里,如下所示:
|
||
>
|
||
> 
|
||
|
||
* ③ 设置操作系统的`语言`环境为`English`:
|
||
|
||

|
||
|
||
* ④ 键盘配置,通过键盘上的`Enter`来确认:
|
||
|
||

|
||
|
||
* ⑤ 选择系统安装的类型,通过键盘上的`Enter`来确认:
|
||
|
||

|
||
|
||
* ⑥ 设置静态 IP 地址,通过键盘上的`方向键`来选择,修改之后,通过键盘上的`Enter`来选择:
|
||
|
||

|
||
|
||

|
||
|
||

|
||
|
||

|
||
|
||

|
||
|
||
> [!CAUTION]
|
||
>
|
||
> 本人的`网关`和`子网`配置如下:
|
||
>
|
||
> 
|
||
|
||
- ⑦ 配置代理,下一步即可:
|
||
|
||

|
||
|
||
* ⑧ 配置 Ubuntu 系统镜像源地址,下一步即可:
|
||
|
||

|
||
|
||
* ⑨ 引导式存储布局配置,保持默认,下一步即可:
|
||
|
||

|
||
|
||

|
||
|
||
* ⑩ 配置用户名、密码等:
|
||
|
||

|
||
|
||
* ⑪ 安装 SSH 服务:
|
||
|
||

|
||
|
||
* ⑫ 一些精选的流行服务软件包,根据个人情况选择,下一步:
|
||
|
||

|
||
|
||
* ⑬ 配置完成,正式开始安装系统,漫长的等待……
|
||
|
||

|
||
|
||
* ⑭ 安装完成之后,会提示`重启系统`,那就重启吧:
|
||
|
||

|
||
|
||
### 2.7.2 配置 root 密码以及开启远程访问
|
||
|
||
* ① 通过`用户名`和`密码`登录到系统中:
|
||
|
||

|
||
|
||
* ② 通过 `sudo passwd root` 修改 root 的密码:
|
||
|
||

|
||
|
||
* ③ 通过如下命令修改 ssh 配置文件,以便开启 root 远程访问:
|
||
|
||
```shell
|
||
sudo vim /etc/ssh/sshd_config
|
||
```
|
||
|
||
```shell
|
||
PermitRootLogin yes # 36 行
|
||
```
|
||
|
||

|
||
|
||
* ④ 重启 SSH 服务:
|
||
|
||
```shell
|
||
sudo apt-get -y install openssh-server
|
||
```
|
||
|
||
```shell
|
||
sudo systemctl enable --now ssh
|
||
```
|
||
|
||
```shell
|
||
sudo systemctl restart sshd
|
||
```
|
||
|
||

|
||
|
||
* ⑤ 通过 Xshell 远程连接 Ubuntu :
|
||
|
||

|
||
|
||
|
||
|
||
# 第三章:Linux 6.12 内核正式合并实时补丁
|
||
|
||
## 6.1 什么是实时操作系统(RTOS)?
|
||
|
||
* 如果是嵌入式领域的同学,可能对实时操作系统(RTOS,Real Time Operating System)非常熟悉。最具代表性的,有国外的 `μC/OS-III`、`FreeRTOS`、`Vxworks` 等,国内的 `RT-Thread` 和 `LiteOS` 等,如下所示:
|
||
|
||
| 实时操作系统 | 开源/商业 | 地区 |
|
||
| ------------ | --------- | ---- |
|
||
| μC/OS-III | 商业 | 国外 |
|
||
| FreeRTOS | 开源 | 国外 |
|
||
| Vxworks | 商业 | 国外 |
|
||
| RT-Thread | 开源 | 国内 |
|
||
| LiteOS | 开源 | 国内 |
|
||
|
||
> [!NOTE]
|
||
>
|
||
> * 实时操作系统(Real-Time Operating System,`RTOS`)是一种专门为时间敏感系统设计的操作系统。其主要目标是确保任务在严格的时间约束内执行,以保证系统的`实时性`和`确定性`。在 `RTOS` 中,任务必须在预定的时间范围内开始和完成,任何延迟或任务执行时间的不可预见性都会导致系统失效或性能下降。
|
||
> * `RTOS` 的关键特性包括:
|
||
> - ① `实时性`和`确定性`:确保任务在严格规定的时间内完成,系统行为可预测。
|
||
> - ② `低延迟的中断处理`:RTOS能迅速响应中断,确保重要任务及时处理。
|
||
> - ③ `高效的优先级调度`:基于优先级的调度算法,确保高优先级任务优先执行,打断低优先级任务以保证关键任务的时间要求。
|
||
> * `RTOS` 广泛应用于嵌入式系统中,如:`工业控制`**、**`汽车电子`**、**`航空航天`和`医疗设备`等领域,这些系统对响应速度和时间精度有着严格要求,任何时间的不确定性都会导致系统失效或性能下降。如下所示:
|
||
> * ① `工业自动化`:自动化生产线的控制系统需要对传感器和执行器做出毫秒级的精确反应。
|
||
> * ② `汽车电子`:汽车的防抱死制动系统(ABS)、发动机控制单元(ECU)等,必须在极短的时间内响应输入信号
|
||
> * ③ `航空航天`:航天器的导航系统、飞行控制系统等,要求系统能够在严格的时间框架内完成任务。
|
||
> * ④ `医疗设备`:生命支持系统、监护设备等需要保证极高的稳定性和实时性,任何延迟都可能带来生命危险。
|
||
|
||
* 实时操作系统(Real-Time Operating System,`RTOS`)的`核心特性`就是`实时性`。这里的“实时性”并不意味着系统运行速度极快,而是指系统能够在严格规定的时间窗口内做出反应,确保任务的执行时间是可预测的。
|
||
* `硬实时系统`:要求任务必须在严格的时间约束内完成,任何延迟都可能导致系统失败。如:飞机的飞控系统、汽车的防抱死系统(ABS)等,若任务延迟,会导致灾难性的后果。
|
||
* `软实时系统`:允许偶尔的时间超出,但过多的延迟会影响系统性能。如:视频流媒体播放或音频处理系统,虽然偶尔的延迟不会导致系统崩溃,但会影响用户体验。
|
||
* `确定性`是实时操作系统(Real-Time Operating System,`RTOS`)的另一个`关键特性`,指的是系统的行为是可预见的。无论系统处于何种负载下,`RTOS` 都能够在规定的时间内响应外部事件。这意味着任务的执行时间是可预测的,不会被其他低优先级任务打断或拖延。
|
||
|
||
* `RTOS` 通常使用`优先级驱动调度算法`,确保高优先级任务能立即执行,打断低优先级任务。常见的调度机制包括:
|
||
- ① `优先级抢占式调度`:高优先级任务可以中断正在执行的低优先级任务,确保最重要的任务能够在最短时间内得到处理。
|
||
- ② `时间片调度`:有些 RTOS 系统会结合时间片轮转调度,以确保多任务系统中的任务能够共享 CPU 时间。
|
||
* `RTOS` 设计中专注于`减少中断处理的延迟`,使得系统能够在中断发生时迅速响应。如:当传感器发出中断信号时,RTOS 能够在非常短的时间内执行相应的处理代码,以确保事件得到快速响应。
|
||
|
||
> [!NOTE]
|
||
>
|
||
> * ① RTOS 通常非常轻量化,以减少运行时的开销和复杂性。它们的内核较小,不像通用操作系统那样包含大量与实时无关的功能。
|
||
> * ② 常见的 RTOS 内核只提供最基本的操作系统功能,如:任务调度、中断管理、计时器、同步机制等。
|
||
> * ① `任务管理`:RTOS 能够管理多个任务的创建、调度和销毁,每个任务可以有不同的优先级。
|
||
> * ② `中断处理`:实时处理外部或内部的中断信号,确保在尽可能短的时间内响应。
|
||
> * ③ `同步和通信`:RTOS 提供机制(如信号量、互斥锁等)确保多个任务之间可以安全地访问共享资源。
|
||
> * ④ `时钟管理`:提供精确的定时机制,允许任务在指定时间内执行或延迟。
|
||
> * ③ 与通用操作系统的区别:
|
||
> - ① `实时性`和`确定性`:RTOS 的设计目标是实时性和确定性,确保任务在预定时间内执行;而通用操作系统(Linux、Windows 、MacOS)则更多关注多任务处理、吞吐量和用户体验,不对时间约束做严格保证。
|
||
> - ② `任务调度`:RTOS 的任务调度通常基于优先级,而通用操作系统通常使用更复杂的调度算法,如:时间片轮转或完全公平调度。
|
||
> - ③ `规模`和`复杂性`:RTOS 通常规模小、功能简化,适合嵌入式设备;通用操作系统则更庞大,提供了丰富的功能和支持多种应用场景。
|
||
|
||
## 6.2 Linux 的实时性、软实时和硬实时
|
||
|
||
* `实时性`指的是一个操作系统能够在规定的时间点内完成指定的任务操作,一旦超过这个时间点会对整个系统带来不可估量的后果。与此相对的是一般操作系统(Windows、Linux、MacOS),它更注重用户体验,系统偶尔卡顿不会给用户带来灾难性后果。实时性反映了一个系统行为控制的精准能力,具体体现在定时器的精准度高,中断响应及时以及系统的行为固定且可预估等。
|
||
* Linux 系统最初是按照`分时系统`设计并推出的,再加上在历史版本中使用的调度算法目的是公平的分配和使用各种系统资源,保证 CPU 被各个进程公平的使用,所以早期并不支持`实时性`。
|
||
* 但是,在后来的 2.6 版本开始,加入了`内核抢占`的功能,使它的实时性得到了提升,在某种程度上具备了`软实时`的能力。`软实时`指的是系统对于时限要求并不是十分的严格,在一些情况下允许系统超限完成。
|
||
|
||
> [!NOTE]
|
||
>
|
||
> 典型的例子,如下所示:
|
||
>
|
||
> * ① 我们在台式机上使用鼠标操作,偶尔会出现卡顿延迟,这种情况除了让我们抓狂影响使用体验之外(鼠标图标转圈圈同时界面不响应操作)并不会给我们带来严重的影响。
|
||
> * ② 视频信号采集,偶尔丢失几个数据帧,并不会对视频最后的播放带来严重的画面缺失。
|
||
|
||
* `硬实时`就不一样,它对操作系统的行为有着严格的时限要求,超出时限往往会带来灾难性后果。
|
||
|
||
> [!NOTE]
|
||
>
|
||
> 典型的例子,如下所示:
|
||
>
|
||
> * ① 我们日常生活中使用的汽车都配备了安全气囊,汽车在发生激烈碰撞时可能会在 0.2s 内停下,那就要求气囊在 0.02s 内充气完毕并弹出,超出这个时间乘客可能就会面临生命危险。
|
||
> * ② 导弹防御系统,当敌方导弹来袭时拦截系统必须做出 100% 的精准反应并计算出弹道轨迹进行拦截,稍有延迟就会造成拦截失败,这种后果是不可接受的。
|
||
|
||
* 由于 Linux 系统内核过于庞大且模块众多,内核中仍然有不少影响实时性的因素,如:使用大量自旋锁、中断禁止、时钟粒度等,使其距离 `us` 级别的控制精度还有很大的距离。
|
||
|
||
## 6.3 Linux 6.12 内核正式合并实时补丁
|
||
|
||
* 在 2024 年 9 月的欧洲开源峰会上,Linus Torvalds 宣布`PREEMPT_RT`(Real-Time Linux) 补丁已被接受合并到 Linux 主线内核。
|
||
* 换言之,从 Linux 6.12 开始,所有发行版都将包含实时 Linux 代码。这意味着 Linux 将开始运行在更多任务关键设备 (mission-critical devices) 和工业硬件上。
|
||
* 有了这一补丁,Linux 内核实现了完全可抢占,这使它能够在微秒内对事件做出响应。这种能力对于需要精确计时的应用至关重要,如:工业控制系统、机器人和音频制作。
|
||
* 随着实时补丁的合并,Linux 现在有望成为实时操作系统(RTOS)领域的重要参与者。
|