Compilation of computer operating system knowledge - Overview of computer operating systems (Introduction Guide to Computer Operating Systems)

Chapter 1 Overview of Computer Operating Systems

1.1 Basic concepts of operating systems

1.1.1 Introduction to operating systems

In today's information age, I believe that all readers are familiar with the four words "operating system". For example, Windows, Android, IOS, MacOS and Linux that we often use are all operating systems. The operating system is an indispensable piece of software in today's information age. For ordinary users, the four words operating system are both familiar and unfamiliar, because ordinary users only pay attention to the application software that runs on each operating system. In fact, I don’t know much about the operating system that assists these application software in operating hardware resources.

So what exactly is an operating system?

Here, we might as well look at the structure diagram of a computer system.

Insert image description here

As can be seen from the above figure, the operating system is a piece of software between the application program and the computer hardware. The operating system and the computer hardware interact directly, while the application software and the user cannot directly interact with the computer hardware.

Note: In the above figure, there is also a direct connection between the user and the operating system. This is because the user can directly interact with the operating system through the online command interface provided by the operating system. This part will be explained in detail in section 1.1.2.

To sum up, we give the standard definition of operating system as follows:

An operating system refers to a collection of programs that controls and manages the hardware and software resources of the entire computer system , rationally organizes and schedules computer work and resource allocation, and provides convenient interfaces and environments for users and other software . The operating system is a set of programs in a computer system. The most basic system software .

Section summary

Insert image description here

1.1.2 Functions of the operating system

The operating system mainly provides three major functions:

  • Act as a manager of computer system resources
  • Serves as an interface between users and computer hardware systems
  • As an expansion of computer resources
1.1.2.1 As a manager of computer system resources

As the manager of computer system resources, the operating system has the functions of processor management, memory management, device management and file management.

  • Processor management

    In a multiprogramming environment, the allocation and operation of processors are based on processes (or threads) as the basic unit. Therefore, the management of processors can also be attributed to the management of processes. The main functions of process management include process control, process Synchronization, process communication, deadlock handling, processor scheduling, etc. (These functions will be introduced in detail in later chapters)

  • memory management

    Memory management is to provide a good environment for the operation of multi-programs, facilitate user use and improve memory utilization. It mainly includes memory allocation and recycling, address mapping, memory protection and sharing, content expansion and other functions.

  • Device management

    The main task of device management is to complete user I/O requests, facilitate users to use various devices, and improve device utilization. It mainly includes functions such as buffer management, device allocation, device processing, and virtual devices.

  • File management

    Information in the computer exists in the form of files. The part of the operating system responsible for file management is called the file system. File management includes file storage space management, directory management, and file read and write management and protection.

1.1.2.2 As an interface between users and computer hardware systems

The operating system provides a user interface so that users can conveniently, quickly and reliably manipulate computer hardware and run their own programs. There are two types of user interfaces provided by the operating system: command interface and program interface . Users can directly use the command interface to interact with the operating system to complete job execution. Program interfaces are programmed by programmers to use these interfaces to request services provided by the operating system.

Here, the operating system uses the idea of ​​encapsulation to encapsulate some complex hardware functions into simple and easy-to-use services, so that users can use the computer conveniently without caring about the underlying hardware principles and only need to issue commands to the operating system. .

Insert image description here

The above figure shows that the application software obtains the functions provided by an operating system through the interface provided by the operating system, thereby manipulating the computer hardware.

  • command interface

    Command interfaces are divided into two categories: online command interface and offline command interface

    Online command interface

    The online command interface, also known as the interactive command interface , is an interface suitable for time-sharing or real-time systems. It consists of a set of keyboard operation commands. Users input operation commands through the console or terminal to put forward various service requirements to the system. Every time the user enters a command, control will be transferred to the command interpreter of the operating system, and then the command interpreter will interpret and execute the entered command to complete the specified function. Then, control will be transferred back to the console or terminal. At this time The user can then enter the next command. That is: the user's interaction with the operating system using the online command interface is carried out alternately.

    For example, in a Windows system, press Win+R on the keyboard, enter cmd and press Enter to enter the command interpreter and use commands provided by various operating systems to interact with the operating system (such as the time command to obtain the current time).

    Offline command interface

    The offline command interface, also known as the batch command interface , is suitable for batch processing systems. It consists of a set of job control commands. Offline users cannot directly intervene in the running of the job. Instead, they write a corresponding series of control commands in advance. The job operating instructions are submitted to the operating system together with the job. When the system schedules the job, the command interpreter in the system executes the commands in the job description one by one, thereby indirectly controlling the operation of the job. That is, the user uses the offline command interface to interact with the operating system by providing a job list in advance and then handing it over to the system to execute it one by one according to the list.

    For example, bat batch file in Windows

  • Program interface

    The program interface consists of a set of system calls (also called generalized instructions). Programmers use these system calls in their programs to request certain services provided by the operating system, such as various external devices, allocate and reclaim memory, etc.

    The figure below is an example of a programmer using a program interface to request the operating system to provide display services for the monitor.

