操作系统 1. 操作系统简介; 物理状态浅谈;

来源:互联网 发布:淘宝订单在线生成器 编辑:程序博客网 时间:2024/05/20 05:11

读前小记, 傻X教授给了我84分,差一分High Distinction,我吃了屎了!草!
这部分文章将记述我2017 S2在阿德莱德大学的OS课学到的几乎所有关于操作系统的知识, 觉得将来可能会用的上。但愿不会有错误。

Reference:
Silberschatz, A., Galvin, P. and Gagne, G. (2013).Operating system concepts. 9th ed. Hoboken, NJ: John Wiley & Sons.

在这个文章里,我会简单描述下操作系统入门的知识。

(1) 与OS相关的计算机组成结构


1. RAM(Random Access Memory): 又被称作Main Memory,物理内存。就是通常被理解为内存条。 主要用来处理硬盘和硬件上的数据与CPU的数据交换。

2. CPU(Central Processing Unit): 中央处理单元, 通常用来解释计算机指令以及处理计算机软件中的数据(计算)。

3. PC(Program Counter): 又被称作IP(Instruction pointer),用来指向下一个CPU要执行的指令。

4. PIN: CPU本身有一个PIN,这个管脚叫做INT(interrupt controller,硬件中断管理器)。然后这跟管脚和APIC芯片相连,分出来8个管脚, 第8个管脚又采用集连的方式与另外8个管脚相连,由此来支持15个硬件中断。 而在目前的操作系统中,我们主要针对最左边的(第0个)timer搞事情(见item 5)。因此,这个东西我可以理解着把他看作有15个函数。每个函数对应着不同的(15个)参数。当CPU接受到高电流发生中断的时候,CPU会思考一下这是几号中断,然后调用对应的函数(见附注item 2)。


5. Timer: 用来计量当前process最多跑多久, 时间到了之后会以一个或门(or gate)的方式产生一个高电流到PIN,之后会发生硬件中断。



6.  Kernel: 内核是依附在RAM里面的一段代码,经常会被唤醒,用来接受software的各种I/O请求并将其转换成data-processing instruction(数据处理指令)转换给CPU。由中断驱动的一段代码。而在书中的“one program running at all times on the computer—usually called the kernel”(P6, Operating System 9th ed), 个人感觉可以这么理解,但并不是特别准确。Kernel本身并不存在RAM里面,是在开机之后由某些特定的存储物导入的(如硬盘)。这个知识点涉及到操作系统里面非常重要的一个概念,Kernel mode(also called Monitor mode, Super mode) and User mode及内核态和用户态, 后面会进行详细叙述。

附注1:

1. BIOS(Basic Input/Output System): 通常分为BootStrap, BootLoader。 依附在ROM(Read Only Memory)中。 (ROM的东西不易流失基本无法删除,但会在特殊情况被一个叫EPROM的东西擦掉)  


2. IVT(Interrupt Vector Table):假设0写一个timer中断处理函数,1写一个keyboard中断处理函数,2写一个mouse中断处理函数, and so on... 那CPU得到指令了就把PC指过去然后,CPU调用对应的函数。(在linux中其实并没有这么用,当前的OSes 通常都使用IDT(Interrupt Descriptor Table). The IVT 只在boot的时候被使用到, because that happens in real mode)。


3. IDT(Interrupt Descriptor Table): 通常来帮processor来处理软件中断(soft interrupts or exceptions)。


开机过程:

1. 上电。

2. CPU指向ROM里面的程序load到RAM中, PC指向BIOS的地址。之后CPU开始1行1行的调用他的instruction进行开机。此时所有中断被IVT处理。

3. 多种启动方式(USB, 硬盘, 光盘 and so on), 大多数为硬盘启动。之后Load kernel, 将kernel加载到RAM里面。

4. kenel又新创建了个新玩意,叫IDT,Linux从此不再使用IVT。 相比较,IDT可以比IVT更明确的确定请求权限。

5. 此时所有内核(kernel)的内容正式进入计算机的Main Memory, 在某个阶段, kernel会创建一个pid为1的进程,这个进程为所有进程的父进程, 可以叫他创世进程。 可打开terminal用指令,"ps aux | head -n 2 | grep -i 1"找到他。

6. 启动完毕。开始干活。


(2) Trap and Interrupt (软件中断和硬件中断)


Trap/exception: 大多数存在RAM 中的 IDT表里面,我很喜欢做个比喻, 这些东西大多数可以想做前人帮你挖好的陷阱。如果你计算机运行的代码在大路上跑阿跑,突然他妈的除了点错误往歪走了,这个时候你必然掉进这个坑里面。举个例子,如果你在计算机里面运行 “x / 0 ” 就会掉进trap然后返回错误。再举个例子, 内存越界。 再再举个例子, page fault。

Interrupt: 硬件打断之前说了,os里面最常见的就是timer interrupt。

相同点:

都会引起context switch。

不同点:

1). 一个硬件打断,一个软件打断(软件打断的具体细节会在下文说)。

2). 软件中断可以被user design,但硬件不可以。并且在我开来,几乎所有的system call都属于软件中断的一种手段。

3). 硬件打断是立即打断, 比如我正处理一个instruction呢,来了个硬件打断,啪,直接就context switch了。 而软件打断通常是由某些instruction经由CPU处理后才发生的。 所以还句话来说,硬件中断就是CPU立即终止当前instruction而指向其他,软件中断是由某个instruction引起中断然后指向其他。



(3) Kernel mode and user mode(内核态和用户态)


如上文所提, 在一个正常工作计算机里的RAM主内存中, 会有一部分存着kernel的程序。 其他部分应该会有用户所正在或者最近使用的各种进程。

