Linux 核心设计: 操作系统术语及概念
面對原始程式碼超越 3 千萬行規模的 Linux 核心 (2023 年),最令人感到挫折的,絕非缺乏程式註解,而是就算見到滿滿的註解,自己卻有如文盲,全然無從理解起。為什麼呢?往往是因為對作業系統的認知太侷限。
Linux 核心发展
虚拟化 (Virtualization) 技术分为 CPU 层级的虚拟化技术,例如 KVM 和 RVM,也有操作系统层级的虚拟化技术,例如 Docker。
- Plan 9 from Bell Labs [Wikipedia]
- LXC [Wikipedia]
看漫画学 Linux
原文地址![](https://turnoff.us/image/en/inside-the-linux-kernel.png)
整理上图,可以得到 自底向上 的 Linux 系统结构:
- 地下层:
- 文件系统 (File System)
- 中央大厅层:
- 进程表 (process table)
- 内存管理 (memory management)
- 信息安全 (security)
- 看门狗 (watchdog)
- httpd
- cron
- 管道 (pipe)
- FTP
- SSH
- Wine
- GNOME
- 最上层
- tty / terminal
wiki:
- Pipeline (Unix) [Wikipedia]
- Process identifier [Wikipedia]
- watchdog [Linux man page]
- init [Wikipedia]
- systemd [Wikipedia]
- fork [Linux man page]
- clone [Linux man page]
- Project Genie [Wikipedia]
- posix_spawn [Linux man page]
- Native POSIX Thread Library [Wikipedia]
- 极客漫画: 不要使用 SIGKILL 的原因
- wait [Linux man page]
- signal [Linux man page]
- TUX web server [Wikipedia] -[x] cron
虚拟内存管理与现代银行的运行逻辑类似,通过 malloc
分配的有效虚拟地址并不能保证真正可用,类似于支票得去银行兑现时才知道银行真正的现金储备。但是根据统计学公式,虚拟地址和银行现金可以保证在大部分情况下,都可以满足需求,当然突发的大规模虚拟内存使用、现金兑现时就无法保证了。这部分的原理推导需要学习概率论、统计学等数理课程。
高阶观点
投影片: Linux Kernel: Introduction ✅
- 对投影片的 重点描述
一些概念理解:
- 1963 Timesharing: A Solution to Computer Bottlenecks [YouTube]
- Supervisory program [Wikipedia]
Monolithic kernel vs Microkernel
- 淺談 Microkernel 設計和真實世界中的應用
- Hybrid kernel [wikipedia]
“As to the whole ‘hybrid kernel’ thing - it’s just marketing. It’s ‘oh, those microkernels had good PR, how can we try to get good PR for our working kernel? Oh, I know, let’s use a cool name and try to imply that it has all the PR advantages that that other system has’.”
—— Linus Torvalds
虚拟化
MicroVM 和 Unikernel 都是使用 CPU 层级的虚拟化技术,在 Host OS 上面构建的 GuestOS:
MicroVM 会减少硬件驱动方面的初始化,从而加快启动和服务速度 (在云服务器方面很常见,服务器端并不需要进行硬件驱动)。
Unikernel 则更激进,将 programs 和 kernel 一起进行动态编译,并且限制只能运行一个 process (例如只运行一个数据库进程,这样云服务器很常见),这样就减少了一些系统调用的呼叫,例如 fork (因为只能运行一个 process),提升了安全性 (因为 fork 系统调用可能会造成一些漏洞)。Unikernel 又叫 Library OS,可以理解为分时多人多工操作系统的另一个对立面,拥有极高的运行速度 (因为只有一个 process)。
Container Sandbox 使用的是 OS 层级的虚拟化技术,即它是将一组进程隔离起来构建为容器,这样可能会导致这一组进程就耗尽了系统的资源,其他进程无法使用系统的资源。同时因为是进程级的隔离,所以安全性不及 CPU 层级的 MicroVM 和 Unikernel。
相关演讲、录影:
- YouTube: Inside the Mac OS X Kernel
- YouTube: What Are MicroVMs? And Why Should I Care?
- YouTube: From the Ground Up: How We Built the Nanos Unikernel
相关论文阅读:
Scalability
Wikipedia: scalability
A system whose performance improves after adding hardware, proportionally to the capacity added, is said to be a scalable system.
- lock-free
- sequence lock
- RCU
- algorithm complexity
eBPF
透过 eBPF 可将 Monolithic kernel 的 Linux 取得 microkernel 的特性
- The Beginners Guide to eBPF Programming, Liza RIce (live programming + source code)
- A thorough introduction to eBPF (four articles in lwn.net), Matt FLeming, December 2017
细节切入点
CPU 和 OS 的基本概念科普网站: Putting the “You” in CPU
相当于科普版 CSAPP
UNSW COMP9242: Advanced Operating Systems (2023/T3)
- YouTube: 2022: UNSW’s COMP9242 Advanced Operating Systems
- 这门课可以作为辅助材料,讲得深入浅出,可以作为进阶材料阅读。
系统软件开发思维
Maslow’s pyramid of code review
![](https://imgur-backup.hackmd.io/DBMmMNi.png)
Benchmark / Profiling
![](https://hackpad-attachments.s3.amazonaws.com/embedded2015.hackpad.com_xDmCCv0k00K_p.299401_1446124062219_truth.jpg)