Task scheduling method, system, device and storage medium based on fixed time slice

By using timers and time slice update mechanisms in task scheduling, the problem of inconsistency between the actual running time and the theoretical time slice in traditional task scheduling is solved, and the precise control of task scheduling and the improvement of system performance is achieved.

CN114153571BActive Publication Date: 2025-04-25EEASY TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202111250142.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-10-26
Publication Date
2025-04-25
Estimated Expiration
2041-10-26

AI Technical Summary

Technical Problem

In the traditional task scheduling method based on fixed time slices, the actual running time of the task is inconsistent with the theoretical time slices, resulting in poor system performance.

Method used

By scheduling tasks in the running ready task queue, turning on the timer corresponding to the task, and setting the timeout time of the timer based on the remaining time slice of the task. When the task actively switches, the remaining time slice is updated; when the timer timed out, the remaining time slice is reset to ensure precise control of the task execution time.

Benefits of technology

This avoids the phenomenon of miscalculation of execution time caused by active task switching, realizes precise control of task scheduling, and improves the overall performance of the system.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114153571B_ABST
    Figure CN114153571B_ABST
Patent Text Reader

Abstract

The present invention is applicable to the field of computer technology, and provides a task scheduling method, system, electronic device and storage medium based on fixed time slices. The method comprises: scheduling tasks in a ready-to-run task queue, starting a timer corresponding to the task, and setting a timeout time of the timer based on a remaining time slice of the task; when the task is actively switched, updating the remaining time slice based on the running time slice of the task, and suspending the task; when the timer times out, resetting the remaining time slice based on the initial time slice of the task, and suspending the task, thereby avoiding the phenomenon of execution time being missed due to active task switching, realizing precise control of task scheduling, and improving the overall performance of the system.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of computer technology, and in particular relates to a task scheduling method, system, electronic equipment and storage medium based on fixed time slices. Background Art

[0002] Embedded operating systems are widely used in various application fields, such as consumer electronics, video security, industrial control, etc. They are mainly responsible for managing various tasks in the system. Task scheduling efficiency is an important indicator to measure the performance of embedded operating systems. It has an important impact on the response speed of application scenarios and the overall product experience. Generally speaking, task scheduling needs to be completed with the help of the operating system's clock tick. The clock tick works like the human heart. It increases the count every fixed time (such as 10ms), which can provide time information for the operating system and provide a basis for the time selection of task scheduling.

[0003] In traditional operating systems based on fixed time slices, a fixed time slice is allocated when a task is created, in ticks. For example, if a tick is 10ms and the time slice is 60ms, the time slice value is 6. The system has a tick interrupt every 10ms, at which time some system-wide processing is performed, such as increasing the total tick count; the remaining time slice of the current task is also reduced by 1. When it is reduced to 0, it means that the time slice has been used up, the current task is suspended, added to the ready task queue, and the next task is scheduled to run from the ready task queue.

[0004] The traditional task scheduling process based on fixed time slices is as follows Figure 1A As shown in the figure, when creating a task, the initial time slice is set to N, in ticks, and the remaining time slice n is the same as the initial time slice. Each time a tick interrupt occurs, n is reduced by 1. When n is reduced to 0, it means that the task time slice has been used up, and the current task is suspended, waiting to be scheduled to run next time. If an active task switch occurs during the running process, such as delaying for a period of time in sleep mode, or suspending due to waiting for an event, and the remaining time slice n has not been reduced to 0 at this time, then the value of n is recorded, and the next time the task is scheduled to run, it is counted based on the remaining time slice n.

[0005] According to the above process, when active task switching occurs during operation, the part of the running time that is less than 1 tick will not be calculated. In this way, when the task time slice is finally used up (n is reduced to 0), its actual running time will be greater than the theoretical time slice. In the process of reducing the task time slice to 0, the more active task switching occurs, the greater the difference between the actual running time of the task and the theoretical time slice.

[0006] The time distribution of traditional task scheduling based on fixed time slices is as follows Figure 1BAs shown in the figure, the initial time slice T of the task includes tick0-tickN. When the task is actively switched at time T1, the actual running time is m milliseconds, and the running time counted is 1 tick, that is, tick0 (10ms). The part less than one tick (shown as p in the figure) will not be counted in the task running time. Finally, when the task is scheduled due to the exhaustion of the time slice, the execution time it obtains will be greater than N+1 ticks, and the length of the excess time is not fixed.

