Task scheduling method and device, electronic equipment, storage medium and program product

By constructing a task scheduling table and utilizing task timers to preload time intervals, the problem of high CPU load in multitasking operating systems is solved, achieving more efficient task scheduling and lower interrupt frequency.

CN121996367APending Publication Date: 2026-05-08CONTEMPORARY AMPEREX FUTURE ENERGY RES INST (SHANGHAI) LTD +1
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
CONTEMPORARY AMPEREX FUTURE ENERGY RES INST (SHANGHAI) LTD
Filing Date
2024-11-07
Publication Date
2026-05-08

AI Technical Summary

Technical Problem

In existing multitasking operating systems, the short minimum time slice leads to frequent interrupts, increasing CPU load.

Method used

By constructing a task scheduling table, the task scheduling time of each periodic task is determined, and the task timer is preloaded according to the task scheduling table. The task timer is used to trigger interrupts for task scheduling, which reduces the dependence on the minimum time slice.

Benefits of technology

It effectively reduces CPU load, improves the real-time performance and efficiency of task scheduling, and reduces interrupt frequency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121996367A_ABST
    Figure CN121996367A_ABST
Patent Text Reader

Abstract

The invention provides a task scheduling method and device, electronic equipment, a storage medium and a program product, and relates to the technical field of computers. According to the method, a task scheduling table of a plurality of periodic tasks is determined, the task scheduling table comprises task scheduling moments of the periodic tasks, and then a time interval between two task scheduling moments after the current task scheduling moment is determined according to the task scheduling moments in the task scheduling table; the time interval is preloaded to the task timer, and the plurality of periodic tasks are scheduled based on the task timer, so that the periodic tasks can be independently maintained through the task scheduling table, and task scheduling can be performed by triggering interruption according to the task scheduling time in the task scheduling table, namely the task timer can trigger interruption according to the time interval. Interrupt processing does not need to be carried out according to the minimum time slice, and the CPU load can be effectively reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer technology, and more specifically, to a task scheduling method, apparatus, electronic device, storage medium, and program product. Background Technology

[0002] With the in-depth development of computer technology, multitasking operating systems are becoming more and more common, such as Windows, Linux, QNX (Quick UNIX), and FreeRTOS.

[0003] In multi-task scheduling algorithms, a minimum time slice is typically defined. An interrupt is generated every minimum time slice using a system interrupt or hardware timer. The minimum time slice is usually determined based on the greatest common divisor of multiple periodic tasks. Each time an interrupt is handled, the currently executing task needs to be paused to process the relevant interrupt logic. Once the interrupt is complete, execution resumes. Because the minimum time slice is usually short, frequent interruptions can often disrupt the current task execution, thus increasing CPU load. Summary of the Invention

[0004] The purpose of this application is to provide a task scheduling method, apparatus, electronic device, storage medium, and program product to improve the problem of increased CPU load caused by frequent interruptions in existing methods.

[0005] In a first aspect, embodiments of this application provide a task scheduling method, the method comprising:

[0006] A task scheduling table for multiple periodic tasks is determined, wherein the task scheduling table includes the task scheduling time for each periodic task;

[0007] Based on the task scheduling times in the task scheduling table, determine the time interval between two task scheduling times following the current task scheduling time;

[0008] The time interval is preloaded into the task timer, and the multiple periodic tasks are scheduled based on the task timer.

[0009] In the above implementation process, a task scheduling table for multiple periodic tasks is determined. The task scheduling table includes the task scheduling time of each periodic task. Then, based on the task scheduling time in the task scheduling table, the time interval between two task scheduling times after the current task scheduling time is determined. The time interval is preloaded into the task timer. Multiple periodic tasks are scheduled based on the task timer. In this way, periodic tasks can be maintained separately through the task scheduling table. Task scheduling can be performed by triggering interrupts according to the task scheduling time in the task scheduling table. That is, the task timer can trigger interrupts according to the time interval, without the need for interrupt handling according to the minimum time slice, which can effectively reduce the CPU load.

[0010] Optionally, the task scheduling table is a linked list structure, wherein the linked list structure is formed by linking each periodic task in sequence according to its task scheduling time, the last task scheduling time is the least common multiple of the periods of the multiple periodic tasks, and the remaining task scheduling times are arranged in order according to integer multiples of the periods of each periodic task.

[0011] In the above implementation, a linked list structure is used to maintain periodic tasks, which simplifies the scheduling process. Tasks are scheduled according to the time specified in the task schedule table, reducing the time required to find the next task and improving scheduling efficiency.

[0012] Optionally, if multiple tasks have the same scheduling time, then the multiple task scheduling times in the task scheduling table are sorted according to the priority of the corresponding periodic tasks.

[0013] In the above implementation process, the priority of tasks is directly reflected in the task scheduling table, so that scheduling can be carried out directly according to the task scheduling table, which can save the time of priority judgment and improve scheduling efficiency.

[0014] Optionally, determining the time interval between two task scheduling times after the current task scheduling time based on the task scheduling time in the task scheduling table includes:

[0015] When scheduling the periodic task corresponding to the i-th task scheduling time, calculate the time interval between the (i+1)-th task scheduling time and the (i+2)-th task scheduling time, where i is an integer greater than or equal to 2.

