Basic Principles of Operating System (1)

1.1 What is an operating
system ? The operating system controls the entire process of computer execution, a system that controls the computer situation, abbreviated as OS, has the supreme authority, to put it another way: the operating system is between the computer application software and computer hardware. Software system.
Insert picture description here
What exactly does the operating system control? Of course it is to control everything that happens in the computer! Operations performed without the permission of the operating system are illegal.
1.2 Operating system as a manager It
is a software system that makes computers easy to use. You can summarize the functions of the operating system:
1) Manage the software and hardware resources on the computer for users and their applications
2) Ensure fair competition for computer resources and use
3) to prevent the illegal occupation and use of computer resources
4) to ensure that the operating system itself is operating normally
, depending on the operating system specific functions of management resources include:
1) the CPU management
2) memory management
3) external memory management
4) I /O Management
1.3 User Programs and Operating
System The upper and lower operating systems are the virtual machine interface and the physical machine interface. The hardware under the physical machine is the hardware, and the virtual machine interface is the application software.
The operating system provides a virtual machine interface for the user user program. The application program runs on this interface, which is what we call API calls. The operating system is also a software program, and so is the application program. It is nothing more than the relationship between the call and the called !
The operating system provides various service interfaces to the user program to reach the application program to complete its tasks. When these services are called, control is transferred from the user program to the operating system, and the operating system returns control to the user program after completing these services. In this mode, the user program is the main program and the operating system is the subprogram.
Insert picture description here
On the contrary: the
system gets up first, and then the application calls!
Insert picture description here
Of course, how to understand how to think is a mutual calling relationship, which forms a very complex dynamic relationship!
1.4 The scope of the operating system
CPU management:
that is, process management, the main purpose is of course, the first is fairness, every process has the opportunity to use the CPU, the second is blocking, that is, any program can not endlessly block other programs Promote normally, where blocking may be the need for input and output or something else. The third is priority. Resolutely oppose the big pot of rice, and let a small group of people get rich first! I hope I will not be beaten when I say this.
Memory management: It
mainly manages the memory architecture formed by storage media such as cache, memory, disk, tape, and so on. As a result, the concept of virtual memory has emerged. That is, the physical memory (cache and memory) is extended to external storage media (hard disks, disks, CDs). Another purpose of memory management is to realize that many programs share the same physical memory. This requires partitioning and protecting the physical memory. Do not allow one program to access the memory space occupied by another program, professional data is called runtime cannot cross the boundary.
Storage management:
It can also be called a file system system. Its main purpose is to turn the disk into an easy-to-use storage medium for users to use, so that we don't need to understand the physical characteristics of the disk when accessing the disk.
Device management
Management is to manage input and output devices. It has two purposes: one is to shield the differences of different devices, that is, users and users access different devices in the same way, thereby reducing the difficulty of programming. The second is to provide concurrent access, which means that devices that seem to be unshareable will become sharable.
Insert picture description here
Batch management
provides a program operation mode that does not require human-computer interaction, and is directly handed over to the computer. The main purpose is to maximize throughput and complete the most tasks per unit of time.
Insert picture description here
1. Basic knowledge of computer hardware
Conceptually, the structure of a computer is very simple. First, a bus is arranged, and then each hardware device is hung on the bus. All these devices have a control device. These controllers communicate with the CPU for external devices. All devices The communication between all needs to go through the bus.
Insert picture description here
The functional part of the computer is divided into multiple steps, and each instruction of the computer is divided into the same number of steps. Yes, each instruction flows on the pipeline. The last step of the pipeline is the execution of the instruction, and each instruction on the pipeline The rungs can accommodate one instruction to be executed at the same time.
Insert picture description here
Later, a four-stage superscalar launch structure appeared, as shown in the figure:
Insert picture description here
Storage architecture:
The storage unit of instructions. The storage architecture includes cache, main memory, disk, and tape. In some cases, there are also multi-level caches and external discs. The figure shows a storage architecture composed of a 5-level storage medium including registers.
Insert picture description here
From top to bottom, the access delay and capacity of the storage media in each episode increase in turn. From high to low, the access speed of the register is the fastest and the capacity is the smallest, but the cost is high. On the contrary, through reasonable matching, a storage architecture with a relatively high cost-performance ratio can be formed.
Disk is the main storage medium of a computer. Without a disk, a computer will not be called a computer.
Typical disk structure: The
Insert picture description here
most important mechanism in a computer when it is interrupted is the fundamental guarantee for the operating system to gain control of the computer. Without interruption, it is difficult to imagine how the operating system accomplishes the tasks assigned by people.
The basic principle of computer interruption: The device sends an interrupt to the CPU after completing its task. The CPU judges the priority and then determines whether to respond. If it responds, it executes the interrupt, and continues the original program after the interrupt is completed, as shown in the figure:
Insert picture description here
OK A soft interrupt occurs, that is, an interrupt generated by the software.
2. Kernel mode and user mode:
People are not equal, the resources they occupy are more or less, some people come, you need to give up your resources, some people can use any resource, some people are very few , The procedure is the same. The operating system can access any resource! In order to distinguish the size of power, there are kernel mode and user mode.
Kernel mode: There are many resources, or many resources that can be accessed, so it is also called privileged state.
User mode : non-privileged mode, there will be confine when accessing resources.
For example: If you want to visit the OS kernel data structure (such as the process table), you need to do it in a privileged state. If you access the data in the user program, just be in the user mode.
The greater the power, the more dangerous it is. Everyone understands that in ancient times, high-ranking officials arrested people and set fires. If they wanted to do it, it would make everyone’s life difficult. If it becomes more difficult, it will be a bit miserable. The reliability and safety of the program is particularly important. If the kernel mode program modifies the various kernel data structures of the operating system, it will cause the entire system to crash. As for the program running in user mode, even if there is a problem, it is the application program that crashes, and the operating system will continue to run.
The state in which a program runs depends on the requirements for resources and efficiency. Generally speaking, if a program can run in user mode, it does not need to be placed in kernel mode, unless it is unavoidable! Or understand it this way, as the emperor (especially the editor), all events that affect the stability of our country will run in the kernel mode. I don’t need to deal with matters under local jurisdiction, only those related to user data and applications. Things are executed in user mode. In addition, things that require extremely high timing should also be done in the kernel mode. For example, if I (I) drive out to play, will there be traffic jams (the road will be freed up a few days in advance)? ?
Examples of running programs in kernel mode:
Both CPU management and memory management must be in kernel mode. Why are they necessary? If it runs in user mode, it is extremely unsafe! It's like my army is handed over to readers to manage (a mountain cannot tolerate two tigers, readers cannot be emperors now, they may be in the future), surely not! Very dangerous to me.
Diagnosis and testing procedures also need to be implemented in the kernel mode, because it needs to access all resources to know if there are any problems, a bit like our security software (Tencent, 360, etc.). The same is true for input and output management. To access various devices and underlying data structures, you must also be in the kernel mode!
For the file management system, one part is placed in the user mode and the other part is placed in the kernel mode. The management of the file system itself, that is, the management of the macro data part of the file, must be placed in the kernel mode, the compiler, some functions of the network management, and the editor. User programs are naturally executed in user mode. As shown in the figure:
Insert picture description here
3. Situation recognition.
Correctly making the judgment of the kernel state and the user state is essential to the correct operation of the system. Obviously, making this judgment requires a certain flag, which is a state of the processor. This status bit is a word bit in the CPU status word. By setting this word bit, the CPU can be set to kernel mode and user mode, or other sub-states (some CPUs have more seed states). When a program is running, What is the status of the CPU. What state is this program running in.
4. How to realize the kernel mode and user mode.
As mentioned earlier, the internal state is a privileged state, and the user mode is a normal state. How is this restriction implemented?
To restrict a program's access to resources, each instruction needs to be checked. This check is address translation. Through the control of address translation, access to resources can be restricted. In order to give the kernel mode program access to all resources, when the system is in the kernel mode, the kernel program can bypass the memory address translation and directly execute privileged instructions, such as shutdown instructions. This approach of bypassing translation breaks through the control of system resources.
5. Operating system structure.
Various functions are classified into different function blocks. Each function block is relatively independent and connected with each other through a fixed interface. Any function block can call the service of another function block, as shown in the figure:
Insert picture description here
But this operation There are many shortcomings in the system. Modifying any function will cause other functional modules to be modified, which increases the difficulty of design. This kind of hierarchical network connection is likely to cause loop calls and form deadlocks, thereby reducing the reliability of the operating system, thereby reducing the reliability of the operating system. With a hierarchical operating system, low-level functions provide services for the next-level functions, and high-level functions can provide services for higher-level functions. As shown:
Insert picture description here
In the above two pictures, all the functions of the operating system are running in the kernel mode, which requires the time cost of changing from the user mode to the kernel mode, and its security and reliability will be reduced. With the increase of hackers, the operation The more things in the system, the more codes run in the kernel mode, the more vulnerabilities, and the lower the security. The current operating system has reached 40 million lines. The structure of the microkernel is the core of the operating system. The core runs in the kernel mode, and everything else is put in the user mode. At the same time, efficiency and safety are improved.
As shown in the figure:
Insert picture description here
1. Process
The core concept of the operating system, a program in motion (program in progress), once a program moves in the computer, it becomes a process. The operating system manages the process through the process table. Because all the information about the process is stored in the process table, at any time, all the resources occupied by the process, including the memory allocated to the process, the kernel data structure and software resources form a process core (core). The nuclear snapshot represents the state of the process at a specific moment.
Processes can communicate, synchronize, and compete with each other, and under certain circumstances, a deadlock may be formed .
2. Memory
It is the place where the process is stored, so the management of the memory is particularly important. The memory management makes the data read and write efficient, high security, high space utilization and location transparency!
3. File A
file is an abstraction of the external storage device provided by the operating system. It is the final storage location of programs and data. How to make the user's data storage easy, convenient, reliable and safe is a matter of the file system.
4. System calls The
operating system is a system program that provides services in the form of system calls, that is, the application program interface (interface) provided by the operating system, which also becomes an API.
For example: if the user program needs to perform all disk operations, the program code uses: result=read(fd,buffer,nbytes); the read function is the library function provided by the C language, and the library function itself is the calling operation The read system call of the system, after seeing the above statement on the compiler, expand the read library function to read system call.
The system call can be divided into:
1) Process control type
2) File management type
3) Device management type
4) Memory management type
5) Information maintenance type
6) Communication type
System calls are generally divided into three stages:
1) Parameter preparation In the stage, the
system service program needs to be used to push the parameters required for the system call, such as the fd, buffer, nbytes mentioned above, onto the stack, and then call the library function read. The library function read puts the code of the system call read into an agreed register. , Pass control to the operating system through trap (a kind of interrupt method)
2)
After the operating system gets the control right in the system call recognition phase , the system call code is taken out of the register, and a system call maintained by the operating system Tables are compared to obtain the memory address of the program body where the system calls read, and then jump to this address.
3) System call execution phase
Execute the system call function, and return to the user program after the system call is completed.
As
Insert picture description here
shown in the figure, the parameter transfer is carried out through the stack, but this is not the only way to transfer parameters. The most efficient way is to use registers. , The access speed of the register is faster than that of the stack, which can improve the efficiency of call execution by the tycoon.
5: shell
The use of API requires programming, but for users who do not want to program or who need to interact with the operating system, they can use the shell provided by the operating system to interact with the user. Every operating system has it. It can be either text or text. It is a user interface overlaid on the operating system services. The user uses functions similar to the C language library and uses utilities to obtain operating system services. powershell is a text command shell.
The functions of a shell include the following items:
1) Display prompts, such as the prompt $ and% under UNIX
2) Receive user commands and execute
3) Realize indirect input and output
4) Start background processes
5) Perform job control
6) Provide Pseudo Terminal Service
Once the shell is started, it will loop until infinite
1) Display the command prompt
2) Wait for the user to enter commands
3) Use fork to create a child process
4) Use execve to execute the command entered by the user in the created child process
5) Repeat the above steps
. The explanation of each concept is briefly described above, and we will explain in detail below.

