Detailed explanation of Ucos system clock beat

When we used this operating system to perform multitasking switching, we talked about how such a concept operating system can perform multitasking switching?

  First, it divides this time into 1 slice and 1 slice. For example, this time is divided into 1 slice in 10 milliseconds . In the first 10 milliseconds, a task is executed, and in the second 10 milliseconds Execute another task, and execute another task in the third 10 milliseconds. In this case, we have performed a multitasking switch. In the process of switching between multitasking, one must use the clock beat. The actual clock beat The above is a timer. This timer regularly switches our various tasks. For example, if this task executes for 10 milliseconds, how does the operating system know whether the execution time of 10 milliseconds has arrived, or when this task voluntarily gives up the CPU ? , how does the operating system know that you voluntarily gave up the CPU , so the clock beat is used here.

  The clock beat is actually a hardware timer, that is, the hardware provides us with a timer, which sets a certain time, for example, we time 1 millisecond, that is to say, I will check the current task every 1 millisecond Status, for example, in a certain clock beat, I find that the timing of this task is up, its execution time is up, then I will let it execute the next task, deprive the execution right of this task, and then execute The next task, or in other words, this task voluntarily gives up the CPU in a certain clock beat , then at this time, I will also execute the next task, which is the role of our clock beat. An operating system must have a clock tick, why?

  Because this time, and the judgment of task status are related to this clock beat. Then the next question arises, the clock beat we use, and we know that the clock beat is provided by the hardware timer, so how long is the timing appropriate? We time 1 millisecond, or 10 milliseconds, or 100 milliseconds. Generally speaking, let's take a look at the difference between timing 1 millisecond and 100 milliseconds.

  First, let's take a look at the timing of 1 millisecond, and the timing of 1 millisecond. That is to say, every 1 millisecond, our CPU will check the status of each task. When the CPU checks the status of this task, we can see, The code it executes when checking the status of this task is useless for our task, because when you check the status of the task, such code has no effect on our task at all, so we say, we It can be seen that when you interrupt once in 1 millisecond, that is to check 1000 times per second, and my 100 milliseconds only check 10 times per second, that is to say, you check 1000 times, and check 10 times, obviously The useless work done by 1000 checks is obviously much larger than that of 10 checks , so we can see from this that the shorter the check time, the more useless work the CPU does.

  However, let's look at another problem. Suppose I still perform a clock beat at 1 millisecond, and the timer is timed for 1 millisecond. Suppose that at this moment, the task gives up the right to execute, that is to say, the task says, I don't need to execute it, The maximum time interval from when it gave up to when our CPU found this was 1 millisecond, why?

  Because the timing of our timer is 1 millisecond, what about the timing of 100 milliseconds? Let's take a look. The maximum time from when this task gives up CPU usage to when the operating system finds it is 100 milliseconds. Obviously, this 1 millisecond takes 100 milliseconds. It is better than the real-time performance of 100 milliseconds, so we can see that if the timer timing is shorter, its real-time performance is better, but the CPU does more useless work, and similarly, the longer the timing time, although the CPU does useless work The less, however, the less real-time it is. So for a system, how much is our clock beat set to be appropriate?

Generally speaking, it is better to set the clock beat between 10-100 milliseconds. Of course, with the increase of our CPU clock, for example, we say that the clock frequency of our CPU is relatively high, and it runs to more than 100 M , such as 128M , 400M and other frequencies, then at this time, we can set it to 1000 Hz, which is a time of 1 millisecond. Generally speaking, setting it to 1 millisecond is better for real-time performance. If you set it to microseconds At this time, the CPU does a lot of useless work, and at this time, our real-time performance will not be improved much. So, in general, we set the clock tick between 10-100 milliseconds. Of course, if the frequency of the CPU is relatively high, you can set it to 1000 Hz.

 

Original link: http://www.maiziedu.com/wiki/iot/ucosclock/

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326482600&siteId=291194637