[0016] The step of preloading the time interval into a task timer and scheduling the plurality of periodic tasks based on the task timer includes:

[0017] The time interval is preloaded into the register of the task timer;

[0018] When scheduling the periodic task corresponding to the (i+1)th task scheduling time, the time interval is assigned to the register of the task timer;

[0019] When the task timer reaches the value of the register, the periodic task corresponding to the (i+2)th task scheduling time is scheduled.

[0020] In the above implementation, the time interval is preloaded into the task timer register, which allows the task timer to quickly obtain the next task scheduling time and ensures that the task can be scheduled and executed at the precise scheduled time, resulting in higher real-time performance.

[0021] Optionally, scheduling the plurality of periodic tasks based on a task timer includes:

[0022] If at least two periodic tasks need to be scheduled at the same time, the scheduling is performed according to the priority of the at least two periodic tasks. This allows for sequential scheduling of multiple tasks based on priority.

[0023] Optionally, if the task timer has at least two periodic tasks to be scheduled at the same time, scheduling is performed according to the priority of the at least two periodic tasks, including:

[0024] If at least two periodic tasks need to be scheduled at the same time, the at least two periodic tasks are added to the task ready list.

[0025] Periodic tasks in the task ready list are scheduled according to their priority.

[0026] In the above implementation process, adding tasks to the task ready list can ensure that tasks at the same time can be executed in order of priority.

[0027] Optionally, after scheduling the periodic tasks in the task ready table according to their priority, the process includes:

[0028] After a periodic task is completed, it is removed from the task ready list. This releases resources from the task ready list promptly, reducing resource consumption.

[0029] Optionally, the method further includes:

[0030] If there are no periodic tasks in the task ready list, the system enters an idle state. The execution status of each periodic task is monitored by a task state machine. This allows for effective monitoring of task execution, improving task management efficiency. Entering an idle state when there are no tasks to process reduces CPU load.

[0031] Secondly, embodiments of this application provide a task scheduling apparatus, the apparatus comprising:

[0032] The scheduling table determination module is used to determine the task scheduling table for multiple periodic tasks, wherein the task scheduling table includes the task scheduling time for each periodic task.

[0033] The time interval determination module is used to determine the time interval between two task scheduling times after the current task scheduling time based on the task scheduling time in the task scheduling table.

[0034] The task scheduling module is used to preload the time interval into the task timer and schedule the multiple periodic tasks based on the task timer.

[0035] Thirdly, embodiments of this application provide an electronic device, including a processor and a memory, wherein the memory stores computer-readable instructions, and when the computer-readable instructions are executed by the processor, the steps of the method provided in the first aspect above are performed.

[0036] Fourthly, embodiments of this application provide a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, performs the steps of the method provided in the first aspect above.

[0037] Fifthly, embodiments of this application provide a computer program product, including computer program instructions, which, when read and executed by a processor, perform the steps of the method provided in the first aspect above.

[0038] Other features and advantages of this application will be set forth in the following description and will be apparent in part from the description or may be learned by practicing embodiments of this application. The objectives and other advantages of this application may be realized and obtained by means of the structures particularly pointed out in the written description, claims, and drawings. Attached Figure Description

[0039] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the embodiments of this application will be briefly introduced below. It should be understood that the following drawings only show some embodiments of this application and should not be regarded as a limitation of the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.

[0040] Figure 1 A flowchart illustrating a task scheduling method provided in an embodiment of this application;

[0041] Figure 2A comparative schematic diagram showing how to trigger an interrupt according to a fixed time slice and how to trigger an interrupt according to the method of this application, provided for embodiments of this application;

[0042] Figure 3 A schematic diagram comparing load scheduling according to fixed time slices and scheduling according to the method of this application, provided for embodiments of this application;

[0043] Figure 4 A schematic diagram of a task scheduling table provided in an embodiment of this application;

[0044] Figure 5 A schematic diagram of another task scheduling table provided in an embodiment of this application;

[0045] Figure 6 A timing diagram illustrating a scheduling process provided in an embodiment of this application;

[0046] Figure 7 This application provides a schematic diagram of task state transitions for a task state machine.

[0047] Figure 8 A detailed flowchart of a task scheduling method provided in an embodiment of this application;

[0048] Figure 9 A structural block diagram of a task scheduling device provided in an embodiment of this application;

[0049] Figure 10 This is a schematic diagram of the structure of an electronic device for performing a task scheduling method, provided in an embodiment of this application. Detailed Implementation

[0050] The technical solutions in the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings.

[0051] It should be noted that the terms "system" and "network" in the embodiments of this invention can be used interchangeably. "Multiple" refers to two or more; therefore, in the embodiments of this invention, "multiple" can also be understood as "at least two". "And / or" describes the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, and B existing alone. Additionally, the character " / ", unless otherwise specified, generally indicates that the preceding and following related objects have an "or" relationship.

[0052] In multi-task scheduling algorithms, a minimum time slice is typically defined. An interrupt is generated every minimum time slice using a system interrupt or hardware timer. The minimum time slice is usually determined based on the greatest common divisor of multiple periodic tasks. Each time an interrupt is handled, the currently executing task needs to be paused to process the relevant interrupt logic. Once the interrupt is complete, execution resumes. Because the minimum time slice is usually short, frequent interruptions can often disrupt the current task execution, thus increasing CPU load.

