Chapter 1: Overview of operating system
0, operating system knowledge overview
1. Concept of operating system
First, combine life experience to understand the hierarchy of computer systems: bare metal (pure hardware) => operating system => applications (software) => users
Operating System (OS) refers to the control and management of the entire computer hardware and software resources, and the reasonable organization and scheduling of computer work and resource allocation, to provide users and other software convenient interface and environment, it is the most basic System software in the computer System.
- Responsible for the management and coordination of hardware, software and other computer resources
- It provides easy-to-use services for upper-layer applications and users
- An operating system is system software, not hardware
2. Operating systemFunctions and Objectives
1. As the manager of system resources (software, hardware, files and other resources)
- Functions: file management, memory management (memory management), processor [CPU] management, device management
- Goal: safe and efficient
Let’s explain these four features in layman’s terms: see the figure below
2. Interface between user and computer hardware
- Function”Command interface and program interface are collectively called user interface】
- Command interface (allows users to use it directly)
- Online command interface (interactive command interface, user say, system do), such as our Windows system
cmd
Window inside input command - Offline command interface (batch command interface, user says a lot, system does a lot), similar to Linux shell scripts
- Online command interface (interactive command interface, user say, system do), such as our Windows system
- Program interface (allows users to use it indirectly through programs) : Consists of a set of system calls (program interface = system call)
- Note: Some places show: System call = system call command = general instruction
- GUI (Graphical user interface)
- Command interface (allows users to use it directly)
- Goal: easy to use
3. As the level closest to hardware
- Functions: to achieve the expansion of hardware machines
3. Four characteristics of the operating system
3.1, concurrent
Concurrency: Two or more events occurring at the same time interval. These events are macroscopically simultaneous, but microscopically alternate [CPU time sharding].
Concurrency VS parallelism (two or more events occurring simultaneously at the same time)
Operating system concurrency refers to the simultaneous existence of more than one running program in a computer system: A single-core processor (CPU) can only execute one program at a time, so the operating system is responsible for coordinating the execution of multiple programs alternately (these programs are microcosmic in execution, but macroscopic in execution, such as my computer can open QQ or wechat to do something at the same time).
In fact, operating systems were born with multiprogram technology, so operating systems and program concurrency were born together.
Today’s computers generally have multi-core CPUS. For example, Intel’s eighth-generation I3 processor is a 4-core CPU, which means that four programs can be executed in parallel at the same time. However, the concurrency of the operating system is still essential.
3.2, share,
Resource sharing means that the resources in the system can be used by multiple concurrently executing processes in memory
There are two resource sharing modes:
-
Mutually exclusive sharing mode (only one process is allowed to access the resource within a period of time)
-
Simultaneous sharing (allowing multiple processes to access the resource at the same time in a period of time, macroscopically at the same time, but microscopically alternately)
Examples of life:
- Mutually exclusive sharing mode: USE QQ and wechat videos. A camera can be assigned to only one process at a time.
- Simultaneous sharing: use QQ to send file A, and use wechat to send file B. At a macro level, both processes are reading and sending files at the same time, indicating that both processes are accessing disk resources to read data from them. At the micro level, two processes access the hard disk alternately.
Concurrency and sharing are the two most basic characteristics, and they are mutually existing conditions.
The relationship between concurrency and sharing: if concurrency is lost, only one application can be running in the system, and sharing loses its meaning. If the loss of sharing, QQ and wechat can not access hard disk resources at the same time, it can not be realized at the same time to send files, it can not be concurrent.
3.3, virtual
Virtual refers to the transformation of a physical entity into several logical counterparts. Physical entities exist, while logical counterparts are felt by the user.
Virtual technology —
- Space division multiplexing (virtual memory) is shown above
- Time division Multiplexing (virtual processor technology) – time slice rotation. The following figure
Obviously, if concurrency is lost and only one program is running in the system at a time, the point of virtuality is lost. Therefore, without concurrency, there can be no virtuality.
3.4, asynchronous
In A multi-program environment, multiple programs are allowed to execute concurrently, but due to limited resources, the execution of the process is not consistent, but rather stop-and-go (for example, process A is halfway through execution, then the next process B is suddenly executed when the time slot is up), and then the process is halfway through execution. And then go back to where the original process A was not executing], moving forward at an unpredictable speed, which is the asynchronism of the process.
Obviously, without concurrency, the system can only process programs serially, with each process executing consistently to the end. Asynchrony is only possible if the system has concurrency.
4. Development and classification of operating systems
4.1 manual operation stage
[Punching machine] Main disadvantages: the user monopolizes the whole machine, human-machine speed contradiction leads to very low resource utilization rate
4.2 batch processing stage
With the support of multi-programming technology and time-sharing technology, batch processing system and time-sharing system appear. The operating systems configured in these two kinds of systems are called batch operating system and time-sharing operating system respectively. The emergence of these two kinds of operating systems marks the formation of operating system.
The characteristics of multi-channel program operation are as follows:
- Multichannel: The simultaneous storage of several independent programs in computer memory.
- Macroparallelism: several programs that enter the system at the same time are in progress, that is, they are all running, but none of them is finished.
- Microserial: Microcosmic, alternating execution of multiple programs in main memory occupying a processor in turn or time – sharing.
Batch processing system adopts multi-channel programming technology to organize the user programs submitted to the system into the form of jobs, and the operator stores a batch of jobs on the auxiliary storage device, forming a job queue, waiting for operation.
Single-channel batch systems: The introduction of offline input/output technology (done on tape) and the supervisor responsible for controlling the input/output of the job
- Major disadvantage: there is only one program running in memory, and the next program can only be called after the program is finished. The CPU has a large amount of idle time waiting for I/O completion, resulting in low resource utilization
Multichannel batch systems (operating systems began to appear and interrupt technology was introduced, operating system assignment managed the running of these programs, and each program was executed concurrently) :
- Main advantages: concurrent execution of multiple programs, sharing computer resources [CPU, memory]
- Main disadvantage: no human-computer interaction function
Let’s use a Gantt chart to illustrate: how can a multichannel batch system improve resource utilization
Suppose your computer needs to process three jobs
- Job 1: Input 1 second, calculate 1 second, output 1 second.
- Task 2: Input 1 second, calculate 1 second, output 1 second.
- Job 3: Input 1 second, calculate 1 second, output 1 second
If using single channel batch processing technology:
If multi-channel batch processing technology is used
4.3 Time-sharing operating system
Introduce time-sharing technology, the so-called time sharing technology, is to put the processor time into a very short time, take turns to assigned to each user programs, if a user program in the allocated time has yet to be completed before you use up all calculations, the program was suspended, waiting for the next round of continue to calculate, the processor to another user programs.
Main advantages: The user request can be immediately responded to solve the problem of human-computer interaction.
Major disadvantages: Some urgent tasks cannot be prioritized, which is fair to each user and job
4.4. Real-time operating System
Main advantages: It can preferentially respond to some urgent tasks [priority queue] without time slice queuing, timeliness and reliability
- Hard real-time system (processing must be completed within absolutely strict specified time)
- Soft real-time system (can accept occasional time violation)
4.5. Modern operating systems
Modern operating systems refer to operating systems that are currently widely used and popular, including:
- A powerful PC operating system with a graphical user interface;
- Modern batch operating system with large throughput and strong processing capacity;
- Time-sharing operating system with strong interactive ability and fast response;
- Real-time operating system with real-time response and predictive analysis capability;
- Network operating system with network resource sharing and remote communication capability;
- A distributed operating system with a single system image, distributed processing capabilities, etc.
5. The operating mechanism and architecture of the operating system
5.1 Knowledge overview
5.2 Operation mechanism
1. What is a directive? What’s the difference between an “instruction” and what we call a “code”?
- C language and other high-level language code → through “translation” → machine language instructions, the operating system can identify, a high-level language code translation may or corresponding to multiple instructions.
- Simply put, “instructions” are the most basic commands that a processor (CPU) can recognize and execute.
2. Two kinds of instructions
- Some instructions are “harmless to humans and animals”. For example: add, subtract, multiply, divide these ordinary operation instructions.
- Some commands require very high permissions. For example, the memory zero clearing instruction. If a user program can use this instruction, it means that one user can zero out other users’ memory data at will, which is obviously dangerous. This privileged directive is not allowed to be used by user programs.
3. How does the CPU determine whether it can execute privileged commands? == According to both processor states
- User state (visual state) : is the state of the machine when the user program is executed. When the CPU is in user state, the CPU can only execute non-privileged instructions.
- Core state (transient) : also known as system state, it is the state of the machine when the hypervisor of the operating system is executed. When the CPU is in the core state, both privileged and non-privileged instructions can be executed.
The two states of the processor are identified by a flag bit in the program state word register (PSW) to identify what state the processor is in. For example, 0 is the user state, and 1 is the core state.
4. Privilege instruction:
The kernel mentality allows the operating system to use all instructions, including a set of privileged instructions, which involve the following aspects:
- An instruction to change the state of a machine
- An instruction to modify a special register
- Instructions for I/O operations involving external devices
5, in the following circumstances, from the user mode automatically to the core mentality:
- User processes access the operating system and require certain services from the operating system. This access is called system function calls
- An interrupt occurs while the user program is executing.
- An error occurred in the user program. (Internal interruption)
- Attempted to execute a privileged command in user mode
- The transition from core state to user state is accomplished with a single instruction, which is itself a privileged instruction.
6. Two procedures
- The kernel program
- The application
Operating system kernel program is the manager of the system, can execute privileged instructions, also can execute non-privileged instructions, run in the core mentality; In order to ensure the safe operation of the system, ordinary applications can only execute non-privileged instructions and run in user mode.
5.3 operating system kernel
The kernel of an operating system is the low-level software configured on a computer. It is the most basic and core part of an operating system. The programs that implement the functions of the operating system kernel are called kernel programs.
An overview of four features in the kernel
5.4 operating system architecture
The operating system architecture is divided into two types: big kernel and micro kernel
- A large kernel is an operating system that contains clock management, interrupt handling, primitives (device drivers, CPU switching, etc.), process management, memory management, device management and other functions.
- A microkernel is an operating system that includes clock management, interrupt handling, primitives, and so on.
Analogy: The architectural problems of an operating system are similar to the management problems of an enterprise.
- The kernel is the management of the enterprise, responsible for some important work. Only management can execute privileged orders; ordinary employees can only execute non-privileged orders.
- The switch between user mode and core state of mind is equivalent to the work handover between ordinary employees and management
- Big core: A startup is small and the people in management are responsible for most of the things. The advantage is high efficiency; The disadvantage is that the organizational structure is chaotic and difficult to maintain.
- Microkernel: With the increasing size of the enterprise, the management is only responsible for some core work. The advantage is that the organizational structure is clear and easy to maintain, but the disadvantage is low efficiency
6. Interruptions and exceptions
6.1 Knowledge overview
6.2. Birth of interrupt mechanism
In order to solve the problem of single channel batch processing system, the invention operating system introduced the interrupt mechanism, and realized the concurrent execution of multiple programs.
Interrupt refers to that when an event (such as power failure, floating point operation overflow, external device I/O transmission completion or error) occurs, the system suspends the execution of the running program, and the event processing program processes the corresponding event. After processing the event, the system returns to the breakpoint to continue execution.
Interrupt source: The event that causes the interrupt is called the interrupt source, such as the interrupt after printing, where the interrupt source is the printer.
Breakpoint: A program that is running at the time of the interruption is temporarily stopped. The point at which the program pauses is called a breakpoint. For example, if a program is interrupted while executing at address 0200, address 0200 is a breakpoint and the next instruction of 0200 is executed when the interrupt returns.
Interrupt response: when the processor detects an interrupt request, it suspends the execution of the running program and automatically elicits the interrupt handler.
Interrupt handler: The program that handles interrupt events is called an interrupt handler. Such as clock interrupt processing, printer complete interrupt processing, printer lack of paper interrupt processing and so on. It is the closest part of the operating system to the hardware, and is the interface between the operating system and the hardware.
The whole interrupt process involves both user program and operating system interrupt program.
Essence: When an interruption occurs, the operating system needs to step in and manage it.
- When an interrupt occurs, the CPU immediately goes into a core mindset.
- When an interruption occurs, the currently running process is paused and the operating system kernel handles the interruption.
- Different interrupt signals are processed differently.
When an outage occurs, it means that the operating system needs to step in to manage it. Since the management of the operating system requires the use of privileged instructions, the CPU moves from the user state to the core state. Interrupts can switch the CPU from user mode to core mode, allowing the operating system to gain control of the computer. With interruption, to achieve the concurrent execution of multiple programs. After processing, the CPU will return to the user state, continue to execute the main program
How to realize the switch between user mode and core state of mind?
A:
- User -> Core – state is implemented through interrupts. And interruption is the only way.
- The switch to “Core State -> User state” is performed by executing a privileged command that sets the program state word flag to “User state”.
6.3 Interrupt classification
Another way to classify it
- Trap: An intentional exception, such as a system call.
- Fault: Caused by an error condition that can be fixed by a fault handler, such as a missing page.
- Abort: The result of an unrecoverable fatal error that the termination handler no longer returns control, such as integer division by zero, to the application that caused the termination.
6.4 External interrupt processing process
- Step1: after executing each instruction, the CPU checks whether there is an external interrupt signal.
- Step2: if the external interrupt signal is detected, the CPU environment that is frequently interrupted needs to be protected (such as program status word PSW, program counter PC, and various general registers)
- Step3: transfer to the corresponding interrupt processing program according to the interrupt signal type.
- Step4: restore the CPU environment of the original process, exit the interrupt, and return to the source program to continue the execution.
7. System calls
7.1 What is a system call?
As the interface between the user and the computer hardware, the operating system needs to provide some easy-to-use services upward. It mainly includes command interface and program interface. A program interface consists of a set of system calls.
System call is an interface provided by the operating system to the application program (programmer). It can be understood as a special function called by the application program. The application program can issue system call request to obtain the services of the operating system.
Example Question: Why does an operating system provide “system call” functionality?
Life scenario: You go to the school print shop to print your paper, and when you press “Print,” the printer starts working. Halfway through your paper, another student presses the “Print” button and starts printing his own paper. In the end, your paper and the student’s paper page are not jumbled together, but printed in sequence.
Consider: What would happen if processes could use the printer at will?
Half way through your paper printing, another student presses the “print” button and starts printing his own paper. As a result, the second half of your paper gets mixed up with the student’s page…
Solution: The operating system provides a “system call” function. If a user process wants to use the printer, it can only request the operating system through the system call. The operating system coordinates and manages requests.
Applications request services from the operating system through system calls. All shared resources in the system are managed by the operating system. Therefore, resource-related operations (storage allocation, I/O operations, file management, etc.) in user programs must make service requests to the operating system through system calls, which are completed by the operating system. This can ensure the stability and security of the system. Prevent unauthorized operations by users
The system call causes the processor to go from user state to core state (execute an int instruction (fall into instruction))
7.2. Difference between system call and library function
Applications request services of the operating system and access various resources through the program interfaces provided by the operating system. The application sends the request, and the kernel receives and processes the request. In any operating system, system calls are the only means of user-space access to the kernel. Applications can be programmed in a variety of languages, but if written in a low-level language (such as assembler programming language), they can be called directly with system-provided system calls, that is, explicitly. If you program in a high-level language, call implicitly. This implicit invocation is provided by API functions and standard C library functions.
Typically, applications request operating system services through apis (Application programming interfaces). In UNIX and Linux systems, application programming interfaces are based on POSIX standards. There is a direct relationship between API functions defined by POSIX standards and system calls. Linux systems, like UNIX systems, provide system calls as part of the C library. The C library implements the major APIS of UNIX systems, including standard C library functions and system calls. Figure 3.4 illustrates the relationship between applications, C libraries, and operating system kernels
It should be noted that there are differences between system calls and library functions: ** Library functions are provided by software developers and linked into user programs by compiler linking tools. The execution of library functions does not cause CPU state changes; While the system call code belongs to the OS, the system call code execution makes the CPU state from user state to core state. ** Some library functions do not involve system calls (such as stracat), while others implicitly make system call requests.
7.3 The process behind the system call
Pass system call parameters → execute into instruction (user mode)→ execute system call corresponding service program (core mentality)→ return to user program
Note:
- The trap instruction is executed in user mode, which immediately causes an internal interrupt and puts the CPU into the core state
- The system call request is made in user mode, and the corresponding processing of the system call takes place in the core mentality
- The trap command is the only one that can only be executed in user mode, not in core mode