Tsinghua Operating System Course Study Notes 1

Recently, I felt that the foundation of my operating system was not solid enough (all the content I had learned was returned to the teacher), so I went back and learned it again. This series is also some notes I made during the learning process. I mainly learned from the operating system video course from Tsinghua University. I have to sigh with emotion here. Station B is really a magical place. Someone has sorted out the videos of this course:

https://www.bilibili.com/video/av6538245 (mainly explaining concepts and principles)

https://www.bilibili.com/video/av10496140 (with more detailed experimental explanations)

The above two videos are actually the same class, but different teachers are teaching.

Experimental environment: https://www.shiyanlou.com/courses/221

Because I was trying to supplement my lack of basic knowledge, I didn't do the experiment.

In addition, in the process of writing notes, I also moved the content of some other blogs, and some places did not indicate the source. If the original author thinks that there are any questions, you can contact me.


Chapter 1 Course Overview

1. What is an operating system

From the user and control point of view, the operating system is a control software that manages applications, provides services to applications, and kills applications. From the perspective of resource allocation, the operating system manages resources, manages various peripherals, and allocates resources. The operating system abstracts material resources: the CPU is abstracted into a process, the disk is abstracted into a file, and the memory is abstracted into an address space for application use.

2. Operating system hierarchy

Above the hardware, below the application. It is a middle-tier system software.

The operating system is located under the application software, provides service support for the application software, and completes the management of the hardware.

The operating system has two layers of external interfaces: the externally exposed interface shell (Shell), and the internal-facing kernel (Kernel).

3. The kernel of the operating system

Kernel - internal components of the operating system, including: CPU scheduling, physical memory management, virtual memory management (providing relatively independent and as large memory as possible to upper-layer applications), file system management (storing and accessing permanently saved data), interrupt handling and Device drivers (to deal directly with underlying devices), etc.

4. Characteristics of the operating system (OS Kernel)

(1) Concurrency. There are multiple running programs in a computer system at the same time, which requires OS management and scheduling. Note the distinction between concurrency and parallelism. Concurrency refers to having multiple programs running at a time. Parallelism refers to the fact that multiple programs can execute simultaneously at one point in time. The difference between "a period of time" and "a point in time". To achieve parallelism, multiple CPUs are generally required. A single-core CPU cannot achieve parallelism.

(2) Sharing. Macroscopically, multiple applications can access resources such as memory and I/O at the same time, but they may be mutually exclusive internally (at the same point in time, some resources can only be used by one program).

(3) Virtual. Using multiprogramming technology, a physical machine is virtualized into multiple virtual machines. Make each user feel that there is a computer dedicated to him.

(4) Asynchronous. The execution of the program is not consistent to the end, but stop-and-go, and the speed of advancing is unpredictable and depends on the management of the system. But as long as the operating environment is the same, the OS needs to ensure that the results of the program running are also the same.

5. The evolution of the structure of the operating system

(1) Simple operating system: MS-DOS (1981-1994) unblocked monolithic kernel

(2) The design of the microkernel moves the functions of the kernel to the user space as much as possible. The services of the application interact in a loosely coupled manner through the message passing mechanism. Industry uses it less.

(3) VMM, virtual machine monitor. From OS-Hardware to OS-VMM-Hardware. Multiple operating systems share hardware resources.

6. Summary

Course overview, what is an operating system, why to learn an operating system, operating system examples, the evolution of operating systems, the structure of operating systems.

Chapter 2 Startup, Interrupt, Exception, System Call

1. Start

The operating system was initially placed in DISK (hard disk), not in memory.

BIOS: Basic I/O processing system. A part of the memory (fixed) is used to store the BIOS.

Take X86 as an example:

1. When the X86 PC is powered on, the CPU is in real mode. At this time, the memory calculation method is segment base address <<4 + segment offset;

2. The first instruction of the CPU is obtained through CS:IP, and at this time CS=0xFFFF, IP=0x0000. This is set by the hardware.

3. Therefore, the address of the first executed instruction is 0xFFFF0, which is a jump instruction, which is transferred to the real startup code in the system BIOS: the BIOS ROM (read-only memory area) of the motherboard.

4. The startup code of the system BIOS first performs a power-on self-test (POST, Power-On Self Test) to detect whether some key devices in the system exist and work normally, such as memory and graphics cards.

5. Find the BIOS of the graphics card.

6. Load the BootLoader into memory. BootLoader is generally placed in the first main boot sector of the hard disk, which is the 0 track 0 sector of the disk (usually 512 bytes). For example, in X86, the BIOS loads the BootLoader at 0x7cdd.

7. After that, BootLoader loads the code and data of the operating system from the hard disk into the memory.

