Basic knowledge of operating system user mode and core mode

Preface

This article combs the basic knowledge of the operating system and establishes an overall understanding of the operating system.
image.png

Operating system concepts, functions and goals

image.png

Operating system definition

Operating System (Operating System, OS) is to control and manage the hardware and software resources of the entire computer system, and to rationally organize and schedule the work of the computer and the allocation of resources to provide users and other software with a convenient interface and environment. It is a computer The most basic system software in the system.

Operating system functions and goals

  1. As the manager of system resources,
    CPU management, memory management, file management, and device management are
    aimed at safety and efficiency.

  2. As an interface between users and computer hardware, providing services to users

    • Command interface: allow users to use directly

      • Online command interface (interactive command interface): the user says something, the system does something
        .terminal
      • Offline command interface (batch command interface): the user talks about a bunch, the system does a bunch
        shell脚本
    • Program interface: only allow users to use indirectly through programs, composed of a set of system calls

    • GUI: Graphical user interface.

  3. As the level closest to the hardware,
    the expansion of hardware machines is realized

Operating system characteristics

image.png

Four characteristics of the operating system: concurrency, sharing, virtual, and asynchronous.
Concurrency and sharing are the two most basic characteristics, and the two are mutually existing conditions.

Concurrent

Concurrency means that two or more events occur within the same time interval. These events occurred at the same time on a macro level, but alternately occurred on a micro level.
The concurrency of the operating system refers to the simultaneous existence of multiple running programs in the computer system.
Operating system and program concurrency were born together.

shared

Sharing is resource sharing, which means that resources in the system can be used by multiple concurrently executing processes in memory.

  • There are two ways to share resources:

    • Mutually exclusive sharing mode For
      certain resources in the system, only one process is allowed to access the resource in a period of time.
    • Simultaneous sharing mode
      Certain resources in the system allow multiple processes to access them at the same time within a period of time.
      Time-sharing sharing, alternate access to the resource at the micro level.
  • Concurrency and sharing are conditions for each other.
    Concurrency refers to the simultaneous existence of multiple running programs in a computer system.
    Sharing means that the resources in the system can be used by multiple concurrently executing processes in memory.

virtual

Virtual refers to the transformation of a physical entity into several logical counterparts. The physical entity actually exists, while the logical counterpart is felt by the user.

  • Virtual technology
    • Time division multiplexing technology: such as CPU
    • Space division multiplexing technology: such as memory

asynchronous

Asynchrony is value in the case of multiple processes. Due to limited resources, the execution of the process is not consistent to the end, but stops and goes forward at an unpredictable speed. This is the asynchronous nature of the process.

Operating system and architecture

image.png

Operating mechanism

  • Instructions are the most basic commands that the CPU can recognize and execute.

  • There are two types of instructions: privileged instructions and non-privileged instructions

  • There are two CPU states: user state and core state.
    Use a certain flag in the program status word register PSW to identify what state the current processor is in. Such as 0 user state, 1 core state.
    User mode can only execute non-privileged instructions,
    core mode can execute privileged and non-privileged instructions

  • There are two kinds of programs: kernel programs and application programs.
    The kernel program is the core of the operating system and is the administrator of the system. It can execute privileged instructions or non-privileged instructions and run in the core state.
    In order to ensure the safe operation of the system, the application can only execute non-privileged instructions and run in user mode.

Operating system kernel

The kernel is the underlying software configured on the computer and is the most basic and core part of the operating system.
The programs that implement the kernel functions of the operating system are kernel programs.

  • Clock management: realize the timing function

  • Interrupt handling: responsible for implementing the interrupt mechanism

  • Primitive: It
    is a special program. It is
    at the bottom of the operating system and is the part closest to the hardware.
    This program runs atomically. It
    runs in a short time and is called frequently.

  • Function to manage system resources
    Process management
    Memory management
    Device management