[0007] Operating systems usually set different priorities for tasks according to the needs of application scenarios, and then allocate different theoretical time slice values. According to traditional methods, the actual execution time of tasks is inconsistent with the theoretical time slice, and the error is not fixed, which will inevitably lead to task scheduling failing to meet the needs of application scenarios, thus affecting system performance. Summary of the invention

[0008] The purpose of the present invention is to provide a task scheduling method, system, electronic device and storage medium based on fixed time slices, aiming to solve the problem of poor system performance caused by the inconsistency between the actual running time of the task and the theoretical time slice in the prior art.

[0009] In one aspect, the present invention provides a method for scheduling tasks based on fixed time slices, the method comprising the following steps:

[0010] Schedule tasks in the ready task queue;

[0011] Starting a timer corresponding to the task, and setting a timeout period of the timer based on a remaining time slice of the task;

[0012] When the task is actively switched, the remaining time slice is updated based on the executed time slice of the task, and the task is suspended;

[0013] When the timer times out, the remaining time slice is reset based on the initial time slice of the task, and the task is suspended.

[0014] Preferably, before scheduling the tasks in the ready task queue, the process further includes:

[0015] When the task is created, the initial time slice and the remaining time slice are set in milliseconds.

[0016] Preferably, after scheduling the tasks in the ready-to-run task queue and before starting the timer corresponding to the tasks, the method further includes:

[0017] Determine whether the remaining time slice is less than a preset time slice threshold;

[0018] If it is less than, the remaining time slice is changed based on the time slice threshold.

[0019] Preferably, before updating the remaining time slice based on the executed time slice of the task, the method further includes:

[0020] When the task is actively switched, the running time slice is recorded in milliseconds.

[0021] On the other hand, the present invention provides a task scheduling device based on fixed time slices, the device comprising:

[0022] A task scheduling unit is used to schedule tasks in the ready-to-run task queue;

[0023] A timer start unit, used to start the timer corresponding to the task and set the timeout period of the timer based on the remaining time slice of the task;

[0024] a time slice updating unit, configured to update the remaining time slice based on the executed time slice of the task and suspend the task when the task is actively switched; and

[0025] A time slice resetting unit is used to reset the remaining time slice based on the initial time slice of the task and suspend the task when the timer times out.

[0026] Preferably, the device further comprises:

[0027] The time slice setting unit is used to set the initial time slice and the remaining time slice in milliseconds when creating the task.

[0028] Preferably, the device further comprises:

[0029] A judging unit, used to judge whether the remaining time slice is less than a preset time slice threshold;

[0030] A time slice changing unit is used to change the remaining time slice based on the time slice threshold if the remaining time slice is less than the time slice threshold.

[0031] Preferably, the time slice resetting unit further includes:

[0032] The time slice recording unit is used to record the running time slice in milliseconds when the task is actively switched.

[0033] On the other hand, the present invention further provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the above method when executing the computer program.

[0034] On the other hand, the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the above method are implemented.

[0035] The present invention schedules tasks in a ready-to-run task queue, starts a timer corresponding to the task, and sets a timeout period of the timer based on a remaining time slice of the task; when the task is actively switched, the remaining time slice is updated based on the running time slice of the task, and the task is suspended; when the timer times out, the remaining time slice is reset based on an initial time slice of the task, and the task is suspended, thereby avoiding the phenomenon of execution time being missed due to active task switching, achieving precise control of task scheduling, and improving the overall performance of the system. BRIEF DESCRIPTION OF THE DRAWINGS

[0036] Figure 1A It is a flow chart of the implementation of the traditional task scheduling method based on fixed time slices provided by the background technology;

[0037] Figure 1B It is a traditional task scheduling time distribution diagram based on fixed time slices provided by the background technology;

[0038] Figure 2A is a flowchart of an implementation of a task scheduling method based on fixed time slices provided in Embodiment 1 of the present invention;

[0039] Figure 2B It is a task scheduling time distribution diagram based on fixed time slices provided in the first embodiment of the present invention;