[0053] The defects in the above-mentioned prior art solutions are all results obtained by the inventors after practice and careful research. Therefore, the discovery process of the above problems and the solutions proposed by the embodiments of the present invention in the following text should be considered as contributions made by the inventors to the present invention.

[0054] This application provides a task scheduling method. The method involves determining a task scheduling table for multiple periodic tasks, where the table includes the scheduling time of each periodic task. Then, based on the scheduling times in the table, the time interval between two subsequent scheduling times is determined. This time interval is preloaded into a task timer, and multiple periodic tasks are scheduled based on the task timer. This allows for the separate maintenance of periodic tasks using the task scheduling table, and task scheduling can be performed by triggering interrupts according to the scheduling times in the table. In other words, the task timer can trigger interrupts according to the time interval, eliminating the need for interrupt handling based on minimum time slices, thus effectively reducing CPU load.

[0055] Please refer to Figure 1 , Figure 1 A flowchart of a task scheduling method provided in this application embodiment, the method including the following steps:

[0056] Step S110: Determine the task scheduling table for multiple periodic tasks.

[0057] The task scheduling table can be understood as a table used to maintain multiple periodic tasks, including the task scheduling time of each periodic task.

[0058] Here, "confirm" can be understood as obtaining the task scheduling table for multiple periodic tasks. The task scheduling table is built when multiple periodic tasks are created, and can be directly obtained when executing task scheduling. It can also be understood as building the task scheduling table for multiple periodic tasks. That is, after creating multiple periodic tasks, when scheduling tasks, the task scheduling table for multiple periodic tasks is built first, and then task scheduling can be executed.

[0059] The scheduling times of each task can be arranged in chronological order in the task scheduling table. For example, if there are multiple periodic tasks including Task 1 with a period of 80us, Task 2 with a period of 100us, and Task 3 with a period of 200us, then the scheduling times of each task in the task scheduling table will be arranged as follows: 80us, 100us, 160us (the second period of Task 1), 200us (the second period of Task 2), 200us (the first period of Task 3), 240us, etc.

[0060] In some implementations, the specific structure of the task scheduling table can be implemented using a time wheel, priority queue, array, or matrix.

[0061] Step S120: Determine the time interval between two task scheduling times following the current task scheduling time based on the task scheduling time in the task scheduling table.

[0062] The task scheduling table contains multiple task scheduling times. When scheduling tasks, the scheduling can be performed according to the task scheduling times in the task table. That is, an interrupt is executed at the task scheduling time. For example, at 80us, an interrupt is executed to schedule task 1; at 100us, an interrupt is executed to schedule task 2; at 160us, an interrupt is executed again to schedule task 1. In this way, multiple periodic tasks can be scheduled.

[0063] To improve the real-time performance of task scheduling, subsequent time intervals can be preloaded into the task timer. That is, during the scheduling process, the time interval between two task scheduling moments after the current task scheduling moment is determined. The current task scheduling moment refers to the current moment. For example, in the example above, if task 1 is scheduled at 80us, the current task scheduling moment is 80us; if task 2 is scheduled at 100us, the current task scheduling moment is 100us.

[0064] The time interval between two task scheduling moments after the current task scheduling moment can be understood as the next time interval. For example, if the current task scheduling moment is 80us, the next task scheduling moment is 100us, and the next task scheduling moment is 160us, then the time interval between two task scheduling moments after the current task scheduling moment refers to the time interval of 60us between 100us and 160us.

[0065] Understandably, to improve scheduling efficiency, the time interval between two adjacent task scheduling times in the task scheduling table can be calculated in advance. This way, during scheduling, the current task scheduling time can be directly located, and the time interval between the next two task scheduling times can be obtained. Alternatively, the time interval can be calculated in real-time during the scheduling process.

[0066] Step S130: Preload the time interval into the task timer and schedule multiple periodic tasks based on the task timer.

[0067] By preloading the time interval into the task timer, the task timer can quickly obtain the preloaded time interval when an interrupt is triggered, and trigger the interrupt according to the time interval, resulting in higher real-time performance.

[0068] The system can maintain a task timer, which can be used to control when to trigger an interrupt, that is, when to execute task scheduling.

[0069] The registers of the task timer can be used for counting. For example, the value of the register represents the scheduling time of the next task scheduling moment. That is, the register can be used to store the time interval. The timer can use this value to determine when to trigger an interrupt for task scheduling.

[0070] Understandably, after the current task is scheduled to complete, the value of the register will be cleared to zero again, and then the preloaded time interval will be assigned to the register.

[0071] In the actual implementation, the task timer can be started by controlling the register. When the count of the task timer reaches the value of the register, an interrupt is triggered to execute task scheduling.

[0072] In this solution, task scheduling is controlled by a task timer, reducing software-level logic checks and data processing, thereby reducing scheduling latency and improving real-time performance. Furthermore, interrupts are triggered by the task timer, and not every time slice is interrupted, effectively reducing CPU load.

[0073] like Figure 2 As shown, Figure 2 The diagram illustrates a comparison between triggering interrupts according to a fixed time slice and triggering interrupts according to the method of this application. It is evident that triggering interrupts according to a fixed time slice requires triggering an interrupt every 20µs. The CPU needs to query whether there are any tasks to schedule at any given time after the interrupt and perform corresponding interrupt service processing, resulting in a high CPU load. In contrast, according to the scheme of this application, since there are tasks to schedule at every task scheduling moment, the CPU only triggers an interrupt when there are tasks to schedule, and then performs task scheduling. The interrupt frequency is much lower than that triggered by a fixed time slice, resulting in a lower CPU load.