After completing the above operations, jump to the starting address of the operating system, and hand over the hardware management of the computer system to the OS.


Second, the program interaction between the operating system and the device

Why can't applications access peripherals directly, but must go through the operating system?

(1) When the computer is running, the kernel is a trusted third party, but the application is not. If it directly accesses the peripherals, it may bring security problems.

(2) Only the kernel can execute privileged instructions.

(3) In order to facilitate the application, a concise interface is provided for the application.

1. System call

Definition: An application actively sends a service request to the operating system, which originates from the application. Asynchronous or synchronous. The application knows when it will make a system call request, so making the request is synchronous, but the application does not know when the operating system will return a result, so it is asynchronous. The application needs to wait for the response of the system call before it can continue execution.

2. Abnormal

Definition: Illegal instructions or other bad processing states (events from bad application operations), such as memory errors, etc. It is a synchronous event, which means that the application can know when this event will happen, such as dividing by 0, an exception will be generated. Response status: Kill or re-execute unexpected application instruction.

3. Interrupt

Definition: timers and network interrupts from different hardware devices (from peripherals, keyboard, mouse, network card, etc., generating various events). is an asynchronous event, the application does not know when this will happen. Response Status: Interrupts are transparent to the application.

 

3. Interrupt, exception, system call processing mechanism

1. Interrupt

Each interrupt has a specific number (for example, different peripherals will have different numbers). After receiving the interrupt, the CPU accesses the interrupt table, finds the address of the corresponding interrupt service routine in the table, and goes to the corresponding address. implement.

Specifically, it is implemented by hardware and software.

hardware:

(1) Set the interrupt flag [CPU initialization]. The peripheral generates a flag, which is sent to the CPU. The CPU generates an interrupt number according to this flag and sends the interrupt number to the operating system.

software:

(1) The operating system saves the current processing state (the address of the currently executed instruction and the contents of some current registers).

(2) The operating system accesses the interrupt service routine and processes the interrupt according to the interrupt number given by the CPU.

(3) After processing, clear the interrupt flag.

(4) Restore the previously saved processing state.

Therefore, the above process is transparent to the application.

2. Abnormal

When the application executes a specific instruction, an exception is triggered, and the CPU gets the exception ID number. First save the scene, and then process according to the exception ID number. It may kill the application, or it may make up for it, restore the scene, and execute it again.

3. System call

The application program requests the services of the operating system through the interface. For example, in the standard C library, the application calls printf(), which triggers the system call write(). When the operating system completes the request, it returns success or failure, allowing the application to proceed with further work. Application access is mainly through high-level API interfaces, rather than direct system calls. For example, the Win32 API is used for Windows, and the POSIX API is used for POSIX-based systems, including all versions of Linux, Unix, and Mac OS X. Java API is used for Java Virtual Machine etc.

Implementation of system calls: The application program directly or indirectly accesses the system call interface through a library (library). Once accessed, it will trigger the transition of the operating system from user mode to kernel mode, and the control right is handed over from the application program to the operating system. The operating system recognizes system calls and performs required services.

User mode: The privilege level executed by the CPU is relatively low, and it cannot directly access some special machine instructions and I/O.

Kernel mode: The CPU can execute any instruction, including special machine instructions and I/O. At this point the operating system has complete control over the entire computer system.

The difference between a system call and a function call:

(1) When the application program issues a function call, it performs parameter transfer between functions and parameter return in a stack space.

(2) When a system call is issued, the application and the operating system have their own stacks. When the application issues a system call, it needs to switch the stack and trigger the transition of the operating system from user mode to kernel mode. Therefore, the overhead of system calls is much larger than function calls. The advantage of system calls is that they are safe and reliable.

 

4. The overhead of crossing operating system boundaries

The above-mentioned interrupts, exceptions, and system calls cross the boundaries of the operating system. There is a certain price, and this price is to ensure that the system can operate safely and reliably. The overhead in execution time exceeds that of program calls (mentioned earlier).

Overhead:

(1) The initialization overhead for establishing the mapping relationship between interrupts, exceptions, system call numbers and corresponding service routines. This table needs to be built before the operating system works properly.

(2) Maintain the operating system's own kernel stack. This kernel stack is established before the operating system starts to work, saved when the operating system exits, and restored when it is executed. The saving and restoration of the application stack is therefore also maintained.

(3) Verify parameters. The operating system does not trust the application and needs to check the system call parameters (time overhead for security).

(4) The kernel mode is mapped to the address space of the user mode.

(5) Kernel state independent address space TLB.

These overheads are worthwhile, and necessary, to ensure that the operating system executes in a safe and secure environment.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325528799&siteId=291194637