Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to schedule and manage threads in rt-thread

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about how to carry out thread scheduling and management of rt-thread. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Thread scheduling and management of rt-thread 1. What is dispatch?

Scheduling is generally a reasonable arrangement, coordination of resources, unified command to complete a thing, while in the operating system, thread scheduling is a task with multiple ready priorities, find the highest priority task and give it to CPU to run.

The rt-thread scheduler determines the current priority of the thread and then executes the ready thread with the highest current priority.

Scheduling can be subdivided into two types. Interruptible scheduling: key to prevent priority inversion; uninterruptible scheduling: first come, first served, uninterruptible.

two。 How to achieve scheduling?

When you create a task, you specify the priority of the task, and in general, each task has its own specific priority. So there are task lists with different priorities in the kernel thread object.

If you specify a maximum of 32 priorities, you can use U32, where each bit represents a priority ready state. The advantages of using bitmaps are high speed and small memory footprint.

Generally speaking, when scheduling to find the highest priority task, you need to make a judgment. How to find the highest priority task. Generally speaking, there are two ways:

Software computing hardware computing

The difference between the two lies only in the problem of computational efficiency, and there is no difference in essential purpose.

And there are two strategies for finding the highest priority:

1. Traverse the ready queue to find the smallest ready queue, the search time is uncertain, and the time complexity is O (n).

two。 Using the method of exchanging space for time, make a bitmap in advance.

For example, if there are a maximum of 8 priorities in the system, the bitmap is as follows:

Const rt_uint8_t _ _ lowest_bit_bitmap [] =

{

/ * 00 * / 0,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0

/ * 10 * / 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0

/ * 20 * / 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0

/ * 30 * / 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0

/ * 40 * / 6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0

/ * 50 * / 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0

/ * 60 * / 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0

/ * 70 * / 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0

/ * 80 * / 7,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0

/ * 90 * / 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0

/ * A0 * / 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0

/ * B0 * / 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0

/ * C0 * / 6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0

/ * D0 * / 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0

/ * E0 * / 5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0

/ * F0 * / 4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0

}

Generally, each bit represents a ready state, so the design of the _ _ rt_ffs program is as follows

Int _ rt_ffs (int value)

{

If (value = = 0) return 0

If (value & 0xff)

Return _ _ lowest_bit_ bitmap [value & 0xff] + 1

If (value & 0xff00)

Return _ lowest_bit_bitmap [(value & 0xff00) > > 8] + 9

If (value & 0xff0000)

Return _ lowest_bit_bitmap [(value & 0xff0000) > > 16] + 17

Return _ lowest_bit_bitmap [(value & 0xff000000) > > 24] + 25

}

If the current thread state of the system is 0b0110 0000, then converting to hexadecimal is 0x60, and the highest priority at this time is 5-1-6, depending on the state in the table. Therefore, the priority of the system can be obtained, and the computational complexity is O (1).

Although rtt supports the same priority, in the design of specific business logic, in the design methods commonly used in RTOS, it is generally required that the running logic of the program is predictable, that is, in the process of program execution, the next step of the program can be predicted. Therefore, it is rare for rtos to design business logic in this way of time-slice training with the same priority. Using the same priority increases the complexity of the system's business logic.

3. When will the system schedule?

RTT is a preemptive system call, so when the system does the scheduling is crucial. System scheduling is divided into active scheduling and passive scheduling.

3.1 Task active block

When thread An is running normally, actively giving up the right to use CPU, such as to execute rt_thread_delay or wait for an event of IPC to arrive, it will release CPU for scheduling, and then go to the system to find the highest priority thread that is ready for scheduling.

The scenarios used in this way are relatively rich. For example, when the current thread does not get the resources, it is necessary to give up the right to use CPU, or when the work is done, take the initiative to give up the right to use CPU. This is the time for the system to schedule.

Thread A has a higher priority than thread B, so after A gives up the right to use CPU, thread B, the highest priority thread that is ready, begins to execute.

3.2 awakened by a higher priority task

This approach is that when a thread with a higher priority than the currently running thread is in the ready state, it will be scheduled to a higher priority thread than the current thread.

According to the understanding that thread An is a running thread, thread C with a higher task priority is ready, so the tick function of the system judges that there are already threads of higher priority than thread An in the ready state, so the rt_schedule () function executes the system scheduling. The status of the current A thread runs on the stack, and the state of the higher priority C thread goes out of the stack and starts running the C thread.

3.3 yield abandons cpu use

First of all, understand what yield is and explain it as giving up and giving up is more reasonable. The sale is only for threads of the same priority.

This applies only to situations where the priority of thread An is equal to that of thread B. Because RTT supports the creation of threads with the same priority, the switching of the same priority is done by time slice polling. Therefore, when the A thread is running normally, if the yield function is executed, it is only equivalent to consuming the A thread's time slice, and the D thread of the same priority starts to run.

Because in RTOS, what is needed is the certainty and reliability of completing the task, and the situation with the same priority is limited, so there are not many applications in this area.

3.4 execution of scheduling in interruption

The above three belong to the active scheduling process, the execution process of the system is predictable, but the interrupt to execute the scheduling is relatively special. It's passive scheduling.

This method performs scheduling in interrupts, and when thread An is running normally, there is an interrupt, because the priority of the interrupt is higher than that of the thread. Therefore, if the scheduling function is executed in the interrupt, then after the interrupt exits, it will switch directly to the higher priority thread in the current system to run. If the highest priority of the current system is still A, the highest priority thread to execute is still An after the interrupt exits. If there is a thread E thread whose priority is higher than An and is in the ready state, at this time, after the interrupt exits, switch to the E thread to execute.

4. What did the dispatcher do?

What does the system do when it is scheduled?

Step 1: find the current and ready thread with the highest priority in the current system, and perform thread switching if there is a thread stack higher than the current running system.

Step 2: turn off the interrupt and press the registers currently running by the system into the stack space

Step 3: find the PC pointer of the thread that needs to run, and find the register state in the pop-up stack at the beginning of the stack

Part IV: open the interrupt, execute the exception ret and let the system resume execution.

At this point, you switch to a higher priority thread that is ready to run.

After reading the above, do you have any further understanding of how to schedule and manage rt-thread threads? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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: 0

*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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report