Insert image description here

The most popular graphical user interface (GUI) at present, that is, the graphical interface, is implemented by calling the program interface. Through the GUI, users can directly use the mouse and keyboard to conveniently use the operating system.

1.1.2.3 As an expansion of computer resources

Bare metal (a computer without any software) is the material basis of a computer system, and the computer system presented to the user is a computer transformed by several layers of software. The bare metal is the innermost layer, and the outer layer is the operating system. The operating system provides The resource management functions and various user-friendly service interfaces make the bare metal more powerful and convenient to use. Therefore, we often refer to machines covered by software as extended machines or virtual machines.

Section summary

Insert image description here

1.1.3 Characteristics of operating systems

The operating system is a kind of system software, which is very different from other system software and application software. It has some basic characteristics unique to it. Includes concurrent, shared, virtual and asynchronous . Understanding these concepts is very important! ! !

1.1.3.1 Concurrency

Concurrency is when two or more events occur within the same time interval . These events occur simultaneously on a macro scale and alternately on a micro scale . In the operating system, concurrency refers to the existence of multiple running programs in the computer system at the same time. The operating system has the ability to process and schedule the simultaneous execution of multiple programs. In a multi-programming environment, within a period of time, macroscopically, multiple programs are executed at the same time. However, at each moment, only one program can be executed in a single-processor environment. Therefore, microscopically, these programs are still executed alternately in a time-sharing manner. , that is, the concurrency of the operating system is achieved through time sharing.

Note: Concurrency and parallelism are two different concepts. Parallelism means that two or more events occur at the same time .

Parallelism means that the system has the characteristic of performing calculations or operations at the same time, and can complete two or more tasks at the same time. Parallelism requires sufficient hardware support, such as a multi-processor hardware environment.

A single-core CPU can only execute one program at a time, and each program can only be executed concurrently.

Multi-core CPUs can execute multiple programs at the same time, and each program can be executed in parallel.

The following figure illustrates the difference between concurrency and parallelism with an example

Insert image description here

In the picture above, if Xiao Ming follows the process on the left to write homework, listen to music, and write homework one after another, it means that the two behaviors of writing homework and listening to music are executed concurrently from 10:00 to 12:00. If Xiao Ming follows the process on the right and writes homework while listening to music from 10:00 to 11:00, then the two behaviors of writing homework and listening to music are executed in parallel.

1.1.3.2 Sharing

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

Resource sharing is divided into two types: mutually exclusive sharing and simultaneous access.

  • Mutually exclusive sharing mode

    Although some resources in the system (such as printers, etc.) can be used by multiple processes, in order to prevent confusion in the printing results, it should be specified that only one process is allowed to access the resource within a period of time.

    Therefore, resources that use mutually exclusive sharing can only be accessed by one process at the same time. For example, if process A wants to access a certain resource, it needs to make a request first. If the resource is idle at this time, the system will The resource is allocated to process A. If process B also wants to access the resource at this time, process B must wait for process A to release the resource after it has finished using it. This method is mutually exclusive sharing .

  • Simultaneous access method

    Some resources in the system (such as disks, etc.) allow multiple processes to access "simultaneously" within a period of time. The "simultaneity" mentioned here is macroscopic. That is, these resources allow concurrent access by multiple processes. For example, multiple processes can concurrently access a disk device and read a file.

Concurrency and sharing are the two most basic features of the operating system . The relationship between the two is a condition for the existence of each other. If the system does not allow concurrent execution of programs, then there will naturally be no problem of resource sharing; if the system cannot implement resource sharing Effective management will affect the concurrent execution of the program.

1.1.3.3 Virtual

Virtualization refers to turning a physical entity into several logical counterparts. Physical entities actually exist, and their logical counterparts do not exist. They are things that users feel.

In the operating system, virtual technology is used to implement functions such as virtual processors , virtual memory , and virtual peripheral devices .

  • virtual processor

    The virtual processor uses multi-programming technology to execute multiple programs concurrently and use one processor in a time-sharing manner. At this time, although there is only one processor, from a macro perspective, it can serve multiple users at the same time, making each end user feel that there is a CPU specifically serving it.

  • Virtual Memory

    Virtual memory converts the actual physical memory of a computer into virtual memory to logically expand the memory capacity. At this time, the memory capacity perceived by the user is virtual.

  • virtual peripheral device

    Virtual external devices use virtual technology to virtualize a physical I/O device into multiple logical I/O devices, allowing each user to occupy one logical I/O device, making the original only allowed for a period of time. A device that is accessed by one user within a period of time becomes a shared device that allows multiple users to access simultaneously for a period of time.

