"Operating System" - the operating environment of the operating system

1. The operating mechanism of the operating system

1.1 Basic concepts

Kernel programs and applications

  • Applications: For example, functions such as weather and email that come with the installed system exist in the user space. In addition, some programs we installed ourselves also exist in the user space, in a word: 非必要的应用,可卸载的应用,我们都可以称为应用程序.
  • Kernel program: Managed by the operating system kernel, such as process manager, process scheduling, etc., all of which exist in the kernel space, which we call it 内核程序.

How does the computer distinguish between the kernel program and the application program?

Regarding the kernel program, we can also call it 核心态or 管态, and about the application program, we can call it 用户态or 目态, in fact, it is distinguished by an identifier, when the instruction to run the application program is 0, and then the instruction to run the kernel program is 1 .

Privileged and Unprivileged Instructions

The instructions related to running the kernel are privileged instructions, and the instructions related to the application program are unprivileged instructions.

The difference between code and instructions

  • Code: We can understand it, but the computer CPU cannot. But it can be compiled by some programs to convert it into instructions.
  • Instructions: We cannot understand them, but the computer CPU can understand them and execute the corresponding operations in the instructions.

1.2 Clock Management

The system clock is actually a server that maintains time and time services in the operating system. The system clock is the only local time source in the operating system, which enables the system to accurately trigger scheduling or execution principles when performing some periodic tasks, such as logging, system performance measurement, background tasks, etc.

Clock management has two main functions:

1.3 Interrupt Mechanism

After the CPU finishes running a program, it is ready to output the results. It directly interrupts the CPU and lets it switch directly to another program. After the IO device of the previous program is finished, switch back. This is achieved through the interrupt mechanism.

The interrupt mechanism is divided into internal and external interrupts:

  • External interrupt: Passive interrupt, for example, multiple applications are waiting for calculation, each program may run for a short while, and then be forced to interrupt to continue the next calculation.
  • Internal interrupt: interrupted by instructions.

1.3.1 There are three cases of internal interrupt

(1) Trapped: actively triggered by the application

Take the following figure as an example. After running a program, the program continuously sends instructions to the CPU. Suddenly, a command to read a file comes. Reading a file belongs to a kernel instruction. At this time, the CPU will generate a trap instruction, and then execute the privileged instruction. .

(2) Fault: Caused by an error condition

The following figure is an example. A very large game program is running. Generally, the game is several g. It is impossible to load all the disk files into the memory. It will only load a part of it into the memory. If the memory is found to be insufficient during execution file, and then a fault interrupt occurs. Wait for the disk file to be read into the memory before continuing to execute the following instructions. Note that the following instructions referred to here are not necessarily privileged instructions.

(3) Terminated: Raised by a fatal error

Address out-of-bounds, arithmetic overflow, illegal access to privileged instructions, etc. These relatively serious CPUs cannot handle them at all and are interrupted directly.

The difference between a trap and a failure:

  • Trapped is intentional, failure is unintentional
  • The trap is that the instruction requires to read a file, and the fault is that it wants to read a file, but the file has not been loaded into the memory, so stop and load the file first.

1.3.2 Interrupt processing

The interrupt referred to here is neither an internal interrupt nor an external interrupt, but the processing of all interrupts.

The off and on interrupts here actually refer to whether the program is terminated:

  • Turning off the interrupt is the execution of the program termination instruction.
  • Opening an interrupt is to continue the execution of program instructions.

In fact, the whole process is mainly divided into three steps:

  1. Save the position of the executed instruction, which is actually saved in the register of the computer
  2. Then start the execution interrupt program
  3. Finally, restore to the position of the instruction just executed through the register

1.3.3 Breakpoint debugging

The breakpoint in the program is actually implemented based on the trapping instruction in the interrupt instruction of the operating system! INT3Breakpoint instructions are specially designed to support debugging instructions. Its purpose is to interrupt the CPU to the debugger for the debugger to perform various analyzes on the execution site. When we are debugging software, we can insert instructions somewhere INT3, and when the cpu executes to this breakpoint, it will pause.

insert image description here
When you click Debug to run, the following dialog box is generated:

insert image description here
Open the disassembly window, as shown below:

insert image description here
It is found that there is an int 3 instruction at the location 00921AF9 of the interrupt.

Open the register window and find that the value of EIP is also 00921AF9, as shown below:

insert image description here

This case is actually not complete. It can only be said that through this case, we can know that what we understand now is not only conceptual knowledge, but actually exists, but it may be a little bit lower.

1.4 Primitives

Control of computer processes is usually 原语done by . The so-called primitives generally mean 由若干条指令组成的程序段that they are used to implement a specific function and cannot be interrupted during execution. In the operating system, some operations called by the process, such as queue operations, operations on semaphores, check and start peripheral operations, etc., cannot be interrupted once they start to execute, otherwise operation errors will occur, causing system confusion. Therefore, these operations must be implemented with primitives. Primitives are an integral part of the core of the operating system (not a process, but a set of program modules), and reside in memory, and are usually executed in managed mode.原语一旦开始执行,就要连续执行完,不允许中断。

1.5 System Data Structure

1.6 System calls

A collection of all system calls provided by the operating system implementation is a program interface or an application programming interface (Application Programming Interface, API). It is the interface between the application program and the system.

The user program only runs in the user state, and sometimes needs to access the core functions of the system. At this time, the system call is used through the system call interface.

Second, the operating system architecture

  • From the user's point of view, the operating system embodies the various services it provides;
  • From the programmer's point of view, the operating system embodies the interface and interface provided to the user;
  • From a designer's point of view, an operating system is a collection of modules and their interconnections.

This is the architecture of the operating system.

2.1 Unstructured Operating System

The basic units that make up the operating system are usually called components, and the basic operating system components include kernels, processes, threads, and monitors. The traditional operating system structure (big kernel) can understand it as a call between functions, which is disorganized.

2.2 Modular structure system

The modular structure mainly provides services to the user program of the outer layer through an API mechanism called a system call. The whole kernel adopts modular design, and the modules communicate with each other by means of interfaces.

2.3 Hierarchical structure system

The kernel system consists of several layers, the bottom layer is bare metal hardware, and the top layer is application services.

The calling relationship between layers strictly follows the calling rules, top-down rules, and each layer can only access the services provided by its lower layer.

2.4 Microkernel OS structure

Also known as client/server architecture. It removes as many things as possible from the operating system kernel, leaving only a small kernel that 由用户进程implements most of the operating system's functions.

The operating system is divided into multiple parts, and each part only deals with one aspect of functions, such as file service, process service or memory service, etc. Each part is small and easy to manage. All services run in the form of user processes, not in the kernel mode, so they do not directly access the hardware. Regarding this point, we can open the task manager to have a look. Our computer did not open any reference programs when it was first started, and it will start many basic processes.

We may not understand others, but this Bluetooth should know what it is.

Cons: One more layer of application. In the past, it was possible to read a file directly by calling the instruction of the kernel program through the application program, but now it is necessary to call the file server provided by the operating system, and the file server calls the kernel instruction.

3. Summary

insert image description here

Reference: "Horse Soldier Operating System Explanation" https://www.bilibili.com/video/BV1Ha411e7D4/

Guess you like

Origin blog.csdn.net/weixin_43888891/article/details/131525173