1.OS-Introduction|Begin

发布时间 2023-04-17 19:16:09作者: 阿新的杂记

image

Von Neumann model of computing

Many millions (and these days, even billions) of times every second, **the processor fetches an instruction from memory, decodes it (i.e., figures out which instruction this is), and executes it **

operating system

In fact, that is responsible for making it easy to run programs (even allowing you to seemingly run many at the same time), allowing programs to share memory, enabling programs to interact with devices, and other fun stuff like that. That body of software is called the operating system.

THE CRUX OF THE PROBLEM -- HOW TO VIRTUALIZE RESOURCES

  • How does the operating system virtualize resources? This is the crux of our problem.
  • Why the OS does this is not the main question, as the answer should be obvious: it makes the system easier to use

You should what is the crux problem!

virtualization

vitualization all resoure!
Easy-to-use virtual form of itself : a ** virtual machine. **
a few hundred system calls,we also sometimes say that the **OS provides a standard library to applications. **
OS is sometimes known as a **resource manager. **

2.1 Virtualizing The CPU

Virtualizing the CPU,让我们看起来像是有很多CPU在运行。其实只是一个,而背后的实现,取决于操作系统OS的策略policy。

代码1:
It doesn’t do much. In fact, all it does is call Spin(), a function that repeatedly checks the time and returns once it has run for a second. Then, it prints out the string that the user passed in on the command line, and repeats, forever.
image.png

-Wall 的意思为显示所有警告
-w的意思是关闭警告
./文件 ./文件 ./文件,一次性启动多个程序

显示1:
image.png
显示2:
Running Many Programs At Once
image.png
the illusion that the system has a very large number of virtual CPUs.
Turning a single CPU (or a small set of them) into a seemingly infinite number of CPUs and thus allowing many programs to seemingly run at once is what we call virtualizing the CPU, the focus of the first major part of this book.

You might also notice that the ability to run multiple programs at once raises all sorts of new questions. For example, if two programs want to run at a particular time, which should run? This question is answered by a policy of the OS; policies are used in many different places within an OS to answer these types of questions, and thus we will study them as we learn about the basic mechanisms that operating systems implement (such as the ability to run multiple programs at once). Hence the role of the OS as a resource manager.
当然,CPU虚拟化也引发出了各种各样的问题,如线程竞争,哪一个优先?

2.2 Virtualizing Memory

image.png
image.png
Physical memory物理内存
The model of physical memory presented by modern machines is very simple. Memory is just an array of bytes; to read memory, one must specify an address to be able to access the data stored there; to write (or update) memory, one must also specify the data to be written to the given address 。
Don’t forget that each instruction of the program is in memory too; thus memory is accessed on each instruction fetch.
字节流->读写地址(不要忘记指令也在内存,所以每一次指令操作都要读写内存)
发现地址是一样的
Indeed, that is exactly what is happening here as the OS is virtualizing memory. Each process accesses its own private virtual address space (sometimes just called its address space), which the OS somehow maps onto the physical memory of the machine. (虚拟内存到物理内存的映射)
A memory reference within one running program does not affect the address space of other processes (or the OS itself); as far as the running program is concerned, it has physical memory all to itself. (互不干扰,有自己的虚拟内存空间)
The reality, however, is that
physical memory is a shared resource, managed by the operating system. (实际上:物理内存是共享的,由操作系统管理)

如何管理,也正是我们要研究的主题!

2.3 Concurrency

image.png
image.png

THE CRUX OF THE PROBLEM: HOW TO BUILD CORRECT CONCURRENT PROGRAMS?
When there are many concurrently executing threads within the same memory space, how can we build a correctly working program? What primitives are needed from the OS? What mechanisms should be provided by the hardware? How can we use them to solve the problems of concurrency ?

Takes three instructions: one to **load the value **of the counter from memory into a register, one to increment it, and one to store it back into memory
Because these three instructions **do not execute atomically (all at once), **strange things can happen.

并发,会引起一系列问题,这是我们要思考且要考虑的。这也是现代编程需要面对的一个大问题。

2.4 Persistence

The hardware comes in the form of some kind of input/output or I/O device; in modern systems, a hard drive is a common repository for** long lived information,** although solid-state drives** (SSDs) **are making head way in this arena as well.
The software in the operating system that usually **manages the disk is called the file system; **it is thus responsible for storing any files the user creates in a reliable and efficient manner on the disks of the system.

硬件作为IO设备出现
文件系统管理磁盘

HOW TO STORE DATA PERSISTENTLY
The file system is the part of the OS in charge of managing persistent data. What techniques are needed to do so correctly? What mechanisms and policies are required to do so with high performance? How is reliability achieved, in the face of failures in hardware and software?

image.png

不像操作系统为 CPU 和内存提供的抽象,操作系统不会为每个应用程序创建专用的虚 拟磁盘。相反,它假设用户经常需要共享(share)文件中的信息。

2.5 Design Goals

An OS actually does: (操作系统做了什么?)
it takes physical resources, such as a CPU, memory, or disk, and virtualizes them. It handles tough and tricky issues related to **concurrency. **And it stores files persistently, thus making them safe over the long-term.

  • One of the most basic goals is to** build up some abstractions **in order to make the system convenient and easy to use
  • One goal in designing and implementing an operating system is to provide high performance; another way to say this is our goal is to **minimize the overheads of the OS **
  • Another goal will be to provide protection between applications, as well as between the OS and applications
  • The operating system must also run non-stop; when it fails, all applications running on the system fail as well. Because of this dependence, operating systems often strive to **provide a high degree of reliability **