The virtual technology of the operating system can be summarized as: time division multiplexing technology (virtual processor) and space division multiplexing technology (virtual memory).

The prerequisite for the virtual technology of the operating system is concurrency. If there is no concurrency, only one process can be run in the system for a period of time, then virtuality will lose its meaning, that is, without concurrency, there will be no virtuality .

1.1.3.4 Asynchronous

In a multi-programming environment of the operating system, since multiple programs are executed concurrently, the execution of each process is not consistent, but stop-and-go, and each process moves forward at an unpredictable speed. Push forward, this is the asynchronicity of the process.

Since concurrently running processes compete for limited system resources, the execution of processes is not consistent. Only when the system has concurrency can it lead to asynchrony.

Section summary

Insert image description here

1.2 Development History of Operating Systems

1.2.1 Manual operation stage (without operating system)

In the initial manual operation stage, every work using the computer requires manual intervention, such as loading and running of programs, output of results, etc. The figure below shows the normal flow of the manual phase.

Insert image description here

As can be seen from the above figure, the speed of computer calculation is very fast compared to other manual intervention speeds in the entire process. Throughout the process, it often happens that the computer has already completed the calculation, but the human is still slowly inputting the next program or reading the results of the operation.

It can be seen that the manual operation stage has two main disadvantages:

  • Users monopolize the entire machine, resulting in low resource utilization
  • CPU waits for manual operations and is underutilized

1.2.2 Batch processing stage (operating system begins to appear)

To solve two significant problems in the manual phase, batch processing systems emerged. Batch processing systems are divided into single-channel batch processing systems and multi-channel batch processing systems .

1.2.2.1 Single-channel batch processing system

In the single-channel batch processing system, offline input/output technology ( peripheral machine + tape ) is introduced , and the supervisory program is responsible for controlling the input and output of the job, which solves the conflict between man and machine and the mismatch in the speed of CPU and I/O equipment. question.

Insert image description here

  • Features

    • Automation: Under normal circumstances, a batch of jobs on the tape can be run automatically one by one without manual intervention.

    • Sequential, the jobs on the tape are entered into the memory and executed one by one in order.

    • Single-track, the supervisory program only transfers one program from the tape into the memory to run.

  • advantage

    To a certain extent, it solves the waste of resources caused by the conflict between man and machine.

  • shortcoming

    Due to the single-channel nature of a single-channel batch processing system, if an I/O request is issued while the program is running, the high-speed CPU needs to wait for the low-speed I/O device to complete before it can continue to run. This results in a waste of CPU resources.

1.2.2.2 Multi-channel batch processing system

In order to solve the shortcomings of the single-channel batch processing system, the multi-channel batch processing system came into being. It uses multi-programming technology to allow multiple programs to enter the memory at the same time and allow them to run alternately in the CPU. In this way, even if there is a certain Program A requests an I/O device while it is running. The CPU can also immediately operate other programs, and then continue to operate program A after the I/O device has completed processing.

Insert image description here

  • Features

    Multi-programming means that multiple programs are running simultaneously in the system's memory.

    Parallel at the macro level and serial at the micro level . At the macro level, all programs in the memory are running. At the micro level, only one program occupies the CPU at the same time.

  • advantage

    Multiple programs execute concurrently and share computer resources, making the CPU and other resources fully "busy" and greatly improving resource utilization.

  • shortcoming

    The user response time is long and there is no human-computer interaction function (when the user submits a job, he can only wait for the system to automatically process it and cannot intervene manually).

1.2.3 Time-sharing operating system

The time-sharing operating system uses time-sharing technology to divide the running time of the processor into short time slices, and allocates the processor to each online job in turn according to the time slice . Each user can use the time-sharing operating system through the terminal. Computers interact, if a job A cannot be completed within a time slice, the job A will be suspended, and the CPU will process other jobs and wait for the next round before continuing to run job A. Because the CPU is very fast, the job rotation is also very fast, making each user feel that there is a dedicated CPU serving them.

  • Features

    Simultaneity, in a time-sharing operating system, allows multiple end users to use a computer at the same time. Users on these terminals can use the computer at the same time or substantially simultaneously.

    Interactive, users can have human-machine dialogue with the system and control the operation of the job.

    Independence, the operations between multiple users in a time-sharing system are independent of each other and do not interfere with each other.

    Timeliness, user requests can be responded to in a short time.

  • advantage

    The time-sharing system solves the problem of inability of human-computer interaction in multi-channel batch processing systems. It allows multiple users to connect to a host through the terminal at the same time, and the operations of each user are independent of each other and cannot be felt by other users.

  • shortcoming

    Since the time-sharing system rotates time slices to execute each program, this results in the time-sharing system being unable to prioritize some urgent matters. In some scenarios that require the system to process external information within a specified time, the time-sharing system The system is not applicable (for example, aircraft booking, aircraft real-time monitoring and other systems).

