In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to understand the operating system". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand the operating system".
1. What is the operating system?
To understand what the operating system is and what the operating system is for, we also need to start with the origin of the requirements of the operating system.
In 1946, "the world's first general-purpose computer ENIAC" was born at the University of Pennsylvania in the United States, using 18000 electronic tubes, covering an area of 170square meters and weighing 30 tons. Most terrifying of all, ENIAC, or "early computers had no operating system", operated the 170sqm behemoth by the manual operation of engineers. What does it feel like? Please take a look at the following picture:
In the picture, the big machine all over the room is the ENIAC. From the photo, we can see an operator manually adjusting a button, and there are thousands of buttons like this. It is conceivable that in an era without an operating system, engineers need to operate directly with this anti-human hardware panel, and the human cost of running and maintaining a computer is almost unimaginable.
From this, it leads to one of the most important functions of the operating system: "to manage, allocate and schedule computer hardware resources." "
With the operating system, how much memory a program needs to occupy and which memory space to allocate; there is only one CPU, how to run multiple programs at the same time, users do not need to care, let alone to learn those obscure underlying expertise such as digital circuit logic, computer composition principles, etc., all leave it to the operating system!
In other words, "the operating system encapsulates all the underlying hardware into a black box", and the user can complete the interaction with the underlying hardware by sending instructions directly to the operating system.
So, who is the operating system? Let's take a look at what the "early operating system Disk Operating System,DOS" (disk operating system) looked like:
Some students said that this is not the cmd command line we use, no mistake, but it is the whole screen is such an interface, full screen is! Not in a dialog box.
Obviously, it is much easier for DOS to enter commands through the keyboard than to manually adjust the buttons and connect the lines. However, such a command line interface (CLI) must not be in line with the taste of most users, so operating systems with a good graphical interface (GUI), such as Windows, have gradually become mainstream, after all, they can be operated only through mouse clicks, and the threshold is low enough.
Thus it can be seen that the mode of operation of an operating system and the beauty of the interface are very important to users.
This leads to the second major function of the operating system: "to provide users with a friendly, clear and simple user interface" (professionally called "shell Shell"), users perform all the functions provided by the operating system through Shell. For the command line, you need to provide enough commands; for the graphical interface, you need to provide enough buttons.
⭐ summarizes: the operating system is essentially software / programs running on the computer. As the first layer of software on the basis of hardware, operating system is the bridge between hardware and all kinds of software. Its function can be roughly divided into two parts:
"manage computer hardware and software resources"provide users with an interface to interact with the system"
two。 A brief introduction to computer hardware
In order to work, the operating system must understand a lot of hardware, or at least how the hardware faces programmers. For this reason, let's first briefly introduce the hardware in modern personal computers, and then discuss the details in the operating system.
A simple personal computer can be abstracted into a model similar to the one in the following figure:
CPU, memory, and Istroke O devices are all connected by a system bus and communicate with other devices through the bus.
Processor
The so-called processor is what we often call CPU, which is the "brain" of the computer. CPU fetches the instruction from memory and executes it. The execution cycle of an CPU is to extract an instruction from memory, decode it, determine its type and Operand, then execute it, and then extract, decode, and execute the next instruction. Repeat the loop until the program finishes running.
To put it simply, just remember this sentence: "A program needs to be put into memory and assigned CPU before it can be executed."
Storage device
The second major component of a computer is memory. "ideally, memory should be" very fast ", faster than executing an instruction, so that the efficiency of CPU execution is not affected by memory, and is" large enough and very cheap ".
However, the current technical means can not meet these three goals at the same time, so there are different ways to deal with it. The memory system adopts a hierarchical structure, as shown in the following figure, "the top memory has the fastest speed, the smallest capacity and the highest cost, and the lower the memory, the slower the speed, the larger the capacity, and the lower the cost."
Why is the speed of accessing the register so fast? That's because the material of the register is the same as that of CPU, so there is almost no delay compared to accessing CPU.
The next level of registers is "caching", which is no stranger to you. Usually, the use of caches will lead to performance improvements. The operating system uses caching all the time, such as keeping frequently used files in memory to avoid repeatedly fetching them from disk.
The next layer is "memory", also known as "main memory", which is often referred to as "Random Access Memory" ("RAM"). All access requests that cannot be satisfied in the cache are transferred to memory.
In addition to memory RAM, many computers have a small amount of "non-volatile random access memory" (Read Only Memory, "ROM"). Unlike RAM, ROM does not lose its content after a power outage, and its content will not be modified once it is stored. And ROM is very fast and cheap.
The next level is "disk", which has a larger capacity. the only problem with disk is that the time for random access to data is about three orders of magnitude slower than memory. The reason for its low speed is that "the disk is a mechanism and has a special structure," as shown in the following figure:
There are one or more metal "disks" in a disk that rotate at a speed of 5400, 7200, 10800 rpm or more. Starting from the edge, there is a "robotic arm" hanging across the disk, and you can recall the old-fashioned plastic record player. The information is written on a series of concentric circles on the disk. At any given arm position, each "head" can read a circular area called a "track". Combine all the tracks at the position of a given arm to form a "cylinder".
Each track is divided into several "sectors" with a value of 512 bytes. In modern disks, there are more sectors than the outer cylinder than the inner cylinder. It takes approximately 1ms for the robotic arm to move from one cylinder to the next. The typical time to randomly move to a cylinder is from 5ms to 10ms, depending on the drive. After the arm reaches the correct track, the drive must wait for the required sector to rotate under the head (which probably requires a delay from 5ms to 10ms) before starting to read and write, the speed of the low-end hard disk is 50MB/s, while the speed of the high-speed disk is 160MB/s.
Ipaw O equipment
CPU and memory are not the only resources that need to be managed by the operating system. As shown in the following figure, the Istroke O device generally consists of two parts: the device controller and the device itself, such as the keyboard controller and the keyboard.
A "device controller" is actually a chip or a group of chips that can receive instructions from the operating system and control physical devices. For example, read the data from the device and complete the data processing.
In many cases, the process of actual control of the equipment is very complex and there are many details. So the job of the controller is to provide a simpler (but still very complex) interface for the operating system.
The other part of the Icano device is the "device itself". The device itself has a relatively simple interface, because the interface can not do much work, and it has been "standardized". After standardization, any disk controller can adapt to any kind of disk, so standardization is very necessary.
3. Four characteristics of operating system
The operating system has four distinct characteristics: concurrency, sharing, virtual and asynchronous. Among them, "concurrency and sharing are the most basic characteristics of the operating system". Without concurrency and sharing, there can be no virtual and asynchronous.
Let's take a brief look at these four features, many of the detailed concepts involved will not be explained in this article, and will be updated one after another.
① concurrent
The twin brothers of concurrency and parallelism often confuse beginners.
"concurrency": concurrency refers to the ability to run multiple programs at the same time "over a period of time" macroscopically. Of course, these programs occur at the same time macroscopically, but alternately microscopically. The operating system enables programs to run concurrently by introducing processes and threads.
"parallel": parallelism means that multiple instructions can be run at the same time, and that two or more events occur at the same time. Parallelism requires hardware support, such as multi-pipeline, multi-core processors or distributed computing systems.
With the understanding of the image in the following picture:
When it comes to concurrency and parallelism, we have to mention CPU. As the brain of a computer, CPU mainly interacts with memory, extracting instructions from memory and executing them. A program needs to be put into memory and assigned CPU before it can be executed, so the ability to run concurrently is closely related to the performance of CPU:
"single-core CPU" can only execute one program at a time, and each program can only be executed "concurrently"; "multi-core CPU" can execute multiple programs at the same time, and multiple programs can be executed "in parallel".
For example, Intel's eighth generation i3 processor is a 4-core CPU, which means that four programs can be executed in parallel. Of course, even for a 4-core CPU, concurrency is still essential as long as there are more than four programs that need to run "simultaneously", so "concurrency is one of the most basic features of the operating system."
② sharing
Sharing is "resource sharing", which means that the resources in the system can be used by multiple "concurrent" processes in memory.
There are two main ways to share:
1) "mutually exclusive sharing"
The so-called mutually exclusive sharing means that although this resource is shared and all processes can use it, "only one process is allowed to access the same resource at a certain time", also known as mutually exclusive access, which requires a synchronization mechanism to achieve mutually exclusive access. Mutually exclusive shared / accessed resources are called "critical resources".
Take an example of mutually exclusive sharing:
If we use QQ and Wechat video at the same time, camera resources can only be allocated to one of the processes at the same time.
2) "share simultaneously"
Simultaneous sharing is the opposite of mutually exclusive sharing, which "allows multiple processes in a period of time" to "access some resources in the system" at the same time. Of course, the so-called "simultaneous" is often macro, while at the micro level, these processes may alternately access the resource (that is, time-sharing).
Take an example of sharing at the same time:
Use QQ to send file An on your hard drive and Wechat to send file B on your hard drive. From a macro point of view, both sides are reading and sending files at the same time, accessing hard disk resources and reading data from them. From a micro point of view, the two processes QQ and Wechat access hard disk resources alternately.
Concurrency and sharing, as the two most basic features of the operating system, are actually conditions for each other's existence. Why would you say that? In this way, let's continue to look at the relationship between concurrency and sharing through the above example of sending files (using QQ to send file An on your hard disk and Wechat to send file B on your hard disk):
Concurrency: two processes are executing concurrently.
If concurrency is lost and only one program is running in the system, sharing loses its meaning.
The embodiment of sharing: two processes need to share access to hard disk resources.
If sharing is lost, QQ and Wechat cannot access hard disk resources at the same time, so they cannot send files at the same time, and concurrency is out of the question.
③ virtual
First, it is defined as "turning a physical entity into several logical counterparts". The physical entity (the former) is real, while the logical counterpart (the latter) is felt by the user.
This is a bit illusory. Use an example to understand:
As mentioned above, a program needs to be put into memory and assigned CPU before it can be executed. For example, GTA5 needs the running memory of 4GB, QQ needs the memory of 256MB, Chrome needs the memory of 512MB, NetEase Cloud Music needs the memory of 256MB. Suppose our computer has only 4GB memory and CPU is single-core. So there are two problems here:
Question1: these programs need much more memory to run at the same time than 4 GB, so why can they run on my computer at the same time?
A: this benefits from the "space division multiplexing technology" of virtual memory technology. Although our computer actually has only 4GB memory, it can run these programs that are much larger than 4G memory at the same time. "it seems to users that our computer memory is much larger than 4GB."
"Question2": since a program needs to be assigned a CPU to run properly, why can so many programs be run on a single-core CPU computer at the same time?
A: this is due to the "time division multiplexing" of virtual memory technology. Although there is actually only one single-core CPU, it is impossible to execute so many programs in parallel at the same time, but microscopically, CPU alternately serves each process in a small period of time. "in the eyes of users, it seems that our CPU is multicore."
Let's explain the above two virtual technologies:
1) "Space Division Multiplexing"
The principle of space division multiplexing technology is to use memory as a cache, only to save some programs that are most frequently used, and to put most of the programs on disk. This mechanism requires a fast image memory address in order to convert the address generated by the program to the physical address of the byte in memory. This image is done by a part of the CPU called the "Memory Management Unit" ("MMU"), which we have seen above and reviewed:
2) "time division multiplexing technology"
Multiple processes can execute concurrently on the same CPU because of the use of time division multiplexing technology, so that each process takes turns to occupy the processor, executing only a small time slice at a time and switching quickly.
Obviously, if concurrency is lost, only one program can be run in the system in a period of time, then it loses the meaning of realizing virtuality. Therefore, "without concurrency, there can be no virtuality."
④ async
Asynchronous means: 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 to the end, but stop-and-go, moving forward at an unpredictable speed." this is the asynchrony of the process.
For example: scum has a concurrent appointment with two girls at 8: 00 and 12:00, each girl has a two-hour date. Dating with No.1 and No.2 are two concurrent programs, while scum's heart is a limited system resource.
Because the programs running concurrently will scramble to use the system resources, and the resources in the system are limited, the 2-hour appointment time of the same old scum on the same number one or two may not end at once, but intermittently. Maybe from 8 to 9, from 9 to 10:00, then from 10:00 to 11:00, and finally from 11:00 to 12:00.
If concurrency is lost, that is, the system can only run programs serially, then the execution of each program will be consistent to the end. Therefore, "it is possible to cause asynchrony only if the system has concurrency."
4. Learn the core content of the operating system
The core contents of this series are as follows:
Process management storage management file system management Imax O device management ① process management
The concept of Process has appeared many times above. In order to better realize the concurrency and sharing of the operating system, the process is introduced.
"A process is an execution of a program, and it is temporary. Contains not only the running program entity, but also all the system resources occupied by the running program, such as CPU, memory, network resources, and so on. When answering the concept of a process, many partners will only say that it is a running entity and ignore the resources occupied by the process. For example, if the same program is run twice at the same time, then they are two separate processes.
When it comes to processes, you have to say, "there can be multiple threads in a process, and they share process resources." "
For example, QQ and Chrome browsers are two processes, and there are many threads in the Chrome process, such as HTTP request thread, event response thread, rendering thread, and so on. The concurrent execution of threads allows the browser to respond to other events of the user when a new link is clicked in the browser to initiate a HTTP request.
What we call process management actually includes two main contents:
Process communication process scheduling
"process communication", as its name implies, is the mutual communication between processes. CPU is the most precious resource of the computer, a process needs to put in memory and assign CPU to it before it can be executed, and a program may contain multiple processes, which requires processes to communicate with each other, synchronize with each other, and complete the program together. For example, if process A produces data and process B prints data, then B has to wait before A generates data, so does A have to send B a message telling B that I have produced the data and you can print it?
"process scheduling" means that, in general, multiple processes or threads compete for CPU at the same time. If only one CPU is available, CPU must choose the next process or thread to run. The process of this choice is process scheduling.
② memory management
Cache as the lowest memory except registers, its management is completed by hardware, so what we learn is focused on memory management.
In fact, this topic has been mentioned when we virtualized this feature in the four features of the operating system above. Memory is generally used to hold executing programs. In a very simple operating system, only one program can be run in memory at a time. If you want to run the second program, the first program must be removed from memory and then loaded into memory.
Obviously, this is very inefficient, so virtual memory technology is introduced to use memory as a cache, only to save some of the programs that are most frequently used, and to put most of the programs on disk.
So what memory management does is probably:
Put some frequently used programs into memory when the memory is full, replace some parts of the memory ③ file system management
A file is actually an information logic unit created by a process. A disk may contain thousands or even millions of files, each of which is independent of other files. In fact, it is easier to understand the nature of each file as an address space.
Files are also managed by the operating system, and the construction, naming, access, use, protection, implementation and management of files are all the contents of the design of the operating system, which is the theme of the file system.
④ I PUBO device Management
The operating system must efficiently manage the IWeiO device. It needs to send commands to the IWeiO device, catch interrupts, and handle various errors on the device. It should also provide a simple and easy-to-use interface between the device and the rest of the system.
5. Kernel state and user state
The proper term kernel may confuse everyone a little bit. Explain here. Modern operating systems adopt the concept of process, in order to better deal with the concurrency and sharing of the system, and make the process work harmoniously, it is far from enough to rely on the functions provided by computer hardware. For example, the scheduling of processes cannot be realized by hardware. The hardware resources must be modified with a set of basic software in order to provide a good running environment for the execution of processes. This software is the kernel.
To put it simply, "the kernel is a group of program modules in the operating system", as trusted software to provide basic functions and basic operations that support the concurrent execution of processes, "with access to hardware devices and all memory space". It is no exaggeration to say that the kernel is the core of the operating system.
Of course, in addition to kernel programs, the operating system also includes other basic components, such as text editors, compilers, programs used to interact with users, such as desktop systems, and so on.
What is kernel state and user state of ①
So since the kernel is a program and it needs to be run, it must be assigned CPU. Therefore, there are two kinds of programs running on CPU, one is the operating system kernel program (also known as the system program), and the other is the application program. The former completes the system task, while the latter realizes the application task. There is a relationship between controlled and controlled, the former has the right to manage and allocate resources, while the latter can only apply to the system for the use of resources.
Obviously, we should distinguish between these two types of programs running on CPU, which is why kernel state and user state appear.
"kernel mode": when CPU is in kernel state, this is the state in which the operating system hypervisor (that is, the kernel) runs. Programs running in kernel mode can access any resources of the computer without restrictions and do whatever they want, such as coordinating CPU resources, allocating memory resources, providing a stable environment for programs to run, and so on. "user mode" (user mode): applications are basically run in user mode, or user mode is to provide space for applications to run. Programs running in user mode can only access the address space where the current CPU executes the program, which effectively prevents the operating system program from being infringed by the application program.
For the operating system, "what kind of program should be put in the kernel state"? This depends on factors such as the demand for resources, the urgency of time and the level of efficiency. For example, CPU, memory, devices and other resource manager programs should run in kernel mode, otherwise there is no guarantee of security. For file systems and data, file system data and management must be in the kernel state, but user data and management can be placed in the user state.
② interrupt mechanism
Where appropriate, the kernel of the operating system will actively cede the right to use CPU to the application, that is, to change the CPU from kernel state to user state. However, if CPU wants to return to kernel state from user mode, it can only be done through interrupt mechanism. If there is no interrupt mechanism, once CPU runs on the application (user mode), CPU will run the application all the time. In other words, "interrupt is the only way for the operating system kernel to regain the right to use CPU." It can be said that "the operating system is driven by interrupts."
Of course, the interrupt mechanism here is very broad, including three means, that is, there are three means to transition from user state to kernel state:
1) the program requests the operating system service, executes the system call 2) when the program is running, it generates an external interrupt event (such as Icano operation is completed), the running program is interrupted, and the interrupt program is turned to interrupt program processing. 3) the internal interrupt (exception) event occurs while the program is running, the running program is interrupted, and the exception handler is turned to work.
All of the above three methods occur through the interrupt mechanism, so let's take a look at the types of interrupts.
According to whether the interrupt signal is external or internal of the CPU, the interrupt type is divided into external interrupt and internal interrupt:
"external interrupt" (also known as interrupt, interrupt in a narrow sense)
The external interrupt has nothing to do with the currently executed instruction, and the interrupt signal comes from outside the CPU. If CPU O completes the interrupt, it means that the device input / output processing has been completed, and the device can send the next input / output request. In addition, there are clock interrupts, console interrupts and so on.
"Internal interrupt" (also known as exception, exception)
The internal interrupt is related to the currently executed instruction, and the interrupt signal comes from within the CPU. Such as illegal opcode, address out of bounds, arithmetic overflow, divisor 0 and so on.
Here is a brief explanation of the basic principles of the interrupt mechanism: different interrupt signals must need to be handled by different interrupt handlers. Then when CPU detects the interrupt signal, it will query the "interrupt vector table" according to the type of interrupt signal, so as to find the location of the corresponding interrupt handler in memory.
③ system call
As mentioned above, the program can also change the CPU from the user mode to the kernel state by executing the system call. So what does system call have to do with interrupts? Why is this method also a kind of interrupt mechanism? That's because "the system call is done by getting caught in an instruction," which raises an internal interrupt.
To be more specific, the operating system, as the first layer of software on top of computer hardware, needs to provide some easy-to-use services to the upper layer, including users and applications:
The interfaces provided to users are graphical interface GUI and command interface, and the application program interface is "program interface". This program interface is composed of a set of system calls and is provided by the operating system for developers. It can be understood as a special function that can be called by the application. "the application can request the service of the operating system kernel through the system call."
The abbreviated version of "procedure for system calls" is roughly as follows:
1) in user mode, the application passes system call parameters
2) the execution of the instruction is trapped, causing an internal interrupt to put the CPU into the kernel state.
3) kernel state, execute the corresponding request, and the kernel program handles the system call
4) return to the application
Think about "Why system calls are necessary"?
For example: we go to the school print shop to print the paper, and you press the "print" option of WPS, so the printer starts working. When your paper was halfway through printing, another classmate pressed Word's "print" button and started printing his own paper. Imagine what would happen if two processes could share printer resources at will and concurrently?
Obviously, the two processes run concurrently, which causes the printer device to receive print requests from WPS and Word processes alternately. As a result, the contents of the two papers are mixed.
How to solve this problem? This requires unified management of shared resources within the operating system, and provides "system calls" to the upper layer. Applications or processes running in user mode want to use shared resources such as printers. Requests can only be made to the operating system kernel through system calls, and then the kernel will coordinate the processing of each request (process scheduling).
From the above example, we can sum up what functions will be used in system calls: "all operations related to shared resources (such as memory allocation, IUnip O operation, file management, etc.), must make a request to the operating system kernel through the system call, which will be completed by the operating system kernel." This can ensure the stability and security of the system and prevent users from operating illegally. These system calls can be roughly divided into the following categories by function:
Equipment management. Complete the request or release of the device, as well as the device startup and other functions. Document management. Complete the functions of reading, writing, creating and deleting files. Process control. Complete the creation, undo, blocking and wake-up functions of the process. Process communication. Complete the functions such as message transmission or signal transmission between processes. Memory management. Complete the functions of memory allocation, recycling and getting the memory area size and address occupied by the job. Thank you for your reading, the above is the content of "how to understand the operating system", after the study of this article, I believe you have a deeper understanding of how to understand the operating system, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 240
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.