[0040] Figure 3 is a flowchart of a task scheduling method based on fixed time slices provided in Embodiment 2 of the present invention;

[0041] Figure 4 is a structural diagram of a task scheduling device based on fixed time slices provided in Embodiment 3 of the present invention; and

[0042] Figure 5 It is a schematic diagram of the structure of an electronic device provided in Embodiment 4 of the present invention. DETAILED DESCRIPTION

[0043] In order to make the purpose, technical solution and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention.

[0044] The specific implementation of the present invention is described in detail below in conjunction with specific embodiments:

[0045] Embodiment 1:

[0046] Figure 2A The implementation process of the task scheduling method based on fixed time slices provided in the first embodiment of the present invention is shown. For the convenience of description, only the part related to the embodiment of the present invention is shown, which is described in detail as follows:

[0047] In step S201, tasks in the ready task queue are scheduled to run.

[0048] The embodiments of the present invention are applicable to electronic devices, which may be mobile phones, tablet computers, wearable devices, vehicle-mounted devices, augmented reality (AR) / virtual reality (VR) devices, laptop computers, ultra-mobile personal computers (UMPCs), netbooks, personal digital assistants (PDAs), and other terminal devices. The embodiments of the present application do not impose any restrictions on the specific types of mobile communication devices.

[0049] In an embodiment of the present invention, a task is selected from the ready task queue for scheduling and running. Before scheduling and running the task, the task needs to be created. Preferably, when creating the task, the initial time slice and the remaining time slice are set in milliseconds instead of ticks, so as to facilitate the subsequent management of the task time slice by a timer. The remaining time slice of the task set when creating the task is the same as the initial time slice.

[0050] In step S202, a timer corresponding to the task is started, and a timeout period of the timer is set based on a remaining time slice of the task.

[0051] In an embodiment of the present invention, the timer can specifically be a high-precision timer, and each task corresponds to a timer. Each time the task is scheduled for execution, the timer corresponding to the task is started, and the remaining time slice of the task is set as the timeout time of the timer, that is, the timer is triggered when the timeout time expires.

[0052] Considering that if the task is not scheduled to run for the first time, its remaining time slice may be very small, and the task will be scheduled out again after a short time, and the scheduling behavior itself has overhead, frequent scheduling in a short period of time will lead to a decrease in system performance. Before starting the timer corresponding to the task, it is preferred to determine whether the remaining time slice of the task is less than the preset time slice threshold. If it is less than, the remaining time slice is changed based on the time slice threshold to ensure that the task can be executed for enough time each time, thereby avoiding frequent scheduling in a short period of time affecting system performance and further improving system performance. Among them, the time slice threshold is the minimum time period for the task to be scheduled to run continuously once.

[0053] In step S203, when the task is actively switched, the remaining time slice is updated based on the executed time slice of the task, and the task is suspended.

[0054] In an embodiment of the present invention, before the remaining time slice is updated based on the time slice that has been run for the task, preferably, when the task is actively switched, the time slice that has been run is recorded in milliseconds so as to accurately update the remaining time slice of the task. When the task is actively switched, the timer has not been triggered, that is, the remaining time slice of the task has not been exhausted. At this time, the remaining time slice is updated based on the time slice that has been run for the task, and the task will be suspended and added to the ready task queue, waiting to be scheduled to run next time. Among them, the updated remaining time slice serves as the basis for scheduling the task to run next time.

[0055] Figure 2B The time distribution diagram of task scheduling based on fixed time slices provided in this embodiment, the initial time slice of the task is T, when the task is scheduled to run for the first time, the timeout time of the task timer is set to T, when the task is actively switched for the first time at T1, the running time slice time_past of the task is recorded, and the remaining time slice time_remain of the task is calculated, time_remain=T-time_past. When the task is scheduled to execute again, the timeout time of the task timer is set to time_remain to accurately control the task execution time.

[0056] In step S204, when the timer times out, the remaining time slice is reset based on the initial time slice of the task, and the task is suspended.

[0057] In an embodiment of the present invention, when the timer is triggered, the remaining time slice of the task has been exhausted, and its callback function will be executed. The remaining time slice will be restored to the above-mentioned initial time slice in the callback function. The task will be suspended and added to the ready task queue, waiting to be scheduled to run next time.