1.2.4 Real-time operating system

In order to solve the problem that the time-sharing system cannot handle urgent tasks, the real-time system came into being. Real-time systems are divided into two types, hard real-time systems and soft real-time systems .

  • hard real-time system

    Hard real-time systems require that an action must absolutely occur at a specified time or within a specified time range. (e.g. autonomous driving system)

  • Soft real-time system

    A soft real-time system stipulates that an action should be completed within a specified time, but can accept the occasional violation of time regulations, and will not cause any permanent damage after the situation occurs. (e.g. train ticket booking system)

  • advantage

    Under the control of the real-time system, the computer system can process the external signal in time after receiving it, and complete the processing within a strict time range.

  • shortcoming

    Difficult to implement.

1.2.5 Other operating systems

In addition to the above-mentioned operating systems, there are also network operating systems, distributed computer operating systems and personal computer operating systems.

  • network operating system

    An operating system that emerged with the development of computer networks can organically combine various computers to realize data transmission functions, sharing of various resources in the network, and communication between various computers.

  • Distributed computer operating system

    A distributed computer operating system consists of multiple computers and meets the following conditions:

    1.系统中任意两台计算机间能够正常通信
    2.系统中的每台计算机都具有同等地位,没有从机和主机之分
    3.每台计算机上的资源为所有用户共享
    4.系统中的任意一台计算机都可以构成一个子系统且可以重构
    5.任何工作都可以分布在几台计算机上,由它们并行、协同完成。
    

    Its main features are distribution and parallelism.

  • personal computer operating system

    Currently the most widely used operating systems, such as Windows, Linux and MacOS, etc.

Section summary

Insert image description here

1.3 The operating mechanism of the operating system

1.3.1 Preface

Before learning the operating mechanism of the operating system, we need to have a certain knowledge reserve.

That is, we need to understand, how does a program run?

When our programmers finish writing a piece of code and submit it to the compiler for compilation, a binary instruction file will be formed. The instructions in this file are the instructions that the CPU can recognize and execute. In the same way, if a program wants to run in a computer, its internal program must be compiled into binary instructions one by one, and then be recognized and executed by the CPU.

Insert image description here

To learn the operating mechanism of the operating system, we need to understand two types of instructions (privileged instructions and non-privileged instructions), two types of programs (kernel programs and user programs), two states (user mode and kernel mode), and the four operating system kernels. Just one aspect.

1.3.2 Kernel program and user program

In the operating system, the CPU executes two programs of different natures, one is the operating system kernel program, referred to as the "kernel program", and the other is the outer layer application program of the system, referred to as the "user program".

The kernel program is the manager of the user program. Privileged instructions can be executed in the kernel program , and only non-privileged instructions can be executed in the user program .

The programs written by our programmers are generally user programs.

1.3.3 Privileged instructions and non-privileged instructions

Among the instructions that the CPU can recognize, there are two types of instructions according to permissions, privileged instructions and non-privileged instructions.

  • Privileged instructions

    Instructions that users are not allowed to use directly, such as I/O instructions, interrupt setting instructions, instructions to access user memory protected registers, send program status words to program status word registers, etc.

  • unprivileged instructions

    Instructions that allow users to use them directly. Non-privileged instructions cannot directly access the software and hardware resources in the system. They are limited to accessing the user's address space, which can prevent user programs from damaging system programs.

1.3.4 User mode and kernel mode

The operating mode of the CPU is divided into two modes, user mode (eye mode) and kernel mode (core mode, pipe mode) . The CPU in kernel mode can execute privileged instructions, but the CPU in user mode cannot execute privileged instructions.

Regarding the content of user mode and kernel mode, readers need to clarify two main issues.

1.3.4.1 How does the CPU identify whether it is currently in user mode or kernel mode?

There is a register in the CPU called the program status register (PSW) . There is a binary bit in this register. 1 means that the CPU is in "kernel mode", and 0 means that the CPU is in "user mode" . When the CPU is in kernel mode, it means that the kernel program is running and privileged instructions can be executed. When the CPU is in user mode, it means that user programs are running and only non-privileged instructions can be executed.

1.3.4.2 How the CPU switches states

If the CPU wants to switch from user mode to kernel mode, it needs to trigger an interrupt or exception by the user program. After the PSW flag is set to 1 by the hardware implementation, it will switch to the kernel mode.