[0074] Each interrupt handler involves a series of logical judgments and data processing, including scheduler status checks and context switching, before task scheduling occurs. Therefore, if interrupts are frequent, the CPU will process these operations frequently, increasing the load.

[0075] like Figure 3As shown, Figure 3 The diagram illustrates a comparison of workload scheduling using a fixed time slice and the method described in this application. It is evident that, under the same conditions for the first 300µs of the first cycle, the sum of the interrupt context saving time, callback function selection and processing time, and interrupt context recovery time under the fixed time slice scheme is approximately 8µs. Under the method described in this application, the sum of these timeframes is also approximately 8µs. During task switching, exception call context saving is approximately 2µs, and exception call context recovery is approximately 2µs for both schemes. Specific time load details are as follows: Figure 3 As shown, according to the fixed time-slice scheme, the total non-idle time is 216µs, and the load is 72%. According to the scheme of this application, the total non-idle time is 134µs, and the load is 44.6%, which is lower than that of the fixed time-slice scheme.

[0076] In the above implementation process, a task scheduling table for multiple periodic tasks is determined. The task scheduling table includes the task scheduling time of each periodic task. Then, based on the task scheduling time in the task scheduling table, the time interval between two task scheduling times after the current task scheduling time is determined. The time interval is preloaded into the task timer. Multiple periodic tasks are scheduled based on the task timer. In this way, periodic tasks can be maintained separately through the task scheduling table. Task scheduling can be performed by triggering interrupts according to the task scheduling time in the task scheduling table. That is, the task timer can trigger interrupts according to the time interval, without the need for interrupt handling according to the minimum time slice, which can effectively reduce the CPU load.

[0077] Based on the above embodiments, in order to improve scheduling efficiency and make it easier to maintain the task scheduling table, the task scheduling table can be a linked list structure. The linked list structure is formed by connecting each periodic task in sequence according to its task scheduling time. The last task scheduling time is the least common multiple of the periods of multiple periodic tasks, and the remaining task scheduling times are arranged in order according to integer multiples of the periods of each periodic task.

[0078] The linked list structure of the task scheduling table can be a circular linked list, such as... Figure 4 As shown, the last task scheduling moment points to the first task scheduling moment, forming a loop. This allows the scheduling of periodic tasks to be performed according to the scheduling moments of each task in the linked list structure. Figure 4 The diagram illustrates three periodic tasks with periods of 80µs, 100µs, and 200µs. The least common multiple of these periods is 400µs. The task scheduling times are sequentially inserted into linked lists of the task scheduling table in integer multiples of their periods until the least common multiple is reached, thus forming a schedule like this. Figure 4The linked list structure shown.

[0079] Each linked list entry in the task scheduling table can contain the task scheduling time, task pointer, task identifier, and the position of the next linked list entry.

[0080] In the above implementation, a linked list structure is used to maintain periodic tasks, which simplifies the scheduling process. Tasks are scheduled according to the time specified in the task schedule table, reducing the time required to find the next task and improving scheduling efficiency.

[0081] Based on the above embodiments, there may be multiple periodic tasks at the same task scheduling time. Therefore, if there are multiple tasks with the same scheduling time in the task scheduling table, the multiple task scheduling times in the task scheduling table are sorted according to the priority of each corresponding periodic task.

[0082] like Figure 4 There are two identical task scheduling times of 200us. At this time, there are two tasks, namely Task 2 and Task 3. In the task scheduling table, the task scheduling times can be directly arranged according to the task priority. If the priority of Task 3 is higher than that of Task 2, then Task 3's 200us time will be arranged first, and then Task 2's 200us time will be arranged next. In this way, when the scheduling is executed, when the time reaches 200us, according to the task scheduling times arranged in the task scheduling table, Task 3 will be scheduled first, and after Task 3 is scheduled, Task 2 will be scheduled.

[0083] For example, if there are three 400us task scheduling times in the task scheduling table, these three task scheduling times are arranged according to task priority. If task 1 has the lowest priority, then task 3's 400us time is placed first, task 2's 400us time is placed in the middle, and task 1's 400us time is placed last. In this way, when the scheduling is executed, when the 400us time arrives, task 3 is scheduled first, then task 2, and finally task 1. Thus, for the same task scheduling time, tasks can be scheduled in order according to their priority.

[0084] In the above implementation process, the priority of tasks is directly reflected in the task scheduling table, so that scheduling can be carried out directly according to the task scheduling table, which can save the time of priority judgment and improve scheduling efficiency.

[0085] In some other implementations, the task scheduling times in the task scheduling table may not be duplicated. For example, if there are two task schedules at 200us, then they are scheduled according to... Figure 4In this way, the task scheduling table will record two task scheduling moments of 200us. At this time, the tasks are arranged according to their priority. Each task scheduling moment will indicate the corresponding task. In this way, you can know which task to schedule first based on the task scheduling table.

