Task scheduling method and device for embedded system
By introducing a common scheduling cycle and staggered execution time mechanism into the embedded system, tasks are dynamically managed, solving the problems of unstable task execution timing and imprecise cycle control. This achieves efficient and stable task scheduling, improving the system's reliability and performance.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- SYL (NINGBO) BATTERY CO LTD
- Filing Date
- 2025-12-31
- Publication Date
- 2026-05-29
AI Technical Summary
Existing embedded system task scheduling methods lack a unified and precise scheduling management mechanism, resulting in unstable task execution timing, imprecise cycle control, difficulty in ensuring the real-time performance of critical tasks when multiple tasks are running concurrently, and insufficient consideration of the priority relationship and time stagger requirements between tasks, leading to low system scheduling efficiency.
By introducing a common scheduling cycle, tasks are dynamically managed. Based on the task's execution cycle, priority, and staggered execution time, a task execution sequence is generated to ensure that high-priority tasks have priority in execution, avoid resource conflicts, and achieve precise task scheduling by adopting staggered execution time and delay counter mechanism.
It improves the reliability and scheduling efficiency of embedded systems, ensures the stability and real-time performance of task execution, and enhances system performance.
Smart Images

Figure CN122111589A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of embedded system technology, and in particular to a task scheduling method and apparatus for embedded systems. Background Technology
[0002] In embedded systems, the periodic scheduling of tasks is crucial for ensuring the system's real-time performance, stability, and reliability. Typically, systems need to handle a variety of tasks with different execution cycles, priorities, and real-time requirements. This is especially true in Battery Management Systems (BMS), which involve tasks such as data acquisition, fault diagnosis, state calculations (e.g., SOX calculations), and device communication.
[0003] Currently, common approaches to task scheduling in embedded systems include triggering task execution based on simple timers or polling scheduling within the main loop. For example, for periodic tasks, multiple hardware timers or time variables maintained in software are used to trigger task startup. However, these methods often lack a unified and precise scheduling management mechanism. Specifically, due to the limited precision of timers, fluctuations in task execution time, and the mutual influence of multiple concurrent tasks, task execution times are prone to deviation, making it difficult to guarantee strict periodicity and stable execution sequence. Especially when multiple tasks have similar cycles or dependencies, execution times may overlap, leading to resource contention and affecting the real-time performance of critical tasks. Furthermore, traditional methods typically do not fully consider the priority relationships and time staggering requirements between tasks, making it difficult to rationally arrange the task execution order within the scheduling cycle and dynamically adapt to changes in task parameters, resulting in low system scheduling efficiency and poor overall timing controllability.
[0004] In summary, existing task scheduling methods suffer from technical problems such as unstable task execution timing, imprecise period control, and difficulty in ensuring the real-time performance of critical tasks when multiple tasks are running concurrently. Summary of the Invention
[0005] Therefore, it is necessary to provide a task scheduling method and apparatus for a battery management system that can optimize task execution timing and improve system resource utilization, in order to address the above-mentioned technical problems.
[0006] Firstly, this application provides a task scheduling method for an embedded system, including:
[0007] Retrieve task parameters for all tasks from the task list. These task parameters include execution cycle, execution priority, and staggered execution times.
[0008] Based on the staggered execution times, determine the initial execution time of each task;
[0009] Based on the execution cycle of each task and the first execution time, candidate tasks to be executed in each scheduling cycle are determined, wherein the scheduling cycle is shorter than the execution cycle of each task;
[0010] Based on the execution priority of the candidate tasks, the candidate tasks are sorted to generate a sorted task execution sequence for each scheduling cycle;
[0011] The tasks are scheduled and executed sequentially according to the order of the task execution sequence.
[0012] In one embodiment, determining the initial execution time of each task based on the staggered execution times includes:
[0013] Tasks with the same execution cycle are grouped into the same task group;
[0014] Different staggered execution times are assigned to each task within the task group;
[0015] Calculate the estimated total runtime for each task group under the staggered execution time;
[0016] If the estimated total runtime exceeds the preset maximum allowable execution time, the staggered execution time of at least one task in the task group is adjusted until the estimated total runtime does not exceed the maximum allowable execution time.
[0017] The final determined staggered execution time is used as the time offset to calculate the first execution time of each task.
[0018] In one embodiment, determining the candidate tasks to be executed within the current scheduling period based on the execution cycle of each task includes:
[0019] The task list is traversed, and the delay counter associated with each task is updated. The delay counter is used to control the execution time of the task according to the execution cycle.
[0020] For a task whose delay counter reaches the trigger condition, determine whether it is in an enabled state;
[0021] The task that is in the enabled state is designated as the candidate task.
[0022] In one embodiment, the step of sequentially scheduling and executing the tasks according to the order of the task execution sequence further includes:
[0023] After the task execution sequence is completed, record the actual total execution time of all tasks within the current scheduling period;
[0024] Based on the actual total execution time, determine the resource load in the current scheduling cycle and adjust the task scheduling strategy for subsequent scheduling cycles.
[0025] In one embodiment, the method further includes dynamically managing the task list, the dynamic management including:
[0026] Receive a task management request, which may include registering a new task or deleting an already registered task;
[0027] In response to a request to register a new task, the new task and its parameters are added to the task list;
[0028] In response to a request to delete a registered task, a deletion flag is added to the corresponding task, and it is removed from the task list.
[0029] In one embodiment, the task parameters further include a task type, which includes one-time tasks, system tasks, and ordinary tasks. Within each scheduling cycle, the method further includes:
[0030] Determine whether the task is a one-time task; if so, remove it from the task list after execution.
[0031] If the task is not a one-time task, then determine whether a deletion flag exists;
[0032] If the task has a deletion flag and is a regular task, then delete it from the task list.
[0033] In one embodiment, the dynamic management of the one-off task includes:
[0034] In response to a one-time task addition request, the one-time task is inserted into the task list, and a delay counter is configured;
[0035] When the delay counter of the one-time task reaches the trigger condition, it will be used as a candidate task and scheduled for execution.
[0036] Once the one-time task has been completed, remove it from the task list.
[0037] In one embodiment, when performing the dynamic management operation on the task list, synchronous mutual exclusion access is achieved based on semaphores.
[0038] Secondly, this application also provides a task scheduling device for an embedded system, comprising:
[0039] The parameter acquisition module is used to obtain the task parameters of all tasks from the task list. The task parameters include the execution cycle, execution priority, and staggered execution time.
[0040] The first execution time determination module is used to determine the first execution time of each task based on the staggered execution times;
[0041] The task filtering module is used to determine the candidate tasks to be executed in each scheduling cycle based on the execution cycle of each task and the first execution time, wherein the scheduling cycle is shorter than the execution cycle of each task;
[0042] The task sorting module is used to sort the candidate tasks according to their execution priorities and generate a sorted task execution sequence for each scheduling cycle.
[0043] The scheduling and execution module is used to schedule and execute the tasks in the task execution sequence in sequence according to the order of the task execution sequence and based on the staggered execution time of each task.
[0044] Thirdly, this application also provides an embedded system having a task scheduler stored thereon, wherein the processor executes the computer program to implement the steps of the method described in any of the first aspects above.
[0045] The aforementioned task scheduling method and apparatus for embedded systems dynamically manage tasks with different execution cycles through a common scheduling cycle. Within each scheduling cycle, tasks are planned to be executed in priority order, ensuring that high-priority tasks can obtain execution rights first. By setting staggered execution times, resource conflicts caused by multiple tasks being triggered simultaneously are avoided. Ultimately, the problems of unstable task execution timing and imprecise cycle control in traditional methods are solved, thereby improving the reliability, scheduling efficiency, and system performance of the system. Attached Figure Description
[0046] To more clearly illustrate the technical solutions in the embodiments of this application or related technologies, the drawings used in the description of the embodiments of this application or related technologies will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0047] Figure 1 This is a hardware runtime environment diagram of a periodic scheduling method for an embedded system in one embodiment;
[0048] Figure 2 This is a flowchart illustrating a periodic scheduling method for an embedded system in one embodiment.
[0049] Figure 3 Here is a task scheduling time diagram after adjusting the staggered execution times in one embodiment;
[0050] Figure 4 This is a flowchart illustrating a method for determining candidate tasks to be executed in another embodiment;
[0051] Figure 5 This is a task scheduling time diagram with adjusted priorities in one embodiment;
[0052] Figure 6 This is a schematic diagram of a method for dynamically managing a task list in one embodiment;
[0053] Figure 7 This is a structural block diagram of a periodic scheduling device for an embedded system in one embodiment. Detailed Implementation
[0054] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.
[0055] The periodic scheduling method for embedded systems provided in this application can be applied to, for example... Figure 1 The hardware operating environment of the system shown is as follows. This embedded system includes a processor 101, a memory 102, and a communication bus 103. The processor 101 is the core computing unit of the system, used to call the periodic scheduler stored in the memory 102 to perform operations such as task filtering, sorting, and scheduling control. The memory 102 can be volatile or non-volatile memory, storing the operating system, the task scheduler, and a task list containing multiple tasks and their parameters. The communication bus 103 is used to enable communication between the various components.
[0056] In one exemplary embodiment, a fixed task scheduling cycle is triggered by a timer within the processor or the system clock, and a unified scheduler is used to schedule multiple tasks. Figure 2 As shown, a periodic scheduling method for an embedded system is provided, including the following steps:
[0057] Step S201: Obtain the task parameters for all tasks from the task list.
[0058] A task list is a dataset stored in memory for unified management of tasks to be executed across all systems. For example, in a battery management system, tasks may include battery state of X (SOX) calculation tasks, battery data acquisition tasks, and battery control tasks. Tasks used to maintain normal system operation, such as SOX calculation and contactor control, typically have higher priority, while routine tasks used to implement specific functions, such as device communication and fault diagnosis, have relatively lower priority. A scheduling cycle is a fixed time interval triggered by a timer, system clock, or software interrupt; it is the smallest basic time unit for the scheduler to make decisions and proceed.
[0059] Task parameters include execution cycle, execution priority, and staggered execution times. The execution cycle is the interval at which the task needs to be executed repeatedly, for example, a voltage acquisition task executed every 20 milliseconds. Execution priority is a numerical value used to distinguish the order in which tasks are executed; a higher value generally indicates a higher priority, used to ensure the execution of critical tasks.
[0060] The staggered execution time is a preset time offset. Within a scheduling cycle, when multiple tasks theoretically meet the execution conditions, by assigning an appropriate offset to each task, their actual start times are staggered on the timeline, thereby reducing resource contention between tasks.
[0061] It should be noted that, in order to ensure that the scheduler can manage tasks at a specific time and simplify the time calculation logic, the execution cycle of the task and the staggered execution time need to be an integer multiple of the system scheduling cycle.
[0062] In some implementations, to ensure data consistency and thread safety during concurrent access to the task list, all read and write operations on the task list are performed under the protection of a synchronization and mutual exclusion mechanism. In a preferred implementation, this synchronization and mutual exclusion is implemented using semaphores or mutexes.
[0063] This step is performed by the scheduler during initialization or at the start of a scheduling cycle, thus providing a complete input data foundation for subsequent scheduling decisions.
[0064] Step S202: Determine the first execution time of each task based on the staggered execution time.
[0065] It is understandable that in multi-task concurrent scenarios, if each task is triggered at the same time, resource contention will occur. To solve this problem, this solution calculates the initial execution time of each task during task scheduling by staggering the execution time parameters. By assigning appropriate offsets to different tasks, their initial trigger times can be evenly distributed along the timeline.
[0066] Step S203: Based on the execution cycle and first execution time of each task, determine the candidate tasks to be executed in each scheduling cycle.
[0067] For example, the logic for determining whether a task needs to be added to the current candidate task set could be that if the difference between the current time and its previous historical execution time has reached or exceeded its own execution cycle, then the task is identified as a candidate task to be executed within the current cycle. By dynamically comparing historical execution times, rather than relying on multiple independent hardware timers, the system can manage the cycles of all tasks on a unified timeline.
[0068] Step S204: Sort the candidate tasks according to their execution priorities to generate a sorted task execution sequence for each scheduling cycle.
[0069] The sorting rules can be configured according to the application scenario of the embedded system. Optionally, they can be sorted in descending order of priority values, ascending order, or by a custom priority mapping relationship. The purpose is to ensure that high-priority tasks receive execution resources first in the current scheduling cycle. The sorting algorithm can be any algorithm such as bubble sort, selection sort, or insertion sort. For example, in a BMS, the priority of the SOX computing task is higher than that of the ordinary data acquisition task. After sorting, the SOX computing task will be placed at the front of the execution sequence to receive processor resources first.
[0070] Step S205: Schedule and execute the tasks in the task execution sequence in order.
[0071] The sequential execution of each task can begin with the first task in the sequence. The processor calls the corresponding execution function for that task, and after it completes and returns, it calls the next task in the sequence, and so on, until all tasks in the sequence have been executed. By executing tasks sequentially according to priority, processor resource contention caused by multiple tasks running concurrently at the same time is avoided.
[0072] The aforementioned periodic scheduling method for embedded systems dynamically manages multiple tasks with different periods and priorities through a common task scheduler. By introducing a common task scheduling period as a base time unit, tasks are scheduled to execute in priority order within each scheduling period, ensuring that high-priority critical tasks receive priority execution rights, thus guaranteeing the system's real-time performance and security. By combining this with a staggered execution time mechanism, resource conflicts caused by multiple tasks being triggered simultaneously can be avoided. This method solves the problems of unstable task execution timing and imprecise periodic control caused by relying on independent timers or simple polling in traditional methods, improving the reliability, efficiency, and performance of embedded systems.
[0073] In the following embodiments, a battery management system (BMS) is used as an example to illustrate the task scheduling method of the present invention more clearly. It should be understood that the scheduling method of the present invention is not limited to a BMS, but is also applicable to other industrial control systems with multi-task periodic scheduling requirements.
[0074] In a Battery Management System (BMS), tasks are primarily categorized into three types based on their functional attributes: battery state calculation tasks, battery data acquisition tasks, and battery control tasks. Battery state calculation tasks include SOX calculations; battery data acquisition tasks include battery voltage and temperature acquisition; and battery control tasks include contactor engagement / disengagement control and battery equalization control. Each task includes at least one of these three types.
[0075] Based on their impact on system security and core functions, the above tasks can be further divided into system tasks and ordinary business tasks. System tasks are critical tasks used to maintain the normal operation of the system, and their execution priority is usually high, such as SOX calculations and contactor control. Ordinary business tasks are routine tasks that implement specific application functions, and their priority and cycle can generally be configured according to business needs, such as equipment communication and fault diagnosis. All types of tasks are associated and stored in a task list through their unique identifiers and various task parameters, facilitating the processor to uniformly call the scheduler for filtering, sorting, and execution. In an exemplary embodiment, determining the first execution time of each task based on staggered execution times in the above embodiments can be implemented using a method that includes evaluation and adjustment of staggered execution times, which can be executed during system initialization. This mainly includes the following steps:
[0076] Step S301: Assign tasks with the same execution cycle to the same task group.
[0077] First, the processor traverses the task list and clusters all tasks according to their execution cycles. It's important to note that tasks with the same execution cycle are grouped together because these tasks are likely to reach their cycle trigger points synchronously on the future timeline. Tasks in the same group have the highest risk of overlapping execution times and resource contention; therefore, their overall execution offset can be adjusted.
[0078] Step S302: Assign different staggered execution times to each task within the task group.
[0079] Initial staggered execution times are assigned to each task within the task group. For ease of understanding, a preferred embodiment is described below. In this embodiment, the system scheduling period is set to 10ms, which can be used as the maximum allowed execution time.
[0080] As shown in Table 1, the system's task list contains 6 tasks. The fault diagnosis task with an execution period of 10ms and number T1 is assigned to group 1; the voltage acquisition and temperature acquisition tasks with an execution period of 20ms and number T2_0 and T2_1 are assigned to group 2; and the remaining tasks with a period of 30ms are assigned to group 3. The initial setting for the staggered execution time of the tasks is 0.
[0081]
[0082] Step S303: Calculate the estimated total running time for each task group under staggered execution times.
[0083] Specifically, based on the estimated single execution time of each task within the group and the staggered execution times allocated to it, the complete execution cycle of all tasks within the same task group is simulated, and the estimated total runtime required for completion is calculated.
[0084]
[0085] As shown in Table 2, during the simulation execution of tasks in Group 3, after T3_0, T3_1 and T3_2 are executed in sequence, their running time is 5ms + 6ms + 6ms = 17ms.
[0086] Step S304: If the estimated total runtime exceeds the preset maximum allowed execution time, adjust the staggered execution time of at least one task in the task group until the estimated total runtime does not exceed the maximum allowed execution time.
[0087] The maximum allowed execution time can be the system's scheduling cycle. It's understandable that if the staggered execution time allocation method shown in Table 2 is followed, after group 3 completes execution, the tasks within group 3 will not be able to complete within the 10ms scheduling cycle, leading to task execution delays and affecting the scheduling of subsequent cycles. Therefore, the staggered execution time needs to be adjusted.
[0088] For example, the staggered execution time for T3_1 can be set to 10ms, and the staggered execution time for T3_2 can be set to 20ms. This means that T3_0, T3_1, and T3_2 are each allocated to three consecutive scheduling cycles. The modified values are shown in Table 3. Then, the estimated total runtime is recalculated. It can be seen that the modified staggered execution time meets the maximum allowed execution time requirement.
[0089] If the adjustment still fails to meet the requirements, continue adjusting until the conditions are met. It should be noted that the method for staggering execution times can be manually preset or calculated using an algorithm based on task number, priority, or other parameters.
[0090]
[0091] Step S305: Use the final determined staggered execution time as the time offset to calculate the first execution time of each task.
[0092] The finalized staggered execution times are used in subsequent task scheduling and execution. The initial execution time of a task is the sum of the system scheduling start time and the staggered execution time of the task, which is represented on the timeline as follows: Figure 3 As shown.
[0093] In this embodiment, by dynamically coordinating and setting the final staggered execution time for each task, and using it as a time offset to calculate the first execution time, tasks with the same or similar cycles are effectively avoided from being triggered completely synchronously on the time axis. This prevents instantaneous competition and conflict of shared resources such as processors, buses and peripherals caused by multiple tasks being ready at the same time.
[0094] In one embodiment, determining the candidate tasks to be executed within the current scheduling period in step S203 based on the execution cycle of each task can be achieved by maintaining a dynamic delay counter for each task. Specifically, as shown... Figure 4 As shown, the method includes the following steps:
[0095] Step S401: Traverse the task list and update the delay counter associated with each task.
[0096] The delay counter can be a software variable, implemented as either an incrementing or decrementing counter. It controls the task's execution time based on the execution cycle. For example, a decrementing countdown counter will be used below. Its initial value can be set to a multiple of the system's scheduling cycle and the task's execution cycle. For instance, for a task with a scheduling cycle of 10ms and an execution cycle of 20ms, a countdown counter with an initial value of 2 can be set.
[0097] At the beginning of each scheduling cycle, the task list is traversed, and the delay counter of each task is decremented, for example, by reducing its value by 1.
[0098] Step S402: For a task whose delay counter reaches the trigger condition, determine whether it is in an enabled state.
[0099] When the delay counter of a task reaches the trigger threshold of 0, it means that the previous execution cycle of the task has ended and it has reached the time when it should be executed.
[0100] In addition to the delay counter, each task is also associated with an enable flag or status bit. The enable flag is a Boolean value used to indicate whether the task is enabled or disabled. Disabled tasks do not participate in subsequent scheduling processes even if their execution cycle has arrived.
[0101] Step S403: Select the task that is in the enabled state as the candidate task.
[0102] The tasks that have passed the screening process are identified as candidate tasks to be executed within the current scheduling period. These tasks are added to the candidate task set, and the system will sort them according to their respective priorities and schedule them in sequence.
[0103] In this embodiment, by assigning a delay counter to each task and managing it centrally within a unified scheduling cycle, a more precise and efficient time control method is achieved compared to relying on multiple independent hardware timers. By introducing an enable state, a task can be temporarily enabled or paused by changing its enable state without deleting it, thereby enhancing the system's adaptability and maintainability.
[0104] In one specific embodiment, the priority of tasks is sorted as follows:
[0105] The priorities of each task in the task list are shown in Table 4, and the system scheduling cycle is 10ms. It can be understood that, based on staggered execution times, without considering priority, the following tasks will be divided into different scheduling cycles for execution, such as... Figure 3 As shown.
[0106]
[0107] exist Figure 3 In the P1 cycle, the candidates include T1, T2_0, and T3_0. When this cycle is reached, the system reorders the three tasks according to priority, resulting in the following execution sequence: T3_0, T2_0, T1. The same logic applies to subsequent scheduling cycles. For example, the reordered scheduling cycle is as follows: Figure 5 As shown.
[0108] In some embodiments, after the system sequentially schedules and executes tasks according to the task execution sequence, the task scheduling strategy can be further optimized based on the current running data. This includes the following steps:
[0109] After the task execution sequence is completed, record the actual total execution time of all tasks within the current scheduling period.
[0110] The actual total execution time of the task is the time elapsed from the start of the first task to the end of the last task, following the sequence order, after the previous task is completed and the next task begins execution.
[0111] Based on the actual total execution time, determine the resource load in the current scheduling cycle and adjust the task scheduling strategy for subsequent scheduling cycles.
[0112] The processor records the actual total execution time within each scheduling cycle in the system log, which reflects the load within the scheduling cycle. If the scheduling cycle length is much longer than the actual total execution time, it indicates that the number of tasks in the current cycle is too small, leading to insufficient system resource utilization. Therefore, tasks can be reordered appropriately, increasing the number of tasks within that scheduling cycle. If the actual total execution time is close to the scheduling cycle length, it indicates that the load is nearing full capacity. If it exceeds the scheduling cycle length, it indicates that tasks failed to complete within the scheduled period. In either case, task adjustments are necessary to prevent system overload.
[0113] The task adjustment strategy can include optimizing task sorting, readjusting staggered scheduling times, or other dynamic task management methods; this application does not limit this approach. For example, optimizing task sorting could involve switching the sorting algorithm that generates the task execution sequence from a time-complexity-intensive algorithm to a more efficient one in subsequent cycles, thereby reducing the scheduler's own overhead. Adjusting staggered scheduling times could involve moving a task from an overloaded cycle to another less-loaded scheduling cycle, thus achieving load balancing.
[0114] This embodiment adjusts the scheduling strategy of subsequent tasks based on the load situation of the current scheduling cycle, which can enhance the long-term stability of the system under complex working conditions.
[0115] In some embodiments, a method for dynamically managing the task list is also provided. Specifically, such as... Figure 6 As shown, it includes the following steps:
[0116] Step S501: Receive a task management request, which may include registering a new task or deleting an already registered task.
[0117] In embedded systems, besides executing registered periodic tasks from a task list, there are also task management needs during system operation, such as adding or removing tasks. Dynamic task management is triggered by the processor responding to external task management requests. These requests are typically issued by other management modules in the system, user configuration interfaces, or upper-layer application software, and at least include the operation type (such as registering a new task or deleting an existing task) and the operation object.
[0118] In step S502, in response to the request to register a new task, the new task and its task parameters are added to the task list.
[0119] When the request type is "Register a new task," the parameter set of the new task is extracted from the request, including but not limited to: the task's unique identifier, the task's execution function, the execution cycle, the execution priority, staggered execution times, the estimated single run time, and the initial enable state. Then, the task information is inserted into the task list, and the delay counter associated with the task is initialized according to its execution cycle.
[0120] In one secure implementation, the addition operation uses semaphores for synchronization and mutual exclusion protection, ensuring that the scheduler's traversal and filtering operations are not interrupted during task list modifications. Once a new task is successfully registered, it will automatically participate in the candidate task filtering and sorting process of subsequent scheduling cycles.
[0121] Step S503: In response to the request to delete a registered task, add a deletion flag to the corresponding task and remove it from the task list.
[0122] When the request type is to delete a registered task, the corresponding task is first searched in the task list based on the task identifier specified in the request. To ensure the safe removal of a task while it may be executing or about to be scheduled, this embodiment employs a two-stage deletion method.
[0123] In the first phase, a deletion flag is added to the task, logically marking it as pending deletion. This operation also needs to be performed under protection. When the scheduler subsequently traverses the task list to filter candidate tasks, if it detects that the deletion flag of a task is set, it will skip that task, no longer include it in the candidate set, and will not schedule its execution.
[0124] In the second stage, a preferred strategy is to have a background cleanup task periodically scan the task list, delete tasks whose deletion flag is set and which are no longer called by other tasks, and release the memory resources they occupy.
[0125] In this embodiment, the system can dynamically manage the task list without restarting, improving the flexibility of system adjustments. By using a two-stage deletion method of mark-based deletion and physical deletion, the system's security and real-time performance can be guaranteed.
[0126] In one embodiment, the task types in the system include one-time tasks, system tasks, and ordinary tasks.
[0127] One-time tasks are those that need to be executed only once, such as a specific configuration operation during system initialization or a specific action triggered by an external event. System tasks are periodic tasks that maintain the core functions, security, or basic operation of the system, such as SOX calculations, critical fault protection logic, and contactor safety control. These tasks are generally not allowed to be dynamically deleted during runtime to ensure the continuous and safe operation of the system. Ordinary tasks are periodic tasks that implement routine application functions, excluding the above two categories, such as routine data acquisition, non-critical communication, and general diagnostics. The lifecycle of these tasks can be dynamically managed according to the system's operating status.
[0128] Within each scheduling cycle, the system employs different processing logics for different tasks, specifically including the following steps:
[0129] Step S601: Determine whether the task is a one-time task.
[0130] In step S602, if it is a one-time task, it is deleted from the task list after execution.
[0131] Step S603: If the task is not a one-time task, determine whether there is a deletion flag.
[0132] The definition of the deletion identifier can be found in the description of dynamic deletion requests in the previous embodiment. If this identifier exists, it indicates that a request to delete the task has already been issued.
[0133] Step S604: If the task has a deletion flag, determine whether it is a normal task.
[0134] Step S605: If the task has a deletion flag and is a normal task, then delete it from the task list after execution.
[0135] For tasks that need to be deleted, determine whether they are ordinary tasks. If so, they can be deleted normally.
[0136] Step S606: Execute the task.
[0137] If the system does not have a deletion flag, the task will proceed normally. Alternatively, if the task is a system task, as a security mechanism, the processor will ignore its deletion flag and will not perform the deletion operation.
[0138] In this embodiment, by dividing different task types and specifying different execution logic for each task, one-time tasks are automatically deleted after execution, avoiding continuous traversal of invalid tasks, improving scheduling efficiency, and adding deletion protection to system tasks to prevent the loss of critical functions due to misoperation or other reasons, thereby improving the reliability of the system.
[0139] Furthermore, the dynamic management of one-off tasks shall be implemented according to the following steps:
[0140] Step S701: In response to the one-time task addition request, insert the one-time task into the task list and configure the delay counter.
[0141] Optionally, a one-time task may include the same task parameters as a periodic task, but excluding execution cycle parameters.
[0142] Step S702: When the delay counter of a one-time task reaches the trigger condition, it will be used as a candidate task and scheduled for execution.
[0143] After a one-time task is inserted, its delay counter decrements or increments in each subsequent scheduling cycle, just like other periodic tasks. When the value of its delay counter reaches the trigger condition, it is included in the candidate task set for the current scheduling cycle.
[0144] Furthermore, when a one-time task parameter has a priority, the scheduler sorts all candidate tasks (including periodic tasks and the one-time task triggered this time) according to their execution priorities and generates a task execution sequence.
[0145] Step S703: After a one-time task is completed, remove it from the task list.
[0146] In this embodiment, the complexity and maintenance cost of the system are reduced by unifying the management of one-time tasks. The execution timing of tasks can be precisely controlled by using a delay counter. And the phenomenon of tasks occupying memory for a long time can be avoided by using automated deletion logic.
[0147] It should be understood that although the steps in the flowcharts of the embodiments described above are shown sequentially according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowcharts of the embodiments described above may include multiple steps or multiple stages. These steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the steps or stages of other steps.
[0148] Based on the same inventive concept, this application also provides an apparatus for implementing the task scheduling method of the embedded system described above. The solution provided by this apparatus is similar to the implementation scheme described in the above method; therefore, the specific limitations in one or more embodiments of the task scheduling apparatus for embedded systems provided below can be found in the limitations of the method described above, and will not be repeated here.
[0149] In one exemplary embodiment, such as Figure 7 As shown, a task scheduling device 800 for an embedded system is provided, comprising:
[0150] The parameter acquisition module 801 is used to acquire task parameters of all tasks from the task list. The task parameters include execution cycle, execution priority, and staggered execution time.
[0151] The first execution time determination module 802 is used to determine the first execution time of each task based on the staggered execution times;
[0152] The task filtering module 803 is used to determine the candidate tasks to be executed in each scheduling cycle based on the execution cycle and first execution time of each task. The scheduling cycle is shorter than the execution cycle of each task.
[0153] The task sorting module 804 is used to sort the candidate tasks according to their execution priority and generate the sorted task execution sequence for each scheduling cycle.
[0154] The scheduling and execution module 805 is used to schedule and execute tasks in the task execution sequence in sequence according to the order of the task execution sequence and based on the staggered execution time of each task.
[0155] The modules in the task scheduling device of the aforementioned embedded system can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in hardware or independently of the processor in the computer device, or stored in software in the memory of the computer device, so that the processor can call and execute the operations corresponding to each module.
[0156] In one exemplary embodiment, an embedded system is provided having a computer program stored thereon that, when executed by a processor, implements the steps in the above-described method embodiments.
[0157] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium, and when executed, it can include the processes of the embodiments of the above methods. Any references to memory, databases, or other media used in the embodiments provided in this application can include at least one of non-volatile memory and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can take many forms, such as Static Random Access Memory (SRAM) or Dynamic Random Access Memory (DRAM). The databases involved in the embodiments provided in this application may include at least one type of relational database and non-relational database. Non-relational databases may include, but are not limited to, blockchain-based distributed databases. The processors involved in the embodiments provided in this application may be general-purpose processors, central processing units, graphics processing units, digital signal processors, programmable logic devices, quantum computing-based data processing logic devices, artificial intelligence (AI) processors, etc., and are not limited to these.
[0158] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this application.
[0159] The embodiments described above are merely illustrative of several implementation methods of this application, and while the descriptions are specific and detailed, they should not be construed as limiting the scope of this patent application. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this application should be determined by the appended claims.
Claims
1. A task scheduling method for an embedded system, characterized in that, The method includes: Retrieve task parameters for all tasks from the task list. These task parameters include execution cycle, execution priority, and staggered execution times. Based on the staggered execution times, determine the initial execution time of each task; Based on the execution cycle of each task and the first execution time, candidate tasks to be executed in each scheduling cycle are determined, wherein the scheduling cycle is shorter than the execution cycle of each task; Based on the execution priority of the candidate tasks, the candidate tasks are sorted to generate a sorted task execution sequence for each scheduling cycle; The tasks are scheduled and executed sequentially according to the order of the task execution sequence.
2. The method according to claim 1, characterized in that, The step of determining the first execution time of each task based on the staggered execution times includes: Tasks with the same execution cycle are grouped into the same task group; Different staggered execution times are assigned to each task within the task group; Calculate the estimated total runtime for each task group under the staggered execution time; If the estimated total runtime exceeds the preset maximum allowable execution time, the staggered execution time of at least one task in the task group is adjusted until the estimated total runtime does not exceed the maximum allowable execution time. The final determined staggered execution time is used as the time offset to calculate the first execution time of each task.
3. The method according to claim 1, characterized in that, The step of determining the candidate tasks to be executed in the current scheduling period based on the execution cycle of each task includes: The task list is traversed, and the delay counter associated with each task is updated. The delay counter is used to control the execution time of the task according to the execution cycle. For a task whose delay counter reaches the trigger condition, determine whether it is in an enabled state; The task that is in the enabled state is designated as the candidate task.
4. The method according to claim 1, characterized in that, The step of sequentially scheduling and executing tasks according to the order of the task execution sequence further includes: After the task execution sequence is completed, record the actual total execution time of all tasks within the current scheduling period; Based on the actual total execution time, determine the resource load in the current scheduling cycle and adjust the task scheduling strategy for subsequent scheduling cycles.
5. The method according to claim 4, characterized in that, The task parameters also include task type, which includes one-time tasks, system tasks, and ordinary tasks. Within each scheduling cycle, the method further includes: Determine whether the task is a one-time task; if so, remove it from the task list after execution. If the task is not a one-time task, then determine whether a deletion flag exists; If the task has a deletion flag and is a regular task, then delete it from the task list.
6. The method according to claim 5, characterized in that, The dynamic management of the one-time task includes: In response to a one-time task addition request, the one-time task is inserted into the task list, and a delay counter is configured; When the delay counter of the one-time task reaches the trigger condition, it will be used as a candidate task and scheduled for execution. Once the one-time task has been completed, remove it from the task list.
7. The method according to claim 1, characterized in that, The method further includes dynamically managing the task list, wherein the dynamic management includes: Receive a task management request, which may include registering a new task or deleting an already registered task; In response to a request to register a new task, the new task and its parameters are added to the task list; In response to a request to delete a registered task, a deletion flag is added to the corresponding task, and it is removed from the task list.
8. The method according to claim 7, characterized in that, When performing the dynamic management operation on the task list, synchronous mutual exclusion access is achieved based on semaphores.
9. A task scheduling device for an embedded system, characterized in that, The device includes: The parameter acquisition module is used to obtain the task parameters of all tasks from the task list. The task parameters include the execution cycle, execution priority, and staggered execution time. The first execution time determination module is used to determine the first execution time of each task based on the staggered execution times; The task filtering module is used to determine the candidate tasks to be executed in each scheduling cycle based on the execution cycle of each task and the first execution time, wherein the scheduling cycle is shorter than the execution cycle of each task; The task sorting module is used to sort the candidate tasks according to their execution priorities and generate a sorted task execution sequence for each scheduling cycle. The scheduling and execution module is used to schedule and execute the tasks in the task execution sequence in sequence according to the order of the task execution sequence and based on the staggered execution time of each task.
10. An embedded system having a task scheduler stored thereon, characterized in that, When the task scheduler is executed by the processor, it implements the steps of the method according to any one of claims 1 to 8.