If the CPU wants to switch from the kernel mode to the user mode, the kernel program calls a privileged instruction that modifies the PSW.

Interrupts and exceptions will be explained in subsequent chapters.

1.3.5 Contents included in the operating system kernel

1.3.5.1 Clock management

The clock is the most basic and critical device of the operating system. It provides timing, interrupt and other functions.

  • The timing function allows the operating system to provide standard time to the user.
  • The clock interrupt function can realize process switching, such as time slice rotation management in time-sharing operating systems.
1.3.5.2 Interrupt mechanism

The original intention of the operating system to introduce interrupt timing is to improve CPU utilization in a multi-program environment, even if multi-programs can be executed concurrently. Over time, the interrupt mechanism gradually became the basis of modern operating systems. For example, keyboard or mouse information input, process management and scheduling, system function calls, device drivers, file access, etc., these functions all rely on the interrupt mechanism.

1.3.5.3 Primitives

The bottom layer of the operating system is some public small programs that can be called. They can each complete certain operations. These small programs are called primitives. They have the following characteristics:

  • It is at the bottom of the operating system and is the part closest to the hardware.
  • It is atomic, runs in one go, and cannot be interrupted.
  • It takes a short time to run and will be called frequently.

The direct way to define a primitive is to turn off interrupts and turn them on after the execution of the primitive is completed. Some operations of device drivers, CPU state switching, process communication and other functions in the system can be defined as primitives. That is, the two privileged instructions " disable interrupt instruction " and " open interrupt instruction " are used to achieve atomicity. After using the "off interrupt instruction", the CPU will not detect whether there is an interrupt signal when executing the program. After using the "open interrupt instruction", the CPU will detect whether there is an interrupt signal when executing the program. (Interrupt related knowledge will be introduced in subsequent chapters)

1.3.5.4 Data structure and processing of system control

There are many data structures used to register status information in the operating system, such as process control blocks, job control blocks, message queues, buffers, etc. In order to achieve effective management, the system defines some basic operations.

  • Process management

    Responsible for process status management, process scheduling and dispatch, creation and cancellation of process control blocks, etc.

  • memory management

    Responsible for memory space allocation and playback, memory information protection program, code swap program, etc.

  • Device management

    Responsible for buffer management, device allocation and recycling, etc.

As can be seen from the above content, kernel mode instructions actually include system call instructions and some operation instructions for clocks, interrupts, and primitives .

Section summary

Insert image description here

1.4 Interrupts and exceptions

1.4.1 The role of interrupts

Let’s talk about the conclusion first: interrupts are the only way for the operating system kernel to regain the right to use the CPU .

Let's assume the following scenario:

In the operating system, two states, kernel mode and user mode, are introduced. When the user starts user program A, the operating system kernel will actively give up the CPU usage rights to user program A at the appropriate time (that is, the CPU is transferred from kernel state to user program A) . Switch to user mode ). At this time, if there is no interrupt mechanism, the CPU will remain in user mode, and the operating system kernel will not be able to obtain the right to use the CPU.

Therefore, an interrupt mechanism was introduced in the operating system.

In the above chapter, we also mentioned that the CPU switches from user mode to kernel mode through the interrupt mechanism. (Triggered by an interrupt, the hardware automatically switches the CPU state).

1.4.2 Types of interrupts

Interrupts are divided into two types : external interrupts and internal interrupts .

1.4.2.1 External interrupt

External interrupts, also known as interrupts, refer to events from outside the CPU execution instructions. They are usually used for information input/output. For example, the I/O end interrupt issued by the device indicates that the device input/output is completed.

Example of external interrupt:

Clock interrupt, assuming that the clock component sends an interrupt signal to the CPU every 100ms, the following situation will occur:

1.CPU依次执行应用程序A的每一条指令(CPU每执行完一条指令后,就会检查是否接收到了中断信号,如果没有则继续执行下一条指令)。
2.CPU接收到由时钟部件发送的中断指令,则转换为内核态,操作系统将CPU交给处理时钟中断指令的内核程序(此时一般会将CPU内核交给另外一个应用程序B)
3.CPU由内核态转换为用户态且应用程序B占有了CPU,CPU依次执行应用程序B的每条指令...

In the same way, the above situation can also be applied to the situation where the I/O device sends an interrupt signal to the CPU.

1.4.2.2 Interrupted within

Internal interrupts are also called exceptions, which refer to events from within the CPU's execution of instructions, such as events caused by illegal opcodes in the program, address out-of-bounds, and special trapped instructions. Exceptions cannot be blocked and must be handled immediately once they occur .

Example of internal interrupt:

