Operating system (1)---The concept of operating system, concurrency, sharing, virtual, asynchronous, system calls, and the operating mechanism and architecture of the operating system.

Table of contents

1. What is an operating system?

Second, the development stage of the operating system:

Manual operating system:

Single pass batch processor:

 Multipass Batch Processor:​Edit

 Time-sharing operating system:

Real-time operating system:

Three, the four characteristics of the operating system:

Concurrency:

Sharing:

Virtuality:

 Asynchronicity:

4. Operating mechanism and architecture of the operating system:

 How does the program work?

CPU user mode and kernel mode:

Operating system kernel:

Outer core operating system:

5. Interruption:

6. Operating system boot:

 Seven, virtual machine:

8. System call:

What are system calls?

Why use system calls?

When are system calls needed?

The process of the system call: ​


1. What is an operating system?

The operating system (Operating System, OS) refers to the control and management of the hardware and software resources of the entire computer system , and reasonably organizes and schedules the work of the computer and the allocation of resources ; to provide users and other software with a convenient interface and environment ; it is The most basic system software in a computer system .

 The operating system is a resource manager, the software closest to the hardware, and provides convenient and easy-to-use services to the upper layer.

Provide user graphical interface and command interface

  •  Online command interface: the user enters a command and the operating system executes a command
  • Offline command interface: the user enters a bunch of commands, and the operating system executes a bunch of commands

A program interface is provided for programmers:

The program interface is actually the interface for calling system calls, such as printf() in C language. When the programmer calls this function, the C language will execute the corresponding logic and notify the operating system that I need the display to display the data output by the programmer, so the operating system starts Invoke system calls to interact with hardware.


Second, the development stage of the operating system:


Manual operating system:

People write instructions on the paper tape -> computer reads the paper bag -> executes the program -> prints the result on the paper tape.

Obviously, the performance of the computer is not displayed at all, and a lot of time is wasted.

Single pass batch processor:

The introduction of batch input and output means that the user can input multiple instructions at the same time, and the execution of these instructions is managed and controlled by the peripheral machine.

Main advantages: Alleviate a certain degree of man-machine speed contradiction, and improve resource utilization. 

Main disadvantage: only one program can run in the memory, and the next program can only be transferred after the program finishes running. The CPU spends a lot of time idle waiting for o to complete. Resource utilization remains low.

 Multipass batch processor:

 Before, only one instruction could be sent to the memory at a time, but now multiple instructions can be sent to the memory for execution at the same time. When the input part is free, the input of the next program can be executed, and when the operation is idle, the previous program can be executed. the operation part of .

 Time-sharing operating system:

Time-sharing operating system : The computer takesturns serving each user/job in units of time slices , and each user can interact with the computer through the terminal.

On the surface, each user monopolizes a computer, but the operating system is actually serving them asynchronously, and each person's program executes a part of the time to run alternately. But it is fair to everyone, so there is no one who comes first, and users with high priority cannot be served first.

 Main advantages : user requests can be responded instantly, solving the problem of human-computer interaction. Multiple users are allowed to use one computer at the same time, and the operations of the users on the computer are independent of each other, and they cannot feel the existence of others.

Major Cons : Cannot prioritize some urgent tasks. The operating system is completely fair to individual users/jobs, serving each user/job a time slice in a cyclical manner, without distinguishing the urgency of tasks.

Real-time operating system:

In view of the shortcomings of the time-sharing operating system above, users with high priority cannot be served first. At this time, a real-time operating system is introduced. When the user's priority is high and an instruction is made, the operating system must put down the currently executing program to serve the user with high priority.

The most important features of real-time operating systems are timeliness and reliability .


Three, the four characteristics of the operating system:

Four major features Concurrency , sharing, virtual, asynchronous . The first two are the most basic characteristics of the operating system, and the two are conditions for each other, which is the premise of virtual and asynchronous realization.

Concurrency:

Before distinguishing concurrency, let's talk about parallelism. Parallelism means that two programs run simultaneously at the same time. And concurrency only means that two programs run alternately within a period of time .