[0086] To simplify the task scheduling table, the two task scheduling times of 200us can be recorded only once, such as... Figure 5 As shown, the three task scheduling times at 400us are recorded only once. However, the 200us scheduling time will identify two tasks (task 2 and task 3), and the 400us scheduling time will identify three tasks (task 1, task 2, and task 3). In this case, if at least two tasks are scheduled at the same time, scheduling can be based on task priority. That is, in this scenario, task priorities can be maintained independently. Figure 4 As shown, task priority can be reflected through the task scheduling table.

[0087] Based on the above embodiments, in order to improve the real-time performance of task scheduling, the scheduling time of the next task scheduling moment can be preloaded into the task timer, and the scheduling of the next task can be triggered by the task timer overflow interrupt.

[0088] Specifically, when scheduling the periodic task corresponding to the i-th task scheduling time, the time interval between the (i+1)-th and (i+2)-th task scheduling times is calculated and preloaded into the task timer register, where i is an integer greater than or equal to 2. Then, when scheduling the periodic task corresponding to the (i+1)-th task scheduling time, the time interval is assigned to the task timer register. When the task timer reaches the value of the register, the periodic task corresponding to the (i+2)-th task scheduling time is scheduled.

[0089] Understandably, when scheduling the periodic task corresponding to the first task scheduling time, the time interval between the first and second task scheduling times, as well as the time interval between the second and third task scheduling times, are calculated before scheduling begins. Alternatively, the time intervals between all adjacent task scheduling times can be calculated in advance.

[0090] For example, the time interval between the first and second task scheduling times is t1, the time interval between the second and third task scheduling times is t2, and so on, with the time interval between the i-th and (i+1)-th task scheduling times being ti.

[0091] At the start of scheduling, the register value is t1, and t2 is preloaded into the register. When the timer count reaches t1, an interrupt is triggered, task scheduling is performed, and t2 is assigned to the register. Then, when scheduling the periodic task corresponding to the second task scheduling time, t3 is calculated and preloaded into the register. When scheduling the periodic task corresponding to the third task scheduling time, t3 is assigned into the register, and t4 is calculated and preloaded into the register. In this way, the time interval between subsequent task scheduling times can be calculated in advance and preloaded into the register. This results in a smaller time lag for task context switching when an interrupt is executed, and the real-time performance of task scheduling is higher.

[0092] In some implementations, the aforementioned register may be an AutoReload / Period register.

[0093] In the above implementation, the time interval is preloaded into the task timer register, which allows the task timer to quickly obtain the next task scheduling time and ensures that the task can be scheduled and executed at the precise scheduled time, resulting in higher real-time performance.

[0094] The specific timing of the above scheduling process can be described as follows: Figure 6 As shown, the scheduling scheme of this application can guarantee the stability of the task timer time. In a scheduling process, the task timer does not perform the decrement operation for only one chip clock cycle (i.e., one clock cycle after the interrupt where the AutoReload / Period register is assigned to the Counter register). Taking a system with a main frequency of 250MHz as an example, this error is only 4*10^-9 seconds.

[0095] Based on the above embodiments, when there are at least two periodic tasks that need to be scheduled at the same time, scheduling is performed according to the priority of the at least two periodic tasks.

[0096] Understandably, in accordance with Figure 4 When scheduling tasks according to the task scheduling table shown, due to... Figure 4 The task scheduling table already reflects the priority of at least two periodic tasks at the same task scheduling time. Therefore, if there are at least two periodic tasks to be scheduled at the same time, the tasks can be scheduled directly according to the sorted tasks in the task scheduling table.

[0097] For example, when preloading the values ​​to the task timer registers, the values ​​are loaded according to the time intervals between task scheduling times in the task scheduling table, such as according to... Figure 4In the task scheduling table shown, the time interval between the 4th and 5th task scheduling times is 0. When executing the task at the 3rd task scheduling time, the time interval 40us will be assigned to the register, and the time interval 0 will be preloaded to the register. If the time interval is 0, it means that at least two tasks need to be scheduled after 40us. According to the task scheduling table, the task identified at the 4th task scheduling time is task 3, so task 3 will be scheduled first, and then the register will be assigned 0. After task 3 is scheduled, the task identified at the 5th task scheduling time is task 2, so task 2 will be scheduled next.

[0098] If according to Figure 5 The task scheduling table shown records only one task scheduling time in 200µs. When scheduling the task at the third scheduling time, the 40µs time interval between the third and fourth scheduling times is assigned to a register. Then, the 40µs time interval between the fourth and fifth scheduling times is preloaded into the register. However, at the fourth scheduling time, tasks 2 and 3 are identified. Upon reaching the fourth scheduling time, the priorities of tasks 2 and 3 are first determined, and the task with higher priority is scheduled first, followed by the task with lower priority. This allows for sequential scheduling of multiple tasks according to priority.

[0099] Based on the above embodiments, when there are at least two periodic tasks that need to be scheduled at the same time, at least two periodic tasks are added to the task ready table, and then the periodic tasks in the task ready table are scheduled according to their priority.

[0100] The task ready table is used to store tasks in a ready state. When the system performs task scheduling, it will find the highest priority task from the task ready table and execute that task.

[0101] Understandably, the task ready list includes two main items: priority and task members. Tasks with the same priority will be placed in the same priority list item in the task ready list. This makes it easy to quickly find all tasks under the same priority, and the task ready list can sort the tasks according to their priority.