1.应用程序A试图在用户态下执行特权指令,这个指令是非法的,会引起一个中断信号,CPU接收到该中断信号,则转换为内核态,交由处理该中断信号的内核程序占有CPU。
2.应用程序A试图请求操作系统内核提供的服务,此时应用程序A执行了一个特殊的指令(陷入指令),该指令也会引起一个中断信号。

A trapped instruction means that the application actively returns the CPU usage rights to the operating system kernel. (System calls are implemented through trap instructions)

1.4.2.3 Classification diagram

Insert image description here

1.4.3 Basic principles of interrupts

The interrupt and exception handling process is roughly as follows

Insert image description here

When the CPU detects an abnormal event when executing the i-th instruction of the user program (or finds an interrupt request message after executing the i-th instruction), the CPU interrupts the current application program, then converts to the kernel state and hands it over to the corresponding Interrupt or exception handler execution. If the interrupt or exception handler can solve the problem, after the execution of the interrupt or exception handler ends, return the CPU to the i-th instruction or i+1 instruction of the interrupted user program by executing the interrupt or exception return instruction. Execution continues; if the interrupt or exception handler finds that this is an unrecoverable fatal error, the user program is terminated.

Among them, after the CPU detects an interrupt or exception, it will find the location of the corresponding interrupt handler in the "interrupt vector table" according to different interrupt signal types.

Section summary

Insert image description here

1.5 System calls

1.5.1 What is a system call

System call is a collective name for a series of interfaces provided to "programmers". Various shared resources in the system are uniformly managed by the operating system, and user programs can request related services (storage allocation, I/O input and output, etc.) through system calls.

System calls are the program interfaces provided by the operating system we mentioned in Section 1.1.2.2

1.5.2 The difference between system calls and library functions

The difference between system calls and library functions is shown in the following table

level illustrate
User program You can make system calls directly or use library functions
programming language Provide library functions upwards. Some library functions encapsulate system calls and hide system call details.
operating system Provide system calls upward so that upper-layer programs can request certain services of the kernel

1.5.3 The necessity of system calls

If there is no system call, that is, the operating system does not uniformly manage various shared resources of the computer, then an error will be sent in the following scenario:

When you want to print a file through a printer, you use the mouse to press the "Print" button of word, and the printer starts printing your file.

After a while, another colleague pressed the "Print" button of WPS, and the printer began to print his file. After that, the two processes will execute concurrently, and the contents of your file will be mixed with the contents of his file. .

The above scenario is because the shared resource printer is not uniformly managed.

Therefore, the necessity of system calls is reflected. The operating system kernel uniformly manages shared resources and provides "system calls" upward. If the user process wants to use shared resources such as printers, it must send a message to the system kernel through system calls. requests, and the system kernel will coordinate and process these requests.

1.5.4 Applicable scenarios for system calls

All operations related to shared resources (I/O operations, file management, etc.) must make requests to the operating system kernel through system calls, and the operating system kernel will complete it on your behalf. This can ensure the stability and security of the system and prevent illegal operations by users.

The classification of system calls is shown in the following table

Function illustrate
Device management Complete functions such as device request, release and startup
File management Complete functions such as reading, writing, creating and deleting files
process control Complete functions such as process creation, cancellation, blocking, and wake-up
process communication Complete functions such as message passing or signal passing between processes
Memory management Complete functions such as memory allocation, recycling, and obtaining the size and starting address of the memory area occupied by the job.

1.5.5 System call process

Specific steps are as follows:

  1. The CPU executes the application program, and some instructions of the application program pass in some system call parameters, and then executes the trapped instruction.
  2. After the CPU executes the trap instruction, the interrupt instruction triggers an interrupt signal, and the CPU transfers to the kernel state, which is occupied by the corresponding interrupt handler (system call entry program). According to certain parameters in the register, it can identify what type of system call it is.
  3. The handler called by this system occupies the CPU to execute relevant instructions. After the execution is completed, it returns to the application program and continues to execute subsequent instructions.

The above process can be summarized as:

Pass the system call parameters -> execute the trapped instruction -> execute the corresponding handler to handle the system call -> return to the application

Section summary

Insert image description here

1.6 Operating system structure

As operating systems continue to grow, various operating system architectures have emerged. Currently, we can divide operating system architectures into five types: hierarchical structure, modularization, macrokernel, microkernel, and outer core.

1.6.1 Hierarchical structure

1.6.1.1 Definition

The hierarchical structure refers to dividing the operating system into several layers. The lowest layer (layer 0) is the hardware, and the highest layer (layer N) is the user interface. Each layer can only call the functions and services of the lower layer immediately adjacent to it, that is, the lower layer goes up to the upper layer. To provide services, a high-level layer depends one-way on the layer below it. The details are shown in the figure below.