Process details

The operating system manages and implements the CPU by means of processes and threads
. 1. Explain
Insert picture description here
the three cores of the operating system:
1) Process management (CPU management)
2) Memory management
3) File management
process=program+execution
CPU usage efficiency is not High makes processes appear and execute concurrently. These programs that exist in the computer memory at the same time are called processes, and multi-processes realize multi-programming.
Insert picture description here
Each process occupies a piece of storage space (memory), and the physical program counter can check the specific program code being executed. At the physical level, all processes share a program counter. From a logical level, each process needs its own counter, so there can be many program counters. In terms of time, each process must move forward, that is, a certain amount of work must be completed within a certain period of time.
Insert picture description here
Processes do not necessarily have to terminate. In fact, many system processes will not terminate (processes used to provide services to other processes) unless they are forced to terminate or shut down the computer.
2. The emergence and disappearance of what can be done:
When a system is initialized, there will be many processes produced. The existence of these processes makes the execution of new processes and user programs possible. After the system is initialized, the system waits for the user to input a command. If the user starts a program, such as double-clicking an executable file, the system will create a process for this file. In addition, users can also directly generate new processes through system calls such as fork or createprocess in the loaded program.
The time that causes the process to die can be divided into four situations:
1) End of life: the task is completed and launched // Voluntary
2) Suicide: The process exits by mistake // Voluntary
3) Mortality: The process kills other processes // Involuntary usually To kill a child process for a parent process, the user can only kill his own process, but a super user can!
4) Execution: The process is forcibly terminated due to a field//involuntary. It may be that a process has performed an illegal operation, such as accessing out of the street or dividing by 0. This illegal operation will be captured by the operating system. Terminate the process that caused the exception.
3. Hierarchical structure of
processes A process can create a new process through system calls. This newly created process is called your child process, and the process that creates the child process is called the parent process. The process tree is the name for the cyclically created process.
Processes in UNIX have a hierarchical structure, so there are different levels, but Windows does not have the concept of a process tree, and all processes have equal status.
4. The state of the
process A process can be executed on the CPU, which is the execution state. What if the CPU hangs? That depends on why! Because the OS needs to select one of the suspended processes to execute when scheduling, it is necessary to clear the cause of a process suspension to effectively promote the scheduling.
If a process performs some blocking operation during execution, such as reading and writing disks. Because blocking needs to wait for the result before continuing execution, the operating system will hang up this process and let other processes run. The other is that a process takes too long to execute, and it will also be suspended, allowing other processes to have a chance to execute.
In this way, we divide the suspended process into two categories: ready state, and blocking state.
Another state is the execution state. As shown in the figure,
Insert picture description here
you can see whether the arrows point to each other. Many commercial operating system processes may have more than three types. It's just the same.
5. Process creation
steps:
1) Allocate the process control block
2) initialize the machine register
3) initialize the page table
4) read the program code from the disk into the memory
5) set the processor to "user mode"
6) jump to the program The starting position (setting the program counter) is
different for different operating systems, and the creation method is different
. 6. Process and address space
The process space also becomes the address space, that is, all the resources needed by your process. All resources constitute the division of state. It is impossible for two processes to be exactly the same.
The main content of the process and address space research is how to make multiple process spaces share a physical memory. Specifically, it is to efficiently and safely let all processes share this physical memory.
7. The means required for process management are managed
by the operating system. Some information about the process must be maintained. The structure used by the operating system to maintain process records is the process table or process control block. This table or pcb is stored in the process. This information includes registers, program counters, status sub, stack pointer, priority, process ID, signal, creation time, CPU consumed, various handles of the current travel, etc. The adopted data structures are mainly linear lists, linked lists and structures.
Insert picture description here
This process table is kept in the kernel space where the operating system is located, as shown in the figure:
Insert picture description here
8. Defects in the
process The process makes our users feel that they have exclusive use of a CPU, he can only do one thing at a time, if you want to do more at the same time For one thing, the process is not enough.
In addition, if the process is blocked during execution, the entire process will be suspended and cannot continue. It is because of the above two reasons that threads were invented!

Guess you like

Origin blog.csdn.net/weixin_42271802/article/details/105947343