根据我的理解, 这个时候CPU里面的那个叫PC的屌东西指向kernel part的程序时,当前模式就是内核态, 指向用户part的时候就是用户态。kernel mode相比与user mode有更高的权限。 内核态与用户态之间的切换就是CPU 的 context switch。


权限对比: 可以用layered approach的方法来清楚的表述权限之间的关系。

(P80, Operating System 9th ed)


如上图,自行歪歪高中地理。 最中间的地方最高(layer 0),理解为权限最大。之后往下越来越小。


Context switch(上下文切换): 发生在CPU的process切换过程。 首先明确一个思想,每个process在cpu中不会一直跑下去。根据并发性(concurrent,见附注2),一个CPU在他们都有上文提到的timer给他时间限制, 时间到了CPU就会切换到另外一个进程(硬件中断)。当然这并不是唯一途径,Trap也可以导致Context switch(软件中断)。





具体步骤有3点:

1). 将旧的process的信息存到pcb中,放回ready queue(下个文章会说)。

2). 将新的process放入CPU中,从pcb读取相关信息,然后跑(下个文章会说)。

3). flush buffer(刷新缓存)

4). 清空TLB和MMU(内存部分会说)


*如何让一个user process 来进行高权限的工作?

一个法子就是System call, 比如说去年学的System programming。 所谓的System programming就是一堆前人帮咱定义好的System call,让咱舒舒服服的用不用再写。 扯远了,System call的作用就是--- 通过呼叫某个特定的函数,让pc指针指向IDT表,把所对应函数的编码存到CPU(的寄存器EAX)中。 然后PC指向内核,之后通过编码来运行内核里面对应的函数。由此我们可以看出,用户态的进程是永远进入不了内核态的。

打个比方:平民就是平民,永远进入不了政府机构。System call相当于一个信件,平民要干什么写到信件里面。 政府机构帮你作,你好好给我等着。具体情况见下面的实例。


实例:

int main(){

cout << "la la la" << endl;

open("xxx.txt", O_RDONLY);

}


首先open是个系统调用,在open之前的部分都会在user mode下直接完成。 可是open这个东西user mode无法完成,这个时候... 详细点说...

 

(P22, Operating System 9th ed)


1. pc指向open那行instruction

2. 收到INT 0x80信号, pc指向IDT寻找特定编号。

3. pc指向kernel通过编号对指令进行处理并实现。

4. 完成后, pc指回当前进程所在instruction的位置。



附注2:

并发和并行(concurrent and parallelism):

并发(concurrent): 单核单CPU无超线程的情况下,CPU在一个时间段内可运行多个进程或者线程。

并行(parallelism): 多个CPU,每个CPU在统一时间点还是处理一个进程或者线程。 但是由于是多个,所以就实现了同一个时间点处理多个进程或者线程。


以下为我的相关考前个人总结, 有点用:

I. Definition of the Operating System

                Operating System is the one program running all times on the computer, usually called the kernel. Or we can understand it as a system software that manages computer hardware and software resources and provides common services for computer programs.

               

II. Goals of the Operating System(main purpose):

                1).To provide an environment for a computer user to execute the program on computer hardware in a convenient and efficient way.

                2).Allocating the separate resources, the allocation process should be as fair and efficient as possible.

                3).As control program, it serves 2 major function:

                                                                1.Monitor the execution of user program to prevent errors and improper use.

                                                                2.Management of the operation and control of I/O devices.

III. Functions of OS:

                1).Manage the computer resource, such as the central processing unit, memory, disk drivers, and

                    printers.

                2).Establish a user interface

                3).Execute and provide services for application software.

                4).I/O operation: A running program may require I/O, which may including I/O devices or a file.

IV. Different type of OS:

                1).Batch: Jobs that similar need to be batched together and run through the computer as a group, so that the CPU and I/O devices can keep busy for the works. It is good for executing the large jobs that need little interaction.The jobs can be submitted and picked up latter. Lack: 1.interaction is low between user and job. 2. CPU often idle, because the I/O execution is slow.3.Difficulty to provide the desired priority.

                2).Time sharing:The system uses multiprogramming, and CPU scheduling to provide economical interactive use of the system. The logical extension of multiprogramming, allow multiple users located as various terminals using same particular system. Example: Unix advantage: provide quick response. avoid duplication of software. reduce CPU idle time. dis: problem of reliability.security problem. Problem of data communication.

                3).Interactive: The interactive system consist of many shorts transaction, and the next transaction is unpredictable. Response time needs to be shorter.

                4).Main frame: Got batch system, low response to users.

                5).Distributed: Computation among several processors. The processors do not share memory or clock with each other, but each processor has local memory. The communication between them used by several communication lines. ->high-speed bus, and local networks.

V. the components of Operating system(structure):

                 (1) Process management

                                1.Process creation and deletion for both user modes and kernel modes.

                                2.The synchronization of processes.

                                3.The protection of processes.

                                4.Avoid deadlock.

                                5.Communication of processes.

                (2) Memory management.

                                1.Keep track which parts of memory being used and by whom.

                                2.Memory allocated and deallocated.

                                3.Decide which process need to loaded into the memory space when memory space is available.

                (3) Secondary-storage management.

                                1.Free space management.

                                2.Storage allocation.

                                3.Disk scheduling.

       (4) File management

       (5) I/O management

                       A buffer-caching system. A general device-driver interface. Drivers for specific hardware devices

       (6) Protection and security

                       The owners of information stored in a multi-user or networked computer system may want to control use of that information, concurrent processes should not interfere with each other. Protection:all access to system recourse is controlled. Security: Outsiders requires user authentication.

       (7)Networking

       (8) Command interrupter

               Allows a cmd line directly entry the kernel or a systems program. Primarily fetches a cmd from users and executes it. Sometimes built-in, some times just the name of the programs.


原创粉丝点击