[0058] When the task needs to be scheduled again, steps S201-204 are repeated until the task is completed.

[0059] In an embodiment of the present invention, a task in a ready task queue is scheduled to run, a timer corresponding to the task is started, and a timeout period of the timer is set based on a remaining time slice of the task; when the task is actively switched, the remaining time slice is updated based on the running time slice of the task, and the task is suspended; when the timer times out, the remaining time slice is reset based on the initial time slice of the task, and the task is suspended, thereby avoiding the phenomenon of execution time being missed due to active task switching, achieving precise control of task scheduling, and improving the overall performance of the system.

[0060] Embodiment 2:

[0061] Figure 3 The following is an example of the implementation process of the task scheduling method based on fixed time slices provided in the second embodiment of the present invention. For the sake of convenience, only the part related to the embodiment of the present invention is shown, which is described in detail as follows:

[0062] In step S301, when creating a task, the initial time slice T and the remaining time slice t of the task are set, t=T;

[0063] In step S302, the task is scheduled to run;

[0064] In step S303, it is determined whether the remaining time slice t is less than the time slice threshold h. If so, step S304 is executed, otherwise, the process jumps to step S305.

[0065] In step S304, the remaining time slice t=h is set;

[0066] In step S305, start the timer and set the timeout time of the timer to t;

[0067] In step S306, it is determined whether the timer has timed out. If the timer has not timed out, this step is repeated. If the timer has timed out, step S307 is executed.

[0068] In step S307, the remaining time slice t=T is reset, and the task is suspended. When the task needs to be scheduled again, step S302 is executed again;

[0069] In step S308, when the task is actively switched, t is updated and the task is suspended. When the task needs to be scheduled again, steps S302-S308 are repeated.

[0070] In the real-time of the present invention, the task time slice is in milliseconds instead of ticks. Each task corresponds to a timer. When the task starts to be scheduled, the timer is turned on and the timeout interval is set to the remaining time slice of the task. When the task is actively scheduled at a certain time point during its operation, the remaining time slice of the task is accurately calculated and used as the basis for the next scheduling, thereby avoiding the phenomenon of missing task execution time due to active task switching, achieving precise control of task scheduling, and improving the overall performance of the system. In addition, by setting the minimum duration of a scheduling operation (time slice threshold), the system performance is further improved.

[0071] Embodiment three:

[0072] Figure 4The structure of the task scheduling device based on fixed time slices provided in the third embodiment of the present invention is shown. For the convenience of description, only the part related to the embodiment of the present invention is shown, including:

[0073] The task scheduling unit 41 is used to schedule tasks in the ready-to-run task queue;

[0074] A timer start unit 42, used to start the timer corresponding to the task and set the timeout period of the timer based on the remaining time slice of the task;

[0075] A time slice updating unit 43, configured to update the remaining time slice based on the executed time slice of the task and suspend the task when the task is actively switched; and

[0076] The time slice resetting unit 44 is used to reset the remaining time slice based on the initial time slice of the task and suspend the task when the timer times out.

[0077] Preferably, the device further comprises:

[0078] The time slice setting unit is used to set the initial time slice and the remaining time slice in milliseconds when creating the task.

[0079] Preferably, the device further comprises:

[0080] A judging unit, used to judge whether the remaining time slice is less than a preset time slice threshold;

[0081] The time slice changing unit is used to change the remaining time slice based on the time slice threshold if the remaining time slice is less than the time slice threshold.

[0082] Preferably, the time slice resetting unit further includes:

[0083] The time slice recording unit is used to record the running time slice in milliseconds when the task is actively switched.

[0084] In the embodiment of the present invention, each unit of the task scheduling device based on fixed time slices can be implemented by corresponding hardware or software units, and each unit can be an independent software or hardware unit, or can be integrated into a software or hardware unit, which is not intended to limit the present invention. The specific implementation of each unit of the task scheduling device based on fixed time slices can refer to the description of the aforementioned method embodiment, which will not be repeated here.

[0085] Embodiment 4:

[0086] Figure 5 The structure of an electronic device provided by a fourth embodiment of the present invention is shown. For the convenience of description, only the parts related to the embodiment of the present invention are shown.