To give a simple example, listening to songs and learning. Parallel means that within an hour, I listen to songs and study at the same time, without delaying both. Concurrency means that I listen to a song for five minutes within an hour, then turn off the music and study for five minutes. After five minutes of study, I stop studying and listen to a song for five minutes. In this way, I can only do one thing in a period of time. are executed alternately.

Concurrency : Refers to two or more events occurring within the same time interval. These events occur at the same time macroscopically, but alternately occur microscopically

The concurrency of the operating system refers to the "simultaneous" running of multiple programs in the computer system. These programs appear to be executed at the same time, but they are actually executed alternately.

According to concurrency, let's introduce the relationship between the number of CPU cores and programs: If your CPU has a single core, then you can only run one application at most at the same time. If your CPU has eight cores, then your computer can run eight programs in parallel. The reason why the single-core CPU we see is also running many applications at the same time is that the operating system is actually executing these programs concurrently.

Sharing:

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

Simultaneous sharing refers to microscopically speaking, multiple processes alternately access a resource within the same time interval. A mutex share allows only one process to access it within a time interval .

  • Mutually exclusive sharing : use QQ and WeChat video. The camera can only be assigned to one of the processes at the same time.
  • Simultaneous sharing : use QQ to send file A, and use WeChat to send file B at the same time. From a macro perspective, both sides are reading and sending files at the same time, indicating that both processes are accessing hard disk resources and reading data from them. From a microscopic point of view, the two processes access the hard disk alternately.

Virtuality:

Virtual refers to turning a physical entity into several logical counterparts . The physical entity (the former) is what actually exists, while the logical counterpart (the latter) is what the user feels.

Suppose a game needs 4GB of memory, QQ needs 300MB, and QQ music needs 300MB, but our computer only has 4GB of memory but games, QQ, and QQ music need 4.6GB of memory to run at the same time, which applies the virtuality of the operating system. He makes our users feel that their memory has become larger. In fact, the operating system has virtualized the memory.

Behind this, the operating system initially schedules part of the code of the above programs into the memory. When running the code, it finds that other codes need to be used, then schedules other codes into the memory, and calls out the code that may not be used. .

 Asynchronicity:

Asynchrony means that in a multiprogramming environment, multiple programs are allowed to execute concurrently . However, due to limited resources, the execution of the process is not consistent to the end, but stops and starts, and moves forward at an unpredictable speed. This is the asynchronous nature of the process.

Before understanding asynchrony, let’s talk about synchronization . Synchronization means that another program can only be executed after the current program is executed. Asynchronous means that I only hand over the things to be executed to you, and I don’t care when it is executed.

For example, for printer printing, three users want to print, but there is only one printer at this time:

  • Synchronization : The first user occupies the printer, the second person clicks to print and the operating system tells him that it can't be done. You can only notify me after the first person finishes executing.
  • Asynchronous : Three people submit requests at the same time, whoever uses the printer first will be assigned by the operating system in the order of submission, and the user does not need to wait for the first person to print before submitting the second printing.

4. Operating mechanism and architecture of the operating system:

 How does the program work?

When our programmers write the C language code, it is compiled into binary instructions, and then sent to the CPU by the operating system, and the CPU executes these instructions in turn.

Instruction: refers to a binary number composed of 01, which is the only basic command that the CPU can recognize and execute.

CPU user mode and kernel mode:

The CPU can execute two types of instructions: user instructions and kernel instructions . User instructions are written by programmers, and kernel instructions are instructions in the kernel program of the operating system. Therefore, the CPU has two states, kernel state and user state .

  •  When in the kernel state , it means that the kernel program is running at this time, and privileged instructions can be executed at this time
  • When in user mode , it means that the application program is running at this time, and only non-privileged instructions can be executed at this time

The CPU switches between kernel mode and user mode through the PSW flag bit .

  • Kernel mode → user mode : Execute a privileged instruction - modify the flag bit of PSW to "user mode", this action means that the operating system will actively give up the right to use the CPU
  • User mode→kernel mode : Triggered by "interrupt" , the hardware automatically completes the metamorphosis process. Triggering an interrupt signal means that the operating system will forcibly regain the right to use the CPU and stop the currently running program.