[0102] When scheduling tasks, the system only needs to focus on the task ready table, selecting and executing the highest-priority task from it. This improves scheduling efficiency. In essence, if a task timer has at least two periodic tasks to schedule simultaneously, adding these two periodic tasks to the task ready table first will automatically add them to their corresponding priority list entries. During scheduling, the highest-priority task can then be directly selected for execution.

[0103] If a task is in the task ready list, it means that the task is in a ready state. In order to improve the search efficiency, the real-time operating system defines a variable (such as OSRdyGrp). Each bit of this variable corresponds to a task group in the task ready list. If the bit corresponding to a task is 1, it means that the task is in a ready state.

[0104] In some implementations, the task ready table can be a bitmap, in which each task in the system occupies a binary bit. The state of this bit indicates whether the task is ready. This representation method can quickly determine the ready state of a task and perform corresponding scheduling operations.

[0105] In a multitasking operating system, the task ready list helps the system quickly switch between tasks. When a task is completed or blocked, the system can quickly find the next highest priority task from the task ready list to execute.

[0106] Understandably, at each task scheduling moment, when an interrupt is triggered, the corresponding task to be scheduled can first be added to the task ready list, and then the highest priority task can be selected from the task ready list for execution. If there is only one task at the time of task scheduling, then there is only this one task in the task ready list, and at this time the task with the highest priority is also the task that can be scheduled.

[0107] In the above implementation process, adding tasks to the task ready list can ensure that tasks at the same time can be executed in order of priority.

[0108] Based on the above embodiments, after a periodic task is completed, the periodic task is deleted from the task ready list.

[0109] Understandably, removing a task from the task ready list promptly after it is completed can release the resources in the task ready list in a timely manner and reduce resource consumption.

[0110] Based on the above embodiments, if there are no periodic tasks in the task ready table, the task enters an idle state, wherein the execution status of each periodic task is monitored by a task state machine.

[0111] The system can maintain a task state machine to monitor the execution status of each periodic task. The task scheduling method in this scheme can be executed by a task scheduler, which can perform scheduling operations by referring to the state of the task state machine. Figure 7As shown, the current task pointer (CurrentTask) points to the highest priority task in the task ready list and performs a context switch (state 4 to state 1). After a periodic task completes, it is removed from the task ready list, and then the periodic task continues to execute. The current task pointer points to the task scheduler (state 1 to state 2), triggering an interrupt and adding the task to the task ready list (state 2 to state 4). If other interrupts trigger tasks, they will also be added to the task ready list (state 3 to state 4). When there are no ready tasks, the task will execute an idle task and perform dynamic task management operations (state 1 to state 5).

[0112] Understandably, since other non-periodic tasks will also be added to the task ready list, when scheduling based on the task scheduler, the task corresponding to the current scheduling time can be added to the task ready list each time, even if there is only one task. This is because if there are other non-periodic tasks to be executed at the same time, they can be scheduled according to the task priority in the task ready list.

[0113] In the above implementation process, the task state machine can effectively monitor the execution process of the task, improve the efficiency of task management, and enter an idle state when there are no tasks to process, which can reduce the CPU load.

[0114] In some implementations, if a new periodic task is added during the scheduling of the aforementioned multiple periodic tasks, a new task scheduling table can be constructed for the new periodic task, and the new periodic task can be scheduled according to the new task scheduling table. Furthermore, since tasks are added to the task ready table during scheduling, even with multiple task scheduling tables, scheduling can be performed based on the task priority in the task ready table. For example, if multiple tasks exist at the same scheduling time, but these tasks have different task scheduling tables, but all are added to the task ready table, the task with the highest priority is selected for scheduling first. This allows for the sequential scheduling of multiple tasks at the same time.

[0115] Understandably, if there is only one periodic task initially, or if a new periodic task is added, a task scheduling table can be built for that single periodic task to implement scheduling. However, to simplify the scheduling process, it is not necessary to build a task scheduling table; scheduling can be performed directly based on periodic interrupts. For newly added periodic tasks, the scheduling of these periodic tasks can be handled using non-periodic task management methods, relative to the existing task scheduling table.

[0116] The specific scheduling process of this solution can be described as follows: Figure 8 As shown, it includes the following:

[0117] Step S1: Initially, create tasks, such as creating a Task Control Block (TCB), which includes information such as the task's execution function, cycle, task priority, function, and stack. The stack depth is allocated by the memory management system for each task's stack.

[0118] Step S2: Construct a task scheduling table for multiple periodic tasks, with the task pointer CurrentPeriodicTask pointing to the first task scheduling time in the task scheduling table.

[0119] Step S3: Calculate the time interval t1 between the first and second task scheduling times, and calculate the time interval t2 between the second and third task scheduling times.

[0120] Step S4: Assign t1 to the AutoReload register of the task timer.

[0121] Step S5: Start scheduling, start the task timer, the run pointer points to the first task scheduling time in the task scheduling table, and preload t2 into the register.

[0122] Step S6: Execute the task function of OurrentTask.

[0123] Step S7: The task timer triggers an interrupt.

[0124] Step S8: The hardware automatically assigns t2 to the register.

[0125] Step S9: Calculate the time interval t3 between the next task scheduling time and the next-next task scheduling time.

[0126] Step S10: Preload t3 into the register.