Insert image description here

1.6.1.2 Advantages
  • It facilitates system debugging and verification and simplifies system design and implementation.

    Because it is a one-way dependency, it can be debugged layer by layer from the lower layer to the higher layer. If a problem is found in a certain layer, you can quickly locate the error in that layer because its lower layer has been debugged to ensure that there are no problems with the functions provided.

  • Easy to expand and maintain.

    You can easily modify the functions of a certain layer. As long as the inter-layer interface of this layer is not changed, other layers will not be affected.

1.6.1.3 Disadvantages
  • It is difficult to define between the various levels.
  • The efficiency is poor. Every time a high-level function is executed, low-level services may need to be continuously called from bottom to top.

1.6.2 Modularization

1.6.2.1 Definition

Modularization refers to dividing the operating system into several independent modules according to functions. Each module is responsible for a certain function, and each module can be further divided into sub-modules. As shown below

Insert image description here

1.6.2.2 Advantages
  • Improved the correctness, understandability and maintainability of operating system design
  • Enhanced operating system adaptability
  • Improved operating system development efficiency
1.6.2.3 Disadvantages
  • The interface between modules is difficult to meet the actual demand for the interface
  • Each module designer works hand in hand, and each decision cannot be based on the previous proven correct decision, so a reliable order of decisions (as opposed to a hierarchical structure) cannot be found.

1.6.3 Macro kernel

1.6.3.1 Definition

The macro kernel, also known as a single kernel or a large kernel, refers to the main functions of the operating system running in the core state as a closely related whole, thereby providing high-performance system services to user programs. The schematic diagram is as follows.

Insert image description here

1.6.3.2 Advantages

Since information is shared between modules, services between modules can be quickly called. Therefore, the macro kernel has extremely high performance advantages.

1.6.3.3 Disadvantages
  • The kernel functions are complex and difficult to maintain.
  • A failure of a certain functional module in the kernel may cause the entire system to crash.

1.6.4 Microkernel

1.6.4.1 Definition

Microkernel refers to retaining the most basic functions of the operating system kernel in the kernel, and moving functions that do not need to be executed in the core state to the user state to reduce the design complexity of the kernel. Those operating system codes that are moved out of the kernel are layered according to The principle is divided into several service programs, their execution is independent of each other, and the interaction is communicated with the help of microkernel.

The microkernel structure divides the operating system into two parts: the microkernel and multiple servers .

A microkernel is a small kernel that can implement the most basic functions of an operating system, including the following functions:

  1. Functions closely related to hardware processing
  2. Communication functions between client and server
  3. Some basic functions

Most of the functions of the operating system are placed in some servers outside the microkernel. For example, process management server, I/O device server, etc. These servers run in user mode, and communication between them is achieved through the message passing mechanism provided by the microkernel. The schematic diagram is as follows.

Insert image description here

1.6.4.2 Advantages
  • Easy to expand, many functions are outside the kernel, it is very easy to modify or add some functions.
  • It has excellent portability. Code related to CPU and I/O hardware is placed in the kernel. Various other servers are independent of hardware, so porting the operating system to another platform requires minimal changes. .
1.6.4.3 Disadvantages

The main disadvantage of the microkernel structure is performance, because it requires frequent switching between the core state and the user state.

1.6.5 Outer core

Just have some knowledge about the outer core.

The outer kernel is a program that runs in the kernel state. Its task is to allocate resources to virtual machines and check attempts to use these resources to ensure that no machine will use other people's resources.

1.6.6 Summary and comparison

Structure name characteristic advantage shortcoming
Hierarchical structure The kernel is divided into multiple levels, and the higher levels are unidirectionally dependent on the lower levels. Easy to debug; easy to expand and maintain The boundaries of each layer are difficult to determine; the efficiency is low and cannot be used across layers.
Modular Divide the kernel into modules Easy to maintain; modules can communicate with each other; support dynamic loading of new modules The interface definition between modules may not be reasonable; modules depend on each other and are difficult to debug.
macro kernel All functionality is placed in the kernel High performance, each module calls each other directly The kernel functions are complex and difficult to maintain; an error in a certain function may cause the entire system to crash.
microkernel Only put basic functions in the kernel, and put the rest in other servers The kernel has few functions and is easy to maintain; it has strong portability. Poor performance, requiring frequent switching between user mode and core mode
outer core The kernel is responsible for process scheduling, process communication and other functions, while the outer kernel is responsible for allocating unabstracted hardware resources to users and ensuring the safety of resource use. The outer core directly allocates "abstract" hardware resources to users, allowing user processes to use hardware resources more flexibly. Reduced system consistency

Section summary

Insert image description here

1.7 Operating system boot program