Operating system kernel:

 Primitives: The operating system uses primitives to inform a piece of hardware what functions to perform.

Clock management: For example, a time-sharing operating system needs to switch programs within a fixed time interval. With clock management, the clock will notify the operating system to process other programs after the time interval expires.

 Large core: When the company was first established, it was small, and the people in the management team would be responsible for most of the things. The advantage is high efficiency; the disadvantage is that the organizational structure is chaotic and difficult to maintain, and some errors may cause the entire kernel to fail.

Microkernel: As the size of the enterprise grows larger and larger, the management is only responsible for some of the core tasks. The advantage is that the organizational structure is clear and easy to maintain; the disadvantage is that the efficiency is low.

 Execution speed blocks for large cores, because the number of CPU state switches is small.

Outer core operating system:

Whether the memory provided by the kernel operating system to the user is virtualized, that is, discontinuous. The user intuitively feels that he has monopolized a continuous memory space, but in fact the user only occupies a part of the non-contiguous memory space. The outer core is different. The outer core operating system will allocate an actual continuous space for the user, and the space that the user intuitively feels is the space he owns.

5. Interruption:

 Interrupt processing: Forcibly terminate the currently executing program and let the operating system execute other programs. It is the only way for the operating system kernel to regain the CPU.

  • Internal interrupt: related to the currently executing instruction from within the CPU
  • External interrupt: It has nothing to do with the inside of the CPU, and comes from outside the CPU, such as the interrupt signal sent by the printer and the clock component to the CPU

6. Operating system boot:

To put it bluntly, the operating system boot is the process of booting the operating system.

When our computer is turned on, the CPU will first access the ROM program in the memory to read the master boot record into the memory. At this time, the CPU will execute the master boot program (MBR) in the memory, and the master boot program will find the C drive. Then the CPU executes the boot record program in the boot record program, and the boot record program will guide the program to find the operating system initialization program and read it into the memory, and the initialization of the operating system is completed at this time.

 Seven, virtual machine:

There are roughly two types of virtual machines:

The first one is directly installed on your own computer, and performs operations such as partitioning the computer, which is equivalent to installing two systems on one computer, and selecting the operating system to enter when the computer is turned on.

The second is equivalent to installing a virtual machine software on the current system, and the virtual machine software manages and allocates resources of the virtual system.

The former is a separate system and the latter is to install another system on one system. Compared with us, we need to call hardware resources. The former directly enters the system call to complete the call of hardware resources, while the latter virtual system needs to send to the virtual machine handler first. Request, and then the virtual machine program sends a request to the parent operating system, and the parent operating system sends the feedback result to the virtual machine software after completing the system call, and the virtual machine software notifies the virtual operating system that the function has been completed, which is a layer-by-layer call process, so the efficiency is obviously very low.


8. System call:

What are system calls?

When we want to use a printer, the printer is a hardware resource, and we programmers have no way to directly call the hardware resources of the computer. Only the operating system can call the hardware resources of the computer, so the system call can be imagined as a function interface first, through this The interface notifies the operating system to complete the hardware call.

Why use system calls?

Still the above example, two people access the printer at the same time, if there is no control, one printer receives two requests at the same time, the final result is to print two files on one paper, the data is confused, so for shared resources Access must be done by our big housekeeper operating system, which sends requests to the operating system kernel by system calls and manages resources by the kernel.

When are system calls needed?

Applications request services from the operating system through system calls. The various shared resources in the system are under the unified control of the operating system kernel, so all operations related to shared resources (such as storage allocation, I/O operations, file management, etc.) A service request is made and the operating system kernel completes it on its behalf. This can ensure the stability and security of the system and prevent users from performing illegal operations.

The process of the system call: 

 The application calls the read library function --> writes the unique id of the current process into the register --> falls into the operating system kernel --> the operating system kernel has many system calls, and the kernel judges what the instruction is to execute based on the transmitted data Call --> The operating system determines that it will call the hardware to start executing the corresponding program --> After the execution is completed, the application program that has just been executed is awakened with a primitive to notify it that the process has been completed. 

Guess you like

Origin blog.csdn.net/weixin_60414376/article/details/126921743