Priority scheduling method of real-time operating system
Through the complement and calculation methods of the priority bitmap, the highest priority thread is quickly obtained, which solves the problem of high thread switching delay in real-time operating system and realizes efficient thread switching.
Patent Information
- Application Number
- CN202510872733.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-27
- Publication Date
- 2025-07-29
- Estimated Expiration
- 2045-06-27
AI Technical Summary
The existing real-time operating system has the problem of high algorithm delay in the thread scheduling switching stage.
By obtaining the priority bitmap, performing complementary operations and calculations, we obtain the preprocessed unsigned integer number, count the number x of 1, obtain the thread control block pointer corresponding to the highest priority, and complete thread switching.
Without using complex instructions, the efficiency of thread switching is improved, and the latency is reduced, which is suitable for efficient operation of all MCUs.
Smart Images

Figure CN120386609A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of data processing, and in particular to a priority scheduling method for a real-time operating system. Background Art
[0002] Taking the rt-thread real-time operating system as an example, when a system interrupt or event is triggered, to complete the corresponding task within a specified time, the following four key stages usually need to be experienced: the first stage is the interrupt scheduling and switching stage; the second stage is the interrupt service routine (ISR) processing stage; the third stage is the thread scheduling and switching stage; the fourth stage is the thread processing stage (bottom half, abbreviated as BH).
[0003] For the third stage, the thread scheduling and switching stage, this step is implemented by the kernel programming of the RTOS. The corresponding algorithms of existing operating systems have high latency. Summary of the Invention
[0004] In order to overcome the defects existing in the prior art, the present invention provides a priority scheduling method for a real-time operating system to solve the above problems.
[0005] The technical solution adopted by the present invention to solve its technical problems is: a priority scheduling method for a real-time operating system, including the following steps: S1: Obtain a priority bitmap containing bits, where m is a natural number; each binary bit in the priority bitmap corresponds to a bit serial number , and the value range of n is from 0 to , arranged from right to left in ascending order of n; 0 indicates that there is no thread control block pointer corresponding to a ready thread in the ready thread control block pointer linked list with priority n; 1 indicates that there is at least one thread control block pointer corresponding to a ready thread in the ready thread control block pointer linked list with priority n; S2: Take the priority bitmap as an input, perform a complement operation on the priority bitmap, perform an AND operation on the result of the complement operation and the original priority bitmap to obtain a first unsigned integer, and subtract 1 from the first unsigned integer to obtain a preprocessed unsigned integer; S3: For the preprocessed unsigned integer, obtain the number x of 1s in the preprocessed unsigned integer, set n = x to obtain a ready thread control block pointer linked list with priority n, and take out the first thread control block pointer in the thread control block pointer linked list; S4: Use the ready thread pointed to by the retrieved thread control block pointer as the thread corresponding to the highest priority; perform thread switching by switching to the processor according to the thread corresponding to the highest priority.
[0006] Preferably, in step S3, the step of obtaining the number x of 1s in the preprocessed unsigned integer includes: S31: When, set the value of R = 0; S32: After shifting the preprocessed unsigned integer to the right by A bits, perform an AND operation with B, and update the value of R after performing an OR operation on the obtained result with R; S33: After subtracting 1 from the value of a, if a is not 0, execute step S32, and if a is 0, end the loop; After ending the loop, the number x of 1s in the preprocessed unsigned integer = R.
[0007] Optionally, in step S3, ; .
[0008] The beneficial effects of the present invention are as follows: In the priority scheduling method of the real-time operating system, complex instructions such as multiplication and division are not used, so the algorithm efficiency will not be reduced when used in low-end MCUs; secondly, only simple digital circuits using shift and logical operations are used, and all MCUs can use single-cycle efficient instructions that can operate efficiently to obtain the thread corresponding to the highest priority in the target priority bitmap, thereby reducing latency. BRIEF DESCRIPTION OF THE DRAWINGS
[0009] Figure 1 is a flowchart of a priority scheduling method for a real-time operating system in an embodiment of the present invention. DETAILED DESCRIPTION
[0010] The following further describes the specific embodiments of the present invention with reference to the drawings. It should be noted here that the description of these embodiments is for helping to understand the present invention, but does not constitute a limitation to the present invention. In addition, the technical features involved in the various embodiments of the present invention described below can be combined with each other as long as they do not conflict with each other.
[0011] As Figure 1 shown, a priority scheduling method for a real-time operating system includes the following steps: S1: Obtain a priority bitmap containing bits, where m is a natural number; each binary bit in the priority bitmap corresponds to a bit number , and the value range of n is , arranged from right to left in ascending order of n; A value of 0 indicates that there is no thread control block pointer corresponding to a ready thread in the linked list of thread control block pointers with priority n; A value of 1 indicates that there is at least one thread control block pointer corresponding to a ready thread in the linked list of thread control block pointers with priority n; In this way, a priority bitmap containing all bits, which is represented by 0 and 1 and reflects the ready thread situation of each linked list of thread control block pointers, is obtained; For example, a priority bitmap containing 256 bits, the subscript n of the bit number ranges from 0 to 255, corresponding to the linked lists of thread control block pointers with priorities from 0 to 255. The 256 linked lists of thread control block pointers form an array with 256 elements; The nth bit in the priority bitmap corresponds to the linked list of thread control block pointers with subscript n in the array. The linked list of thread control block pointers is linked by one or more thread control block pointers with priority n, and each thread control block pointer points to a ready thread; S2: Taking the priority bitmap as input, perform a complement operation on the priority bitmap (that is, invert each bit of the priority bitmap and then add 1), perform an AND operation on the result of the complement operation and the original priority bitmap to obtain a first unsigned integer after operation, and subtract 1 from the first unsigned integer after operation to obtain a preprocessed unsigned integer; For example, for the 32-bit binary number 00010010 1001 1000 0000 0000 0000 0000, after obtaining the preprocessed unsigned integer, the bit number of the first value of 1 is , so the preprocessed unsigned integer is 0000 0000 0000 0111 1111 1111 1111 1111; S3: For the preprocessed unsigned integer, obtain the number x of 1s in the preprocessed unsigned integer, set n = x to obtain the linked list of thread control block pointers with priority n, and take out the first thread control block pointer in the linked list of thread control block pointers; S4: Take the ready thread pointed to by the taken-out thread control block pointer as the thread corresponding to the highest priority; Perform thread switching according to the thread corresponding to the highest priority to the processor.
[0012] In this solution, performing thread switching according to the thread corresponding to the highest priority to the processor is a prior art and will not be elaborated here one by one.
[0013] Preferably, in the step S3, the step of obtaining the number x of 1s in the preprocessed unsigned integer includes: S31: When, set the value R = 0; S32: After shifting the preprocessed unsigned integer by A bits to the right, perform an AND operation with B, and then perform an OR operation on the result with R to update the value of R; S33: After subtracting 1 from the value of a, if a is not 0, execute step S32; if a is 0, end the loop; After ending the loop, the number of 1s x in the preprocessed unsigned integer is x = R.
[0014] Optionally, in step S3 ; 。
[0015] In one embodiment, for a bitmap containing 32 bits, i.e., m = 5; Obtained according to step S31 , R = 0; Obtained according to step S32, after shifting the preprocessed unsigned integer by 11 bits to the right, perform an AND operation with 16 to update the value of R; Obtained according to step S33 ; Obtained according to step S32, after shifting the preprocessed unsigned integer by A = 4 + R bits to the right, then perform an AND operation with B = 8 to obtain a result, and perform an OR operation on the result with R to update the value of R; Obtained according to step S33 ; Obtained according to step S32, after shifting the preprocessed unsigned integer by A = 1 + R bits to the right, then perform an AND operation with B = 4 to obtain a result, and perform an OR operation on the result with R to update the value of R; Obtained according to step S33 ; Obtained according to step S32, after shifting the preprocessed unsigned integer by A = 0 + R bits to the right, perform an AND operation with B = 2 to obtain a result, and perform an OR operation on the result with R to update the value of R; Obtained according to step S33 ; Obtained according to step S32, after shifting the preprocessed unsigned integer by A = 0 + R to the right, perform an AND operation with B = 1 to obtain a result, and perform an OR operation on the result with R. Since it has been shifted to the last valid bit at this time (this bit is 1 or 0), the AND operation with 1 can be omitted. After shifting by R, the result can be directly obtained; finally, update the value of R; After ending the loop, the number of 1s x in the preprocessed unsigned integer is x = R.
[0016] In another embodiment, take the binary preprocessed unsigned integer 0000 0000 0000 0111 1111 1111 1111 1111 obtained after preprocessing a bitmap containing 32 bits as an example: For , the number obtained by shifting the preprocessed unsigned integer to the right by 11 bits is 0000 0000 0000 0111 1111 1, and the updated value R after performing an AND operation with 16 (binary 10000) is 16 (binary 10000); For , after shifting the preprocessed unsigned integer to the right by 16 bits (i.e., binary 10000), and then shifting it to the right by 4 bits, the resulting number is 0000 0000 0000. Then, performing an AND operation with 8 (binary 1000) gives a result of binary 0000. Then, performing an OR operation on binary 0000 with the value R = 16 (binary 10000) updates R, and at this time, R is 16 in binary 10000; For , after shifting the preprocessed unsigned integer to the right by 16 bits and then shifting it to the right by 1 bit, the resulting number is 0000 0000 0000 011. Then, performing an AND operation with decimal 4 (binary 100) gives a result of binary 000. Then, performing an OR operation on binary 000 with the value R = 16 (binary 10000) updates R, and at this time, R is 16 (binary 10000); For , after shifting the preprocessed unsigned integer to the right by 16 bits and then shifting it to the right by 0 bits, the resulting number is 0000 0000 0000 0111. Performing an AND operation with decimal 2 (binary 10) gives a result of binary 10. Then, performing an OR operation on binary 10 with R = 16 (binary 10000) updates R, and at this time, R is 18 (binary 10010); For , after shifting the preprocessed unsigned integer to the right by 18 and then shifting it to the right by 0 bits, the resulting number is 0000 0000 0000 01. Then, performing an OR operation on binary 1 with R = 18 (binary 10010) updates R, and at this time, R is 19 in binary 10011; End the loop when; After ending the loop, the number of 1s x in the preprocessed unsigned integer is 19 (i.e., binary 10011). Therefore, according to n = 19, using 19 as the subscript to obtain the corresponding thread control block pointer linked list, and taking out the first thread control block pointer of this thread control block pointer linked list.
[0017] In this solution, complex instructions such as multiplication and division are not used, so using it on a low - end MCU will not reduce the algorithm efficiency; secondly, only simple digital circuits using shift and logical operations, and single - cycle high - efficiency instructions that can run efficiently on all MCUs are used to obtain the thread corresponding to the highest priority in the target priority bitmap, thus reducing the latency.
[0018] The embodiments of the present invention have been described in detail above in conjunction with the accompanying drawings, but the present invention is not limited to the described embodiments. For those skilled in the art, without departing from the principle and spirit of the present invention, various changes, modifications, substitutions, and variations to these embodiments still fall within the protection scope of the present invention.
Claims
1. A priority scheduling method for a real-time operating system, characterized in that, Including the following steps: S1: Obtain a priority bitmap containing m bits, where m is a natural number; each binary bit in the priority bitmap corresponds to a bit sequence number , and the value range of n is from 0 to , arranged from right to left in the ascending order of n; 0 indicates that there is no thread control block pointer corresponding to a ready thread in the linked list of thread control block pointers with priority n; 1 indicates that there is at least one thread control block pointer corresponding to a ready thread in the linked list of thread control block pointers with priority n. S2: Taking the priority bitmap as input, performing a complement operation on the priority bitmap, performing an AND operation on the result of the complement operation and the original priority bitmap to obtain a first unsigned integer of the operation, and subtracting 1 from the first unsigned integer of the operation to obtain a preprocessed unsigned integer; S3: For the preprocessed unsigned integer, obtaining the number x of 1s in the preprocessed unsigned integer, setting n = x to obtain a linked list of ready thread control block pointers with priority n, and taking out the first thread control block pointer in the linked list of thread control block pointers; S4: Taking the ready thread pointed to by the taken-out thread control block pointer as the thread corresponding to the highest priority; Switching to the processor according to the thread corresponding to the highest priority to complete the thread switch.
2. The priority scheduling method of a real-time operating system according to claim 1, characterized in that: In the step S3, the step of obtaining the number x of 1s in the preprocessed unsigned integer includes: S31: When, set the value of R = 0; S32: After right-shifting the preprocessed unsigned integer by A bits, perform an AND operation with B, and then perform an OR operation on the result with R to update the value of R; where ; ; S33: After subtracting 1 from the value of a, if a is not 0, execute step S32, and if a is 0, end the loop; After ending the loop, the number x of 1s in the preprocessed unsigned integer = R.
Citation Information
Patent Citations
Instruction decode buffer device of multithreading microprocessor
CN101763251A
Multi-thread scheduling method and device
CN117331655A
Priority based arbitration
US20210349769A1