Operating System understanding

Source: Internet
Author: User
 
Roger's curiosity

Roger thinks the operating system is the most amazing invention in the world. He can zoom in and out the window at will, drag the window to any position on the screen, and switch the windows at will, which will not interfere with the normal operation of the software. No matter how you zoom in or out, move the position, switch the window, the button, the text box, or the text box. When watching movies with storm audio and video, you can let Thunder slowly download another movie. At this time, QQ may suddenly flash up and tell you a friend sent a message. Roger was excited by curiosity: Who can tell me how it was done?

As a result, he went to listen to the operating system principles course with great enthusiasm, hoping to unveil his own mysteries. The professor is indeed an expert in operating systems, but his courses are full of processes, threads, and file systems. During the entire semester, Roger found that his problem still exists. He still does not know how the operating system successfully manages a large number of software. He wanted to talk to a classmate, but everyone was not interested in this topic. No one was willing to think about such a "profound" question.

Roger is confused and confused. Finally, he had no way to go, and finally decided to use the most stupid way to redeem his curiosity: Write the operating system on his own!

One man's battle

No comrades-in-arms are alone, but they are also the most powerful. In those days, Roger worked hard to write down his own OS. In a word written by tianchao, it is concluded that it is particularly able to endure hardship, fight, tackle problems, and contribute. Finally, after suffering from the 7-fold hell, he finally completed the operating system kernel with four processes running at the same time, followed by an epiphany ------ An epiphany about the running environment.

Essence of Runtime Environment

The term "runtime environment" generally comes from the English run time environment. The running environment is an environment for running programs (haha, it seems like nonsense ).

Hardware System and Operating System

