# OSTEP [虚拟] Chapt. 03-06 - 虚拟化入门

## 4.1 The Abstraction: A Process

OS提供的对运行程序的抽象叫做进程。

## 4.2 Process API

• 新建(create)；
• 销毁(destroy)：主动退出或者被杀死；
• 等待(wait)：等待进程直到运行结束；
• 混合操作(miscellaneous control)：睡眠、恢复等；
• 状态(status)：获得进程状态。

## 4.4 Process States

• Running：进程在处理器上执行指令；
• Blocked：进程此前执行了某些操作，让它在其他事件发生前停止执行。

## 5.1 The fork() System Call

fork()系统调用可用于创建新进程。每个进程会被分得一个编号，叫做process identifier (PID)

fork()调用会创建一个与调用者进程 完 全 一 致 的进程(exact copy of the calling process)。在OS看来，就像是两个进程的拷贝在运行，两个都即将从fork()调用中返回。父进程通过返回值获得了子进程的PID，而子进程获得0，由此可以区分父子进程。

## 5.2 The wait() System Call

wait()系统调用会阻塞当前进程，直到等待对象状态改变/结束为止，然后返回当前进程继续执行。

## 5.3 The exec() System Call

fork()调用只能拷贝当前程序，而exec()系统调用可以运行一个其他的程序。exec()也会拷贝一份进程，然后从给定的可执行文件读取代码，覆盖本身的代码段、重新初始化堆栈，然后执行新的程序。

## 5.4 Why? Motivating The API

fork()exec()的区分对于构建一个Unix外壳(shell)非常重要，因为OS允许shell在fork()之后到exec()之前的中间部分执行代码。这段代码可以改变即将运行的程序的环境，提供更多的特性支持。

##### TIP: Getting It Right

Sometimes, you just have to do the right thing, and when you do, it is way better than the alternatives. There are lots of ways to design APIs for process creation; however, the combination of fork() and exec() are simple and immensely powerful. Here, the UNIX designers simply got it right.

shell是一个工具人用户程序，给你展示一个提示符，等待你向它输入指令。shell接着找出你要来执行的程序在哪里，然后调用fork()来创建一个子进程，再调用exec()的各种变种来执行程序。最后，shell通过wait()等待程序完成退出，此时shell从系统调用中返回，打印新的提示，等待用户的下一个输入。

prompt> wc p3.c > newfile.txt

## 5.5 Process Control And Users

##### ASIDE: The Superuser (Root)

（太长了，又太精彩了我就直接复制整段了）

A system generally needs a user who can administer the system, and is not limited in the way most users are. Such a user should be able to kill an arbitrary process (e.g., if it is abusing the system in some way), even though that process was not started by this user. Such a user should also be able to run powerful commands such as shutdown (which, unsurprisingly, shuts down the system). In UNIX-based systems, these special abilities are given to the superuser (sometimes called root). While most users can't kill other users processes, the superuser can. Being root is much like being Spider-Man: with great power comes great responsibility. Thus, to increase security (and avoid costly mistakes), it's usually better to be a regular user; if you do need to be root, tread carefully, as all of the destructive powers of the computing world are now at your ﬁngertips.

## 5.6 Useful Tools

• ps
• top（现在用htop更多）；
• kill(user friendly killall)；
• 各种各样的CPU性能表(CPU meters)；

## 6.2 Problem #1: Restricted Operations

（此部分很多ICS的内容）

##### TIP: Use Protected Control Transfer

The hardware assists the OS by providing different modes of execution. In user mode, applications do not have full access to hardware resources. In kernel mode, the OS has access to the full resources of the machine. Special instructions to trap into the kernel and return-from-trap back to user-mode programs are also provided, as well as instructions that allow the OS to tell the hardware where the trap table resides in memory.

• 内核初始化跳转表，CPU记住其位置并处理后续中断；
• 内核创建一块内存，执行中断返回函数来真正进入用户态执行程序；程序期望进行系统调用时，自陷并切换到内核态，由OS处理调用；程序运行结束时，执行结束时的调用（如exit()），最后陷入内核态，OS清场，结束运行。

## 6.3 Problem #2: Switching Between Processes

### 合作方案：等待系统调用

##### TIP: Dealing With Application Misbehavior

Operating systems often have to deal with misbehaving processes, those that either through design (maliciousness) or accident (bugs) attempt to do something that they shouldn't. In modern systems, the way the OS tries to handle such malfeasance is to simply terminate the offender. One strike and you're out! Perhaps brutal, but what else should the OS do when you try to access memory illegally or execute an illegal instruction?

### 非合作方案：OS接管

OS可以利用时钟中断，当时钟中断发生，硬件开始执行特定的处理函数，OS重新获得控制权。

##### TIP: Reboot Is Useful

Reboot is useful because it moves software back to a known and likely more tested state. Reboots also reclaim stale or leaked resources (e.g., memory) which may otherwise be hard to handle. Finally, reboots are easy to automate.

Thus, next time you reboot, you are not just enacting some ugly hack. Rather, you are using a time-tested approach to improving the behavior of a computer system. Well done!

### 保存和恢复上下文

OS重新获得控制权后，调度者(scheduler)会决定是否要切换当前运行的程序。如果决定要切换，OS会执行一段低级代码来切换上下文(context switch)。