[0127] Step S11: Task scheduling. CurrentPeriodicTask adds all tasks scheduled at the same time to the task ready list. CurrentPeriodicTask points to the task scheduled at the next time in the task scheduling table. CurrentTask selects the highest priority task in the task ready list to perform context switching.

[0128] After step S6, if any non-periodic task is interrupted, non-periodic task management is performed, and the process is as follows:

[0129] Step S12: Interrupt / Event Trigger.

[0130] Step S13: If so, release the blocking status of the corresponding task.

[0131] Step S14: Compare the priorities of the original periodic tasks.

[0132] Step S15: If the priority is higher than the original periodic task, the original periodic task is saved back to the ready state, CurrentTask points to the current non-periodic task, and a context switch is performed.

[0133] Step S16: If the priority is lower than that of the original periodic task, add it to the task ready list and return to step S6 for execution.

[0134] Step S17: If the CurrentTask task has finished executing.

[0135] Step S18: CurrentTask is put back into the blocking list, a new task execution context is selected based on the task ready list, and then the process returns to step S6.

[0136] In conjunction with the above embodiments, please refer to Figure 9 , Figure 9 This is a structural block diagram of a task scheduling device 200 provided in an embodiment of this application. The device 200 may be a module, program segment, or code on an electronic device. It should be understood that this device 200 is similar to the one described above. Figure 1 The method implementation corresponds to this and can be executed. Figure 1 The various steps involved in the method embodiment and the specific functions of the device 200 can be found in the description above. To avoid repetition, detailed descriptions are omitted here.

[0137] Optionally, the device 200 includes:

[0138] The scheduling table determination module 210 is used to determine the task scheduling table for multiple periodic tasks, wherein the task scheduling table includes the task scheduling time of each periodic task.

[0139] The time interval determination module 220 is used to determine the time interval between two task scheduling times after the current task scheduling time based on the task scheduling time in the task scheduling table.

[0140] The task scheduling module 230 is used to preload the time interval into the task timer and schedule the multiple periodic tasks based on the task timer.

[0141] Optionally, the task scheduling table is a linked list structure, wherein the linked list structure is formed by linking each periodic task in sequence according to its task scheduling time, the last task scheduling time is the least common multiple of the periods of the multiple periodic tasks, and the remaining task scheduling times are arranged in order according to integer multiples of the periods of each periodic task.

[0142] Optionally, if multiple tasks have the same scheduling time, then the multiple task scheduling times in the task scheduling table are sorted according to the priority of the corresponding periodic tasks.

[0143] Optionally, the time interval determination module 220 is used to calculate the time interval between the (i+1)th and (i+2)th task scheduling times when scheduling the periodic task corresponding to the i-th task scheduling time, where i is an integer greater than or equal to 2; the task scheduling module 220 is used to preload the time interval into the register of the task timer; when scheduling the periodic task corresponding to the (i+1)th task scheduling time, assign the time interval into the register of the task timer; and when the task timer reaches the value of the register, schedule the periodic task corresponding to the (i+2)th task scheduling time.

[0144] Optionally, the task scheduling module 220 is used to schedule tasks according to their priorities when the task timer has at least two periodic tasks to be scheduled at the same time.

[0145] Optionally, the task scheduling module 220 is used to add the at least two periodic tasks to the task ready table when the task timer has at least two periodic tasks to be scheduled at the same time; and to schedule the periodic tasks in the task ready table according to their priority.

[0146] Optionally, the task scheduling module 220 is used to delete the periodic task from the task ready table after the periodic task has been completed.

[0147] Optionally, the task scheduling module 220 is further configured to enter a task idle state when there are no periodic tasks in the task ready table, wherein the execution status of each periodic task is monitored by a task state machine.

[0148] It should be noted that those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working process of the device described above can be referred to the corresponding process in the foregoing method embodiments, and will not be repeated here.

[0149] Please refer to Figure 10 , Figure 10This is a schematic diagram of an electronic device for executing a task scheduling method, provided in an embodiment of this application. The electronic device may include: at least one processor 310, such as a CPU; at least one communication interface 320; at least one memory 330; and at least one communication bus 340. The communication bus 340 is used to establish communication between these components. In this embodiment, the communication interface 320 is used for signaling or data communication with other node devices. The memory 330 may be high-speed RAM or non-volatile memory, such as at least one disk storage device. Optionally, the memory 330 may also be at least one storage device located remotely from the aforementioned processor. The memory 330 stores computer-readable instructions. When these computer-readable instructions are executed by the processor 310, the electronic device performs the aforementioned... Figure 1 The method and process are shown.

[0150] Understandable. Figure 10 The structure shown is for illustrative purposes only; the electronic device may also include components that are more advanced than those shown. Figure 10 The more or fewer components shown, or having the same Figure 10 The different configurations shown. Figure 10 The components shown can be implemented using hardware, software, or a combination thereof.

[0151] This application provides a computer-readable storage medium storing a computer program thereon, which, when executed by a processor, performs the following... Figure 1 The method process executed by the electronic device in the illustrated method embodiment.

[0152] This embodiment discloses a computer program product, which includes a computer program stored on a non-transitory computer-readable storage medium. The computer program includes program instructions, and when the program instructions are executed by a computer, the computer can perform the methods provided in the above-described method embodiments, such as including:

