from mosaic import *
OS2023(1)
背景回顾:大家已经有多年的操作系统使用经验,也知道 “操作系统” 是一门很重要的课程,但却无法回答 “操作系统到底是什么”、“为什么重要” 这样的问题。我们将在一学期的时间里回答这些问题,并建立操作系统相关的基本概念、思想方法和编程技巧。
本讲内容:个人/课程简介、操作系统的发展历史,回答三个问题:
slideshow('1.1')
slideshow('1.2')
# Life is short; you need Python.
import z3
import numpy as np
import sympy as sp
import matplotlib.pyplot as plt
x = sp.symbols('x')
def plot(f, points, draw_label=True, draw_points=True):
"""Plot a sympy symbolic polynomial f."""
xmin = min([x_ for x_, _ in points], default=-1) - 0.1
xmax = max([x_ for x_, _ in points], default=1) + 0.1
xs = np.arange(xmin, xmax, (xmax - xmin) / 100)
ys = [f.subs(x, x_) for x_ in xs]
plt.grid(True)
plt.plot(xs, ys)
if draw_points:
plt.scatter(
[x_ for x_, y_ in points],
[y_ for x_, y_ in points],
)
if draw_label:
for x_, y_ in points:
plt.text(x_, y_, f'$({x_},{y_})$', va='bottom', ha='center')
plt.title(f'$y = {sp.latex(f)}$')
plot(x + 1, [], draw_label=False)
plot(x**2 + 1, [], draw_label=False)
plot(x**3 + 1, [], draw_label=False)
def interpolate(n, xs, ys):
"""Return a polynomial that passes through all given points."""
n = max(n, len(xs), len(ys))
if len(xs) == 0: xs = [sp.symbols(f'x{i}') for i in range(n)]
if len(ys) == 0: ys = [sp.symbols(f'y{i}') for i in range(n)]
vs = [sp.symbols(f'a{i}') for i in range(n)]
power = list(range(n))
cons = [
sum(
v * (x_ ** k) for v, k in zip(vs, power)
) - y
for x_, y in zip(xs, ys)
]
sol = list(sp.linsolve(cons, vs))[0]
f = (sum(
v * (x ** k) for v, k in zip(sol, power)
))
return f
xs = [-1, 0, 1, 2, 3]
ys = [-1, 2, 1, 4, 5]
f = interpolate(0, xs, ys)
plot(f, list(zip(xs, ys)), True)
n = 10
xs = np.arange(-1, 1, 1 / n)
ys = np.sin(xs * n)
f = interpolate(0, xs, ys)
plot(f, list(zip(xs, ys)), draw_points=True, draw_label=False)
f
sp.simplify(interpolate(3, [], []))
slideshow('1.3')
demo('logisim', 'i/logisim')
如果你阅读示例代码遇到困难,可以想一想今天的大语言模型已经可以 “阅读理解” 程序的行为 (不过也有错误的地方)。你可以相信课堂上的示例已经尽可能做到 “可读”;你只要学习相关的语法特性,就能理解示例代码。
demo('rvemu', 'i/rvemu')
slideshow('1.4')
如果一件事很困难,通常是因为没有找到正确的方法。这门课程中绝大部分的内容都可以在互联网上找到适当的学习资料。在互联网/搜索引擎普及的时代,只要掌握 “提出问题” 的能力,就能极快地提升自己。同时,这可能将在 AI 时代被颠覆——AI 可能会猜测你的目的,并且告诉你 “应该去做什么”。
操作系统没有传说中那么复杂 (程序视角:对象 + API,硬件视角:一个 C 程序)
教科书 Operating Systems: Three Easy Pieces:
延伸阅读:
浏览课程网站内容,包括:
熟悉一些典型的 Linux 的命令行工具:
gcc --help
的帮助信息了解 gcc 支持的主要功能用每一个工具都完成一个基本的功能,并在你愿意的时候读一读 Tutorial 和手册 (我们强烈推荐你收藏上面这些工具的官方文档,并且在你空闲的时候读一读)。此外,在互联网上找到好的 tutorial 会极大幅地提高你的效率;同时你也可以开始尝试定制你的工具 (例如课堂上的 fish 和 tmux 行为就与默认配置有一定区别)。我们为大家提供了一些参考资料,其中有一些你用得上的线索,例如 The Art of Command Line。
此外,现在已经是 2023 年了。我们也有两条新的建议:
在你的 Linux 中运行课堂上的代码示例。