Why does the x86 hardware system (the Von noriman System) perfectly support the operating system? Because it provides the operating system environment through a series of mechanisms. This runtime environment includes: ds/CS/ES/SS, general register group, gdt, LDT, IDT, Ram, Rom, and so on. In fact, from the perspective of the hardware system, there is no concept of an operating system, nor the concept of processes, threads, and graphics software. From the perspective of hardware systems, from power-on to power-offOnly one programIt runs on it and has no idea about the internal details of the Program (including the operating system, process, thread, and other concepts. For a hardware system, there is no difference between a boot loader and a Linux system. Now let's talk about the relationship between the hardware system and the operating system. Next we will talk about the relationship between the operating system and processes.

Operating system and process

How does the operating system support independent and concurrent running of multiple processes? It provides a runtime environment for every process. If you read the Linux source code, you will find that the operating system maintains a process table for each process. This process includes ds/CS/ES/SS, general register group, gdt, LDT, IDT, Ram, Rom, and so on. Haha, isn't it familiar. Yes. This one is very familiar.The process table is essentially a complete ing of the hardware system!Whenever the operating system wants to run a process, it will first save the status of the current process to the progress table, and then load the progress table data of the target process to the entire hardware system. In this way, the process is completely "Spoofed" by the operating system. In the view of a process, it runs continuously from beginning to end, and no one will disturb it. The operating system is like a container, which contains many "buckets" and each bucket has a process. When you need to "run" a process, the operating system will remove the current "Bucket" from the "socket" and put it back into the container, then install the target bucket on the socket. (By The Way, thread. The operating system uses the thread table management thread. Each process has its own thread table. The content of a thread table may include: SS/PC, general register group, and LDT. Therefore, when switching threads in the same process, you only need to switch the SS/PC, general register group, and LDT. The context switch of the so-called thread has a lower cost than the process .)

Then, how does the operating system obtain the CPU execution right when a process is running? After all, the process does not take the initiative to give control to the operating system. The answer is: clock interruption. When an interruption occurs, the hardware system terminates the execution of the current Code and executes the code directed to the interrupt vector. The operating system re-obtains the CPU execution right through the interrupt function.

The second Nirvana

Roger finally unveiled the first layer of the operating system, and his inner joy is beyond words. Now, he has a deeper understanding of the questions raised at the beginning of the article. The essence of these problems is communication and data distribution. After days of practice, he went further and realized the following truth.

Communication

The hardware system tells the operating system about what happened in the external world. Roger defines it as "communication ". How can we complete communication? The answer is: interruption. When Roger knocks on the keyboard, clicks a mouse, or inserts a USB flash drive in his notebook, or another computer sends a network package to Roger's notebook, hardware interruption is triggered. Each type of interruption has a unique identify ID, as if each person has an ID card number. When an interruption occurs, x86 searches for the corresponding handler for the interruption in the IDT (Interrupt Descriptor Table), and then gives the control to this program (that is, the operating system ). In this way, the operating system obtains information from the outside world. To send information to the outside world, you only need to use the Assembly interface command to operate the keyboard, mouse, or Nic.

Data Distribution and Interaction Interface"

Now, the OS obtains data. How can we distribute data to various processes? Roger intends to provide an operating system API ----- get_key_input () for the process (). If the process calls this function, when Roger knocks on the keyboard, it can get the input characters from the return value. At the same time, Roger planned to use the Linux Command Line input style to provide users with multiple consoles. For a program that uses the get_key_input () function, the console waits for the user's keyboard input. In the same console, you cannot run two programs that call get_key_input () at the same time (you can try to start two such programs in the same console of Linux and observe the response, haha ). Therefore, when a program is started, the OS associates it with a console. If the program calls the get_key_input () function internally, the OS will distribute the keyboard input of the console associated with it to this program.

Abstract view of the console above. In fact, a "console" represents the concept of "interactive interface. Each process has a set of interactive interfaces, such as keyboard input, mouse clicks, and network read/write. If you think of the keyboard input of the entire OS as a large interface of 100 m², 20 m² may belong to process a, and 50 m² may belong to process B, and the remaining 30 m² may belong to process C. In which region the character entered by a keyboard falls, the OS will distribute the character to the process to which it belongs.

The concept of "interactive interface" is interpreted.

In Windows XP, the entire desktop is the interactive interface of the mouse. Each region belongs to a process, and the OS maintains this set of information. The OS updates the relationship between the region and the process accordingly when the window is enlarged, zoomed, dragged, and switched. In this way, no matter where the mouse clicks occur, the OS will know exactly which process the mouse event should be distributed.

The keyboard input is a little more complex. The OS records a status: keyboard focus. For example, if Roger clicks the browser window with the mouse, the keyboard focus will change to an input box or button in the browser (in fact, each process also records its own keyboard focus; when you click your mouse, the previous focus is restored ). Every time Roger knocks on the keyboard, the OS will distribute the input to the focus process.

The network is simpler. Each network connection uses the IP address and port of both parties as the unique identifier. That is, (ip_a: port_a, ip_ B: port_ B ). By checking the IP address and port information in the network package, you can determine which process to distribute.

Homogeneous Window Software

After a lot of training, Roger finally completely solved his own mystery about the operating system. On this day, a student came up with a question: Roger, can you explain the event trigger principle of MFC? To be honest, Roger has never studied this problem. But with a deep understanding of the operating system, he gave the answer after just 10 minutes.

What is MFC? Is a desktop software development framework. When using MFC, developers only need to drag buttons, input boxes, and menus to the window and add several response functions. A desktop software was born! For the first time contact with MFC, It is a magic thing. So what is the principle behind it?

One sentence:The event triggering mechanism of MFC and the message distribution mechanism of the operating system are homogeneous..

Why are the MFC and operating systems homogeneous? The relationship is as follows:

MFC === Operating System

Loop === interrupt

Window component === Process

The operating system captures messages through interruption, and the MFC does not interrupt. It uses a simple loop. In each loop, first query whether there are messages from my interaction interface? If not, continue the next cycle. If yes, retrieve each message. For example, it is a mouse click. MFC checks its various window components and displays the mouse response function of each component when the mouse clicks "falls. For example, it is a keyboard input. MFC checks its keyboard focus and focuses on which component calls the keyboard response function of which component.

Mr. Hou Jie wrote a book "let's get down to MFC" before, and Roger, who was not doing well, looked too big at the time. Now let's look back. Isn't the whole book about message delivery ......

Summary

As a large-scale computer professional, it is a bit embarrassing to see the essence of the operating system. I believe that many talented people in the computer industry know this well in their undergraduate, high school, and even junior high school fields. In short, I hope that curiosity will keep driving me forward. At the same time, I would be delighted if I could become a professional friend and study hard together.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.