Operating System Learning Notes (2) Operating Environment and Architecture of the Operating System

Continuing with the previous summary, this time let’s talk about the operating system’s operating environment and architecture.

Operating system operating environment

Operating system operating mechanism

In a computer system, the CPU usually executes two programs of different natures, one is the operating system kernel program, and the other is the user-written program (that is, the application program of the outer layer of the system, also known as the application program).

For the operating system, these two programs play different roles. The former is the manager of the latter, so the hypervisor has to execute some privileged instructions, while the latter cannot execute these instructions for security reasons.

The so-called privileged instruction refers to the instruction that the user is not allowed to use directly in the computer, such as I/O instructions, interrupt instructions, access registers for memory protection, and instructions that send program status words to program status word registers.

In terms of specific implementation, the state of the CPU is divided into User Mode and Kernel Mode.

Note that ** User Mode and nuclear mentality are the states of the CPU, which are privileged instructions that can run operating system kernel programs when the CPU is in the nuclear mentality. ** It can be understood that there is a switch inside the CPU. When the switch is 1, the CPU is in the nuclear mentality., privileged instructions can be executed, but not when it is 0.

The kernel of the operating system is divided into two parts:

  • Some modules closely related to hardware, such as clock management, interrupt handling, device drivers are at the bottom.
  • Followed by programs that run more frequently, such as Process Management, Memory Management, and Facility Management.

Different systems have slightly different definitions of the kernel, which can be roughly divided into four types

1.

Among the various components of a computer, the clock is the most critical device.

The first function of the clock is timing, providing users with accurate system time.

In addition, process switching can be realized through the processing of clock interrupts. For example, time slice rotation scheduling is adopted in a time-sharing operating system, running according to deadline control in a real-time system, and the running degree of a job is measured by clock management in batch processing.

2.

** The original intention of introducing interrupts is to improve CPU utilization in multi-program running environments, and it is mainly aimed at external devices. **

Later, it gradually developed and formed a variety of types, which became the basis of various operations of the operating system. For example, the information input of the mouse or keyboard, the management and scheduling of processes, the call of system functions, device drivers, file access, etc., all rely on interrupt mechanisms. It can be said that modern operating systems are software driven by interrupts.

Interrupt mechanism, ** only a small part of the kernel **, they are responsible for protecting and restoring the interrupt site, transfer control to the relevant processing procedures, etc., which can reduce the processing time of interrupts and improve the ability of parallel processing of the system.

An interrupt, as the name suggests, is to suspend the operation of something, it is not only caused by an exception, such as an interrupt after A’s I/O is completed, pausing the execution of B’s program in the current CPU.

3.

Common Mini Programs that can be called at the bottom of the operating system and have the following characteristics:

  • The lowest level of the operating system, the part closest to the hardware.
  • Atomic, the operation can only be done in one go.
  • Short running time and frequent calls.

The straightforward way to define a primitive is to close the interrupt (that is, prohibit other interrupts), and then open the end point after all actions are indivisibly completed.

Some operations in functions such as device drivers, CPU switching, and process communication in the system can all be defined as primitives.

That is to say, the primitive belongs to the operating system, and its implementation and guarantee are software-level.

4.

There are many data structures used to register status information in the system, such as job control block, process control block (PCB), device control block, linked list, message queue, buffer, free area memory registration table, etc. In order to achieve effective management, the system requires some basic operations, such as:
Process management: process state management, process scheduling and dispatching, creating and undoing process control blocks, etc.

  • Memory management: allocation and reclamation of memory space, memory information protection program, code exchange program.
  • Facility Management: buffer management, equipment allocation and recycling.

Interrupts and exceptions

The system does not allow user programs to implement the functions of the nuclear mentality, but they must use these functions. Therefore, some “doors” need to be provided on the nuclear mentality in order to achieve from User Mode to the nuclear mentality.

In the actual operating system, when the CPU runs the upper-layer program, the only way to enter these doors is an interrupt or exception. Once an interrupt or exception occurs, the CPU running User Mode will immediately enter the nuclear mentality, which is achieved through hardware.

The process of operating system development is generally a process of trying to improve resource utilization, and to improve resource utilization, it is necessary to release its possession of that resource when the program does not use a certain resource, which requires interruption.

Definition of Interrupts and Exceptions

Interrupt, also known as external interrupt, refers to the occurrence of events other than the instructions currently executed by the CPU. For example, the I/O end interrupt issued by the device indicates that the input and output of the device has been completed, and it is hoped that the processor can issue the next input and output instruction to the device., and then the program continues to execute after the input and output is completed. Clock interrupt indicates that a fixed time slice has arrived, allowing the processor to process timing and start timing tasks.

Exceptions, also known as internal interrupts, exceptions, or traps, refer to events that come from within the instructions being executed by the CPU, such as address out-of-bounds, arithmetic overflows, memory page defects, etc. The handling of exceptions generally depends on the running site of the current program, and exceptions cannot be masked. Once they occur, they should be dealt with immediately.

Interrupt handling procedure

The processing of interrupts (here referred to as external interrupts) on different computers has its own characteristics, but it is probably the same:

  1. Turn off the interrupt. After the CPU responds to the interrupt, it must first protect the scene of the program. In the process of protecting the scene, the CPU cannot respond to other interrupts, otherwise the scene cannot be preserved intact.
  2. Save breakpoints. In order to ensure that the interrupt service program can return to the original program correctly after execution, the endpoint of the original program (program counter PC) must be saved. (Note that this step is automatically completed by hardware)
  3. Interrupt service routine addressing. The essence is to find the entry of the interrupt service routine and send it to the program counter. This entry address is also called the interrupt vector.
  4. Save the scene and shield words. After entering the interrupt service program, the first thing to save the scene, the scene information generally refers to the program status word register PSWR and some general purpose register (by the operating system).
    Open interrupt. Allows higher level interrupt requests to be responded to.
  5. Execute the interrupt service routine. This is the purpose of the interrupt. But note that it has already been opened in the short, that is, it can be interrupted here.
  6. Off interruption. Ensure that the scene is not interrupted when resuming and masking words.
  7. Restore the scene and shielded words. Restore the scene and shielded words to their original state.
  8. Open interrupt, interrupt return. Return to the breakpoint of the source program.

There are a few points to note in this process.

There is a primitive before and after executing the interrupt service routine, but the interrupt service routine itself is not entirely in the primitive, so it can theoretically be interrupted. If the interrupt itself is interrupted again, a stack is needed to store the scene layer by layer.

However, note that this stack is different from the call stack of the program’s internal function, which is simulated and managed by the program itself.

Interrupts are bound to fall into Kernel Mode because they involve operations such as saving program counters.

The purpose of an interrupt is to interrupt the program currently being executed by the CPU through a series of operations and go to execute the interrupt service program, while the purpose of a primitive is that some instructions cannot be interrupted. The interrupt processing process contains at least two primitives, that is to say, there are at least two primitives that cannot be interrupted in the interrupt processing process.

System call

The so-called system call refers to the user calling some subfunctions provided by the system in the program, and the system call can be regarded as a special public subroutine.

All kinds of shared resources in the system are uniformly managed by the operating system. Therefore, in the user program, all operations related to resources must make service requests through system calls, which are completed by the operating system on its behalf.

Note the content of the previous blog post: a program interface consists of a series of system calls.

System calls are divided into several categories by function.

  • Facility Management
  • Document management
  • Process control
  • Process communication
  • Memory management

System call related functions involve system resource management, process management, etc., which have a great impact on the entire operating system and need to run in the core mentality. Users can execute trap instructions and system calls.

The user executes the trap instruction, which is equivalent to giving the CPU usage right to the operating system kernel, and then the kernel program makes relevant processing on the system call, and then returns the CPU usage right to the user program after completion.