[0153] A task scheduling table for multiple periodic tasks is determined, wherein the task scheduling table includes the task scheduling time for each periodic task;

[0154] Based on the task scheduling times in the task scheduling table, determine the time interval between two task scheduling times following the current task scheduling time;

[0155] The time interval is preloaded into the task timer, and the multiple periodic tasks are scheduled based on the task timer.

[0156] In summary, the embodiments of this application provide a task scheduling method, apparatus, electronic device, storage medium, and program product. By determining a task scheduling table for multiple periodic tasks, the task scheduling table includes the task scheduling time of each periodic task. Then, based on the task scheduling time in the task scheduling table, the time interval between two task scheduling times after the current task scheduling time is determined, and the time interval is preloaded into the task timer. Multiple periodic tasks are scheduled based on the task timer. In this way, periodic tasks can be maintained separately through the task scheduling table, and task scheduling can be performed by triggering interrupts according to the task scheduling time in the task scheduling table. That is, the task timer can trigger interrupts according to the time interval, without the need for interrupt processing according to the minimum time slice, which can effectively reduce CPU load.

[0157] In the embodiments provided in this application, it should be understood that the disclosed apparatus and methods can be implemented in other ways. The apparatus embodiments described above are merely illustrative. For example, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. Furthermore, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Additionally, the displayed or discussed mutual couplings, direct couplings, or communication connections may be through some communication interfaces; indirect couplings or communication connections between devices or units may be electrical, mechanical, or other forms.

[0158] Furthermore, the units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.

[0159] Furthermore, the functional modules in the various embodiments of this application can be integrated together to form an independent part, or each module can exist independently, or two or more modules can be integrated to form an independent part.

[0160] In this document, relational terms such as first and second are used only to distinguish one entity or operation from another entity or operation, without necessarily requiring or implying any such actual relationship or order between these entities or operations.

[0161] The above description is merely an embodiment of this application and is not intended to limit the scope of protection of this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the scope of protection of this application.

Claims

1. A task scheduling method, characterized in that, The method includes: A task scheduling table for multiple periodic tasks is determined, wherein the task scheduling table includes the task scheduling time for each periodic task; Based on the task scheduling times in the task scheduling table, determine the time interval between two task scheduling times following the current task scheduling time; The time interval is preloaded into the task timer, and the multiple periodic tasks are scheduled based on the task timer.

2. The method according to claim 1, characterized in that, The task scheduling table is a linked list structure, in which each periodic task is linked sequentially according to its task scheduling time. The last task scheduling time is the least common multiple of the periods of the multiple periodic tasks, and the remaining task scheduling times are arranged in order according to integer multiples of the periods of each periodic task.

3. The method according to claim 2, characterized in that, If multiple tasks have the same scheduling time, then in the task scheduling table, these multiple task scheduling times are sorted according to the priority of the corresponding periodic tasks.

4. The method according to claim 1, characterized in that, Determining the time interval between two task scheduling times following the current task scheduling time based on the task scheduling time in the task scheduling table includes: When scheduling the periodic task corresponding to the i-th task scheduling time, calculate the time interval between the (i+1)-th task scheduling time and the (i+2)-th task scheduling time, where i is an integer greater than or equal to 2. The step of preloading the time interval into a task timer and scheduling the plurality of periodic tasks based on the task timer includes: The time interval is preloaded into the register of the task timer; When scheduling the periodic task corresponding to the (i+1)th task scheduling time, the time interval is assigned to the register of the task timer; When the task timer reaches the value of the register, the periodic task corresponding to the (i+2)th task scheduling time is scheduled.

5. The method according to claim 1, characterized in that, The scheduling of the plurality of periodic tasks based on the task timer includes: If at least two periodic tasks need to be scheduled at the same time, the scheduling shall be carried out according to the priority of the at least two periodic tasks.

6. The method according to claim 5, characterized in that, When the task timer has at least two periodic tasks to schedule at the same time, scheduling is performed according to the priority of the at least two periodic tasks, including: If at least two periodic tasks need to be scheduled at the same time, the at least two periodic tasks are added to the task ready list. Periodic tasks in the task ready list are scheduled according to their priority.

7. The method according to claim 6, characterized in that, After scheduling the periodic tasks in the task ready table according to their priority, the process includes: After a periodic task is completed, it is deleted from the task ready list.

8. The method according to claim 6, characterized in that, The method further includes: If there are no periodic tasks in the task ready list, the task enters an idle state, where the execution status of each periodic task is monitored by a task state machine.

9. A task scheduling device, characterized in that, The device includes: The scheduling table determination module is used to determine the task scheduling table for multiple periodic tasks, wherein the task scheduling table includes the task scheduling time for each periodic task. The time interval determination module is used to determine the time interval between two task scheduling times after the current task scheduling time based on the task scheduling time in the task scheduling table. The task scheduling module is used to preload the time interval into the task timer and schedule the multiple periodic tasks based on the task timer.

10. An electronic device, characterized in that, It includes a processor and a memory, the memory storing computer-readable instructions that, when executed by the processor, perform the method as described in any one of claims 1-8.

11. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it performs the method as described in any one of claims 1-8.

12. A computer program product, characterized in that, It includes computer program instructions, which, when read and executed by a processor, perform the method as described in any one of claims 1-8.