[0087] The electronic device 5 of the embodiment of the present invention includes a processor 50, a memory 51, and a computer program 52 stored in the memory 51 and executable on the processor 50. When the processor 50 executes the computer program 52, the steps in the above-mentioned method embodiments are implemented, for example: Figure 2A Alternatively, when the processor 50 executes the computer program 52, the functions of each unit in the above-mentioned device embodiments are realized, for example Figure 4 The functions of the units 41 to 44 are shown.

[0088] In an embodiment of the present invention, a task in a ready task queue is scheduled to run, a timer corresponding to the task is started, and a timeout period of the timer is set based on a remaining time slice of the task; when the task is actively switched, the remaining time slice is updated based on the running time slice of the task, and the task is suspended; when the timer times out, the remaining time slice is reset based on the initial time slice of the task, and the task is suspended, thereby avoiding the phenomenon of execution time being missed due to active task switching, achieving precise control of task scheduling, and improving the overall performance of the system.

[0089] Embodiment five:

[0090] In an embodiment of the present invention, a computer-readable storage medium is provided, which stores a computer program. When the computer program is executed by a processor, the steps in the above method embodiment are implemented, for example Figure 2A Alternatively, when the computer program is executed by a processor, the functions of each unit in the above-mentioned device embodiments are realized, for example Figure 4 The functions of the units 41 to 44 are shown.

[0091] In an embodiment of the present invention, a task in a ready task queue is scheduled to run, a timer corresponding to the task is started, and a timeout period of the timer is set based on a remaining time slice of the task; when the task is actively switched, the remaining time slice is updated based on the running time slice of the task, and the task is suspended; when the timer times out, the remaining time slice is reset based on the initial time slice of the task, and the task is suspended, thereby avoiding the phenomenon of execution time being missed due to active task switching, achieving precise control of task scheduling, and improving the overall performance of the system.

[0092] The computer-readable storage medium of the embodiment of the present invention may include any entity or device or recording medium capable of carrying computer program code, for example, ROM / RAM, magnetic disk, optical disk, flash memory and other memories.

[0093] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the protection scope of the present invention.

Claims

1. A task scheduling method based on fixed time slices, characterized in that: The method comprises the following steps: Schedule tasks in the ready task queue; Determine whether the remaining time slice of the task is less than a preset time slice threshold; If it is less than, changing the remaining time slice based on the time slice threshold; Starting a timer corresponding to the task, and setting a timeout period of the timer based on a remaining time slice of the task; When the task is actively switched, the remaining time slice is updated based on the executed time slice of the task, and the task is suspended; When the timer times out, the remaining time slice is reset based on the initial time slice of the task, and the task is suspended.

2. The method according to claim 1, characterized in that Before scheduling tasks in the ready task queue, it also includes: When the task is created, the initial time slice and the remaining time slice are set in milliseconds.

3. The method according to claim 1, characterized in that Before updating the remaining time slice based on the executed time slice of the task, the method further includes: When the task is actively switched, the running time slice is recorded in milliseconds.

4. A task scheduling device based on fixed time slices, characterized in that: The device comprises: A task scheduling unit is used to schedule tasks in the ready-to-run task queue; A judging unit, used to judge whether the remaining time slice of the task is less than a preset time slice threshold; a time slice changing unit, configured to change the remaining time slice based on the time slice threshold if the remaining time slice is less than the time slice threshold; A timer start unit, used to start the timer corresponding to the task and set the timeout period of the timer based on the remaining time slice of the task; a time slice updating unit, configured to update the remaining time slice based on the executed time slice of the task and suspend the task when the task is actively switched; and A time slice resetting unit is used to reset the remaining time slice based on the initial time slice of the task and suspend the task when the timer times out.

5. The device according to claim 4, characterized in that The device also includes: The time slice setting unit is used to set the initial time slice and the remaining time slice in milliseconds when creating the task.

6. The device according to claim 4, characterized in that The time slice resetting unit also includes: The time slice recording unit is used to record the running time slice in milliseconds when the task is actively switched.

7. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 3 are implemented.

8. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 3 are implemented.

Citation Information

Patent Citations

  • Task scheduling processing system and method of embedded real-time operating system

    CN111367652A

  • Scheduling in a multicore architecture

    CN1955931A