1.7.1 Overview

From the study in the previous chapters, we learned that the operating system is essentially a program, so in the computer, the programs are stored on the hard disk in the form of data. When our computer presses the power button, how to run the operating system installed on the hard disk? This problem requires the operating system boot program introduced in this section to complete. It means that the computer uses the CPU to run a specific program -> identify the hard disk through the program -> identify the hard disk partition - identify the operating system on the disk partition -> finally start the operating system through the program . The above process is completed step by step under the guidance of the operating system boot program.

1.7.2 Operating system boot process

Before learning the operating system boot process, we first need to understand what data is stored in the hard disk.

The picture below shows an empty disk

Insert image description here

The new disk I just bought has nothing on it, just like an empty box.

The picture below shows a disk after the operating system is installed.
Insert image description here

The figure below shows the schematic diagram of the main memory.

Insert image description here

After understanding the above related structures, we will start to learn the boot process of common operating systems, which is divided into the following eight steps.

  1. Activate the CPU . The activated CPU will read the boot program in the ROM, set the instruction register to the first instruction of the BIOS (Basic Input/Output System), and start executing the BIOS instructions.
  2. Hardware self-test . After starting the BIOS program, first perform a hardware self-test to check whether there is a fault in each hardware. If a fault occurs, the motherboard will issue different beeps to prompt and terminate the startup. If there are no faults, proceed to the next step.
  3. Load the hard disk with the operating system , the BIOS begins to read the Boot Sequence (through the boot sequence saved in CMOS, or through interaction with the user), and hands control to the storage device ranked first in the boot sequence, and then the CPU Load the contents of the boot sector of the storage device into memory.
  4. Load the master boot record MBR . The hard disk uses a specific identifier to distinguish boot hard disks from non-booting hard disks. The function of the master boot record MBR is to tell the CPU which primary partition of which hard disk to search for the operating system.
  5. Scan the hard disk partition table and load the hard disk active area . MBR contains the hard disk partition table. In the hard disk partition table, active partitions and inactive partitions are distinguished by specific identifiers. The master boot record scans the hard disk partition table, finds the hard disk partition (active partition) containing the operating system, starts loading the active partition, and hands control to the active partition.
  6. Load the partition boot record PBR . Read the first sector of the active partition, which is called the Partition Boot Record (PBR). Its function is to find and activate the program (boot manager) used to boot the operating system in the root directory of the partition.
  7. Load the boot manager , search the partition boot record for the boot manager in the active partition, and load the boot manager.
  8. Load the operating system .

Insert image description here

The execution process in the above figure can be summarized into the following steps:

1. The CPU starts fetching instructions from a specific main memory address and executes the boot program in the ROM (first perform a hardware self-test, and then turn on the computer)

2. Read the MBR in the first sector of the disk into memory, execute the disk scan program, and scan the disk partition table

3. Read the partition boot record from the active partition (also called the primary partition, that is, the partition where the operating system is installed) and execute the program in it

4. Find the complete operating system initialization program (startup manager) from the root directory, load the operating system and complete the boot

1.8 Virtual Machine

1.8.1 Overview

A virtual machine refers to a logical computer that uses virtualization technology to provide users with an abstract, unified, and simulated computing environment by hiding the actual physical characteristics of a specific computing platform .

Currently, there are two virtualization methods for virtual machines, which can be divided into first-type virtual machines and second-type virtual machines .

1.8.2 The first type of virtual machine

The first type of virtual machine is like an operating system. It is the only program in the computer system that runs at the highest privilege level. It runs on bare metal and has multi-programming capabilities.

The virtual machine runs as a process in user mode and is not allowed to execute sensitive instructions. However, the operating system on the virtual machine thinks that it is in kernel mode. This is called virtual kernel mode. When the virtual machine operating system executes a privileged instruction, it will trap the hypervisor. On a CPU that supports virtualization, the hypervisor checks this instruction. If the instruction is executed by the operating system in the virtual machine, Then arrange for this instruction to run normally, otherwise it will simulate the behavior of real hardware executing privileged instructions in user mode.

The schematic diagram is as follows

Insert image description here

1.8.3 The second type of virtual machine

The second type of virtual machine is just like an ordinary process and relies on resources provided by operating systems such as Windows and Linux.

For example: we can install VMware on the Windows operating system, and then install the image of the Linux operating system on VMware. In this way, we can use the Linux system on the Windows system and run other processes on the Windows system at the same time.

The schematic diagram is as follows

Insert image description here

Section summary

Insert image description here

references

《2023年操作系统考研复习指导》-王道论坛

If this article is helpful to you, you may wish to like, forward, and save it~

Guess you like

Origin blog.csdn.net/weixin_42584100/article/details/126090180