Why use an operating system

Why use an operating system

I started using simple logic systems from 51 single chip computers, and slowly started to contact small operating systems such as ucos and FreeRTOS. Now I have begun to contact large open source operating systems such as Linux. Have you ever thought about why you should use an operating system? In the past, the simple logical relationship was not very good. Why is it getting more and more complicated?

Introduce examples

It ’s good to see an example on the blog . Step by step guide us to think about why the medicine uses the operating system.

The general simple embedded device programming ideas are as follows:

main
{
    {处理事务1}{处理事务2}{处理事务3}.......
    {处理事务N}}

isr_server
{
    {处理中断}}

This is the most general idea. Of course, it is enough for simple systems, but such systems have poor real-time performance, such as "transaction 1" if it is a user input detection, when the user enters, if the program is processing Those transactions under transaction 1, then the user input will be invalid this time, and the user's experience is "this button is not sensitive, this machine is very slow";

If we put the transaction in an interrupt to deal with it, although it improves the real-time performance, it will cause another problem, which may lead to the loss of the interrupt. This consequence is sometimes more serious and worse than "slower"! For another example, transaction 2 is a task that only needs to be processed once in 1s, so obviously transaction 2 will waste CPU time in vain.

At this time, we may need to improve our programming ideas, generally we will try to adopt the "time slice" approach. At this time, programming will
become the following way:

main
{
      {事务1的时间片到了则处理事务1}{事务2的时间片到了则处理事务2}.......
      {事务N的时间片到了则处理事务N}}
time_isr_server
{
       {判断每个事务的时间片是否到来,并进行标记}}
isr_server
{
      {处理中断}}

We can see that this improved idea makes the execution time of the transaction controlled. The transaction will only be executed after its own time slice arrives, but we found that this method still cannot completely solve the "real-time" Problem, because a certain transaction time slice can not be executed immediately, she must wait until the current transaction time slice is used up, and the subsequent transaction time slice does not come, she will have the opportunity to obtain "execution time".

At this time, we need to continue to improve our thinking. In order to allow a transaction to be executed immediately after the time slice arrives, we need to change the return position of the program after the time slice is judged in the clock interruption, so that the program does not return to the just interrupted. Position, and the execution starts from the transaction office that has obtained the latest time slice, which completely solves the real-time problem of the transaction.

We improve on this idea, we need to save the current state of the CPU and some data used by the current transaction before entering the clock interrupt, and then we enter the clock interrupt for time slice processing, if we find that there is a new more urgent The time slice of the transaction has arrived, then we change the return address of the interruption, and restore the scene of this more urgent transaction in the CPU, and then return to the interruption to start the execution of this more urgent transaction.

The above paragraph is a bit difficult to read. In fact, this is because the process is a bit complicated and cumbersome. At this time, we need to find an operating system (OS) to help us do these things. If you can do it yourself Using code to achieve this process, in fact, you are writing the operating system yourself. In fact, you can also see from here that the principle of the operating system is actually not so mysterious, but it is difficult to do some details.

At this point, we finally know why we need an operating system. In fact, the use of the operating system is far more than helping you complete this "transaction time slice processing", she can also help you handle various timeouts, perform memory management, complete communication between tasks, etc. With her, the level of the program is also It is clearer, and it is more convenient to add functions to the system, which is more and more obvious in large projects!

1. Multi-tasking to improve real-time

The single-chip system is a main function, which can be seen as only one task running in the while function; while multi-tasking can be seen as multiple tasks running at the same time , each task has its own while loop, feeling each task All have a CPU and do their own things in their respective CPUs.

The simultaneous operation mentioned here is not actually executed in parallel, but executed concurrently. The operating system has a reference switching clock, for example, 10ms. When there are multiple tasks, the running between each task is switched at a frequency of 10ms, which makes people feel like the illusion that all tasks are executed together.

The biggest advantage of the operating system lies in the concurrent execution of tasks. Of course, according to different operating systems, the way of scheduling tasks with interrupts / priorities is different.

2. Sub-module development to reduce coupling

The division of modules by the operating system is very clear, especially for systems like Linux, the division of labor in the development process is more clear.

For example, all kinds of drivers in the underlying driver are placed in their own folders, i2c, spi, mmc, mtd, usb, etc .; the application layer is generally divided by processes, each process is independent of each other, and each completes its own function There are many ways to communicate between processes, to isolate the program development of each function and reduce the coupling between programs.

3. Strong portability, online installation and uninstallation

The analysis of why the operating system is used is here, and it will continue to be updated when you feel it.

Note: The above content is some of my experience accumulated in the learning process, it is inevitable that there will be some knowledge referring to other articles. If there is any infringement, please notify me in time, I will delete or mark the source of the content in time. Please point out, to explore and learn. The article only serves as a guide. For detailed data analysis, please refer to the operating system related tutorials. Thank you for your review.

Published 106 original articles · praised 76 · 130,000 visits +

Guess you like

Origin blog.csdn.net/Creator_Ly/article/details/85877150