Operating system architecture

  • Big kernel: The main functional modules of the operating system are taken as the system kernel and run in the core state

    • Advantages: high performance
    • Disadvantages: the kernel code is huge, the structure is chaotic, and it is difficult to maintain
  • Microkernel: keep only the most basic functions in the kernel

    • Advantages: less kernel functions, clear structure, easy maintenance
    • Disadvantages: need to switch frequently between core mode and user mode, low performance

Interrupts and exceptions

image.png

The concept and function of interruption

  • Concept: An
    interrupt is a request event initiated by an external device to the processor. The essence of an interrupt is a real-time controlled interface that the processor opens to the outside world. It is a high-priority or sudden event that needs to be responded within a certain time.
    An interruption means that the operating system needs to intervene to carry out management work.

  • Function
    When an interrupt occurs, the CPU immediately enters the core state.
    After the interrupt occurs, the currently running process is suspended, and the operating system kernel handles the interrupt.
    Different interrupt signals will be processed differently.

  • Switching between user mode and core mode The
    user mode is switched to the core mode through interrupts. And interruption is the only way.
    From the core state to the user state, the flag bit of the program status word PSW is set to the user state by executing a privileged instruction.

Classification of interrupts

Interrupts are divided into internal interrupts and external interrupts.

  • The essential difference between internal interrupt and external interrupt is whether the source of the interrupt signal comes from inside the CPU.
    Signal source of internal interrupt: CPU internal and related to the currently executed instruction.
    External interrupt signal source: CPU external and independent of the currently executed instruction

  • Internal interruption (also called exception, exception, trap)

    • Voluntary interruption: instruction interruption, such as the access control instruction used during system call (also called trap instruction, trap instruction)
    • Forced interruption
      • Hardware failure, such as page fault interrupt
      • Software interrupts, such as dividing an integer by 0
  • External interrupt

    • Peripheral request, such as interrupt signal issued by I/O operation completion
    • Manual intervention, such as the user forcibly terminating a process

External interrupt processing

Step1: After the CPU executes each instruction, it must check whether there is an external interrupt signal.
Step2: If an external interrupt signal is detected, the CPU environment of the interrupted process needs to be protected (such as program status word PSW, program counter PC, various General-purpose registers, etc.)
step3: switch to the corresponding interrupt handler according to the interrupt signal type
step4: exit the interrupt according to the CPU environment of the original process, return to the original process and continue to execute

System call

image.png

What is a system call

  • System calls are interfaces provided by the operating system to applications (programmers). It can be understood as a special function that can be called by an application program. The application program can issue a system call request to obtain the service of the operating system, and the operating system will coordinate and manage each request.

  • The application requests the services of the operating system through system calls. Various shared resources in the system are controlled by the operating system.
    Therefore, in the user program, all resource-related operations (such as storage allocation, I/O operations, file management, etc.) must be requested by the operating system through system calls, and the operating system will complete it on its behalf. This can ensure the stability and security of the system and prevent illegal operations by users.

  • System call related processing needs to execute some privileged instructions, and system call related processing needs to be performed in the core state.

  • Classification of system calls (by function)

    • Equipment management: complete equipment request/release/start functions
    • File management: complete file read/write/create/delete functions
    • Process control: complete process creation/cancellation/blocking/waking up and other functions
    • Process communication: complete the message transfer/signal transfer function before the process
    • Memory management: complete memory allocation/reclamation and other functions
  • System calls and library functions
    Some library functions are a further encapsulation of system calls;
    applications can directly make system calls or use library functions.
    Some library functions involve system calls, and some do not.

System call internal procedure

image.png

Pass system call parameters -> execute the trapped instruction (user mode) -> execute the system call corresponding service program (core mode) -> return to the user program

  • Note The
    trapped instruction is executed in the user mode. After the trapped instruction is executed, an internal interrupt is triggered immediately, so that the CPU enters the core state; the
    system call request is issued in the user mode, and the corresponding processing of the system call is performed in the core state; the
    trapped instruction It is the only instruction that can only be executed in user mode but not in core mode;

Guess you like

Origin blog.csdn.net/u014099894/article/details/112561218