Implementation method of real-time operating system without field protection

By allocating a stack space for periodic tasks and allowing high-priority tasks to be preempted, the problem of wasting RAM space resources in existing real-time operating systems is solved, and the effect of saving RAM space in embedded systems is achieved.

CN119829241APending Publication Date: 2025-04-15SHANGHAI AUTOMOTIVE CHIP ENG CENT CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411881921.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-19
Publication Date
2025-04-15

AI Technical Summary

Technical Problem

Existing real-time operating systems need to allocate a stack space for each task during task switching, resulting in unnecessary waste of RAM space resources, especially in embedded systems with smaller RAM space.

Method used

By allocating a stack space for all periodic tasks, high-priority task preemption is allowed only after the scheduling function of the periodic task is executed, avoiding unnecessary field protection.

Benefits of technology

It realizes a real-time operating system that does not require protection on the site, saves a lot of RAM space, and is suitable for embedded systems with smaller RAM space, such as body controllers.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119829241A_ABST
    Figure CN119829241A_ABST
Patent Text Reader

Abstract

The invention discloses a method for realizing a real-time operating system without field protection, which comprises the following steps of: S1, distributing a stack space for all periodic tasks for executing the periodic tasks; initializing periodic tasks, wherein each periodic task comprises a plurality of states with different priorities and a plurality of scheduling functions; s2, activating and executing the periodic task according to the state of the periodic task; and S3, when the interruption occurs and one scheduling function in the currently executed periodic task is executed, allowing the scheduling function to be preempted by the periodic task with the high priority. According to the invention, a large amount of RAM space can be saved, and the problem of unnecessary waste of RAM space resources is solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of real-time operating systems, and in particular to a method for realizing a real-time operating system that does not require on-site protection. Background Art

[0002] The current real-time operating systems all allocate a stack space for each task. When the task is switched, the kernel register value of the current task is pushed onto the stack space of the current task, and the current stack pointer is saved. Then, the stack pointer of the new task is obtained, and the kernel register value is popped out from the stack space of the new task and restored to the corresponding registers.

[0003] However, each task needs to be allocated a stack space for protecting the scene, which will take up a lot of RAM space. For embedded systems that do not have such high real-time requirements, such as body controllers, this will cause unnecessary waste of RAM space resources. Especially for controllers with smaller RAM space, the use of RAM space resources will become very tight or even insufficient, and some functions will be abandoned. Summary of the Invention

[0004] The object of the present invention is to provide a method for implementing a real-time operating system that does not require field protection, thereby solving the problem of unnecessary waste of RAM space resources.

[0005] In order to solve the above problems, the present invention is achieved through the following technical solutions:

[0006] A method for implementing a real-time operating system that does not require field protection includes:

[0007] Step S1: Allocate a stack space for all periodic tasks for executing the periodic tasks; initialize the periodic tasks, each periodic task includes several states with different priorities and several scheduling functions;

[0008] Step S2: Activate and execute the periodic task according to the status of the periodic task;

[0009] Step S3: When an interrupt occurs and a scheduling function in the currently executed periodic task is completed, it is allowed to be preempted by a periodic task with a higher priority.

[0010] Optionally, the periodic tasks include: tasks with a period of 1ms, 5ms or 10ms.

[0011] Optionally, the byte space of the stack space is equal to the bytes of the largest stack used in all periodic tasks.

[0012] Optionally, the states include: Suspend state, indicating that the periodic task is in a suspended state and has not been activated; Ready state, indicating that the periodic task is in an activated and ready state and can be executed at any time; Running state, indicating that the periodic task is being executed; the Ready state includes the Running state, and the Running state has the highest priority.

[0013] Optionally, the step of initializing the periodic task in step S1 includes:

[0014] Step S1.1, respectively calculate the number of scheduling functions contained in each periodic task;

[0015] Step S1.2: Each periodic task is provided with a pointer for pointing to the next scheduling function to be executed. During initialization, the pointer of each periodic task is set to point to its first scheduling function.

[0016] Step S1.3: During initialization, the status of the periodic task to be executed after initialization is set to the Ready status, and the status of the remaining periodic tasks is set to the Suspend status.

[0017] Optionally, step S2 includes:

[0018] Step S2.1: Determine whether there is a periodic task in the Running state. If yes, execute step S2.2; if not, execute step S2.3.

[0019] Step S2.2: Determine whether there is a periodic task in the current Ready state with a higher priority than the currently executing Running state periodic task. If so, change the state of the Running state periodic task to the Ready state and execute step S2.3. If not, execute step S2.4.

[0020] Step S2.3: Set the state of the periodic task with the highest priority in the Ready state to the Running state, and execute step S2.4;

[0021] Step S2.4: Execute the scheduling function pointed to by the pointer of the periodic task in the Running state. After the execution is completed, proceed to step S2.5;

[0022] Step S2.5: Determine whether all the scheduling functions in the periodic task in the current Running state have been executed. If not, execute step S2.6; if all have been executed, execute step S2.7;

[0023] Step S2.6: Increase the pointer of the periodic task in the Running state by 1, and return to step S2.1;

[0024] Step S2.7: Set the state of the periodic task in the Running state to the Suspend state, and re-point its pointer to the first scheduling function in the periodic task, and return to step S2.1.

[0025] Optionally, step S3 includes:

[0026] Step S3.1, configure a timer and an interrupt counter; the timer generates an interrupt once every predetermined time, and each time an interrupt occurs, the interrupt counter increases by 1;

[0027] Step S3.2: Determine whether the counter remainder N is equal to 0. If so, proceed to step S3.3; if not, proceed to step S3.4;

[0028] Step S3.3, add 1 to the count of the periodic tasks with a period of N in the Ready state, and proceed to step S3.4;

[0029] Step S3.4: Determine whether the periodic task is in the Suspend state. If so, execute step S3.5;

[0030] If not, proceed to step S3.7;

[0031] Step S3.5: Determine whether the Ready status count of the periodic task is greater than 0. If it is greater than 0, proceed to step S3.6; if it is equal to 0, the periodic task remains in the Suspend state and proceed to step S3.8;

[0032] Step S3.6: decrement the Ready status count of the periodic task by 1, and change the status of the periodic task from Suspend to Ready, and proceed to step S3.7;

[0033] Step S3.7: All periodic tasks call steps S3.2 to S3.6 in sequence until all periodic tasks exit the interruption after executing the above steps.

[0034] On the other hand, the present invention further provides an electronic device, comprising a processor and a memory, wherein a computer program is stored in the memory, and when the computer program is executed by the processor, the method described above is implemented.

[0035] In other aspects, the present invention further provides a readable storage medium, wherein the readable storage medium stores a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1 to 7 is implemented.

[0036] The present invention has the following technical effects:

[0037] The real-time operating system provided by the present invention does not require a security field. It only requires a single stack space, saving a significant amount of RAM space. This solves the problem of unnecessary waste of RAM resources. When this real-time operating system is applied to a vehicle body controller, the substantial RAM savings enable the inclusion of more functions within the controller.

[0038] Specifically, the present invention supports that a scheduling function of a periodic task can only be preempted by a high-priority periodic task after execution, and only a stack space needs to be allocated for the periodic task. This can save a lot of RAM space for embedded systems that do not have such high real-time requirements, such as body controllers. For example, without considering interrupt preemption, a 1ms, a 5ms, and a 10ms task are now required, and the maximum stacks used by the 1ms, 5ms, and 10ms tasks are 1024, 512, and 512 bytes respectively. Therefore, 1024+512+512 bytes of space need to be allocated for task execution, while the method provided by the present invention only needs to allocate 1024 bytes, which can save 1024 bytes. In actual applications, more periodic tasks are needed, and each periodic task also needs to occupy a larger stack space. This can save a lot of RAM space for embedded systems that do not have such high real-time requirements, especially for controllers with smaller RAM space. BRIEF DESCRIPTION OF THE DRAWINGS

[0039] Figure 1 A flowchart of a method for implementing a real-time operating system that does not require a protection scene is provided in one embodiment of the present invention;

[0040] Figure 2 A schematic diagram of a task initialization process according to an embodiment of the present invention;

[0041] Figure 3 A schematic diagram of the operation process of a real-time operating system provided by one embodiment of the present invention;

[0042] Figure 4 A schematic diagram of a task interruption process according to an embodiment of the present invention. DETAILED DESCRIPTION

[0043] The following is a further detailed description of a method for implementing a real-time operating system that does not require a protection site, as proposed by the present invention, in conjunction with the accompanying drawings and specific embodiments. According to the following description, the advantages and features of the present invention will become clearer. It should be noted that the drawings are in a very simplified form and use non-precise proportions, which are only used to conveniently and clearly assist in explaining the purpose of the embodiments of the present invention. In order to make the purpose, features and advantages of the present invention more obvious and easy to understand, please refer to the drawings. It should be noted that the structures, proportions, sizes, etc. illustrated in the drawings of this specification are only used to match the contents disclosed in the specification for people familiar with this technology to understand and read, and are not used to limit the conditions for the implementation of the present invention, so they have no technical significance. Any modification of the structure, change in the proportional relationship or adjustment of the size should still fall within the scope of the technical content disclosed by the present invention without affecting the efficacy and purpose that can be achieved by the present invention.

[0044] like Figure 1 As shown, this embodiment provides a method for implementing a real-time operating system that does not require field protection, including: Step S1, allocating a stack space for all periodic tasks for their execution; initializing the periodic tasks, each of which includes several states with different priorities and several scheduling functions; Step S2, activating and executing the periodic tasks based on their states; Step S3, when an interrupt occurs and a scheduling function in the currently executing periodic task has completed execution, allowing the higher-priority periodic task to preempt it.

[0045] The real-time operating system provided in this embodiment does not require a security context. It only requires a single stack space, saving a significant amount of RAM. This eliminates the problem of unnecessary waste of RAM resources. When this real-time operating system is applied to a vehicle body controller, the substantial RAM savings enable the inclusion of more functions within the controller.

[0046] Current real-time operating systems (RTOSs) support the preemption of task scheduling functions by other high-priority tasks during execution. This requires allocating stack space for each task for context protection, which consumes a significant amount of RAM. However, this embodiment supports periodic tasks, where a scheduling function can only be preempted by a high-priority periodic task after completion. This only requires allocating stack space for the periodic task. This can save significant RAM space for embedded systems with less stringent real-time requirements, such as vehicle body controllers.

[0047] Optionally, the periodic tasks herein include tasks with a period of 1 ms, 5 ms, or 10 ms, but the present invention is not limited thereto.

[0048] In this embodiment, the byte space of the stack space is equal to the bytes of the largest stack used by all periodic tasks. That is, this embodiment only needs to allocate a piece of stack space for periodic tasks. For example, without considering interrupt preemption, a task with a period of 1ms, a task with a period of 5ms, and a task with a period of 10ms is required. The maximum stack used by the 1ms, 5ms, and 10ms tasks is 1024, 512, and 512 bytes, respectively. Therefore, 1024+512+512 bytes of space need to be allocated for task execution. However, the method provided by this real-time method only needs to allocate 1024 bytes, thus saving 1024 bytes. In actual applications, more periodic tasks are required, and each periodic task also needs to occupy a larger stack space. This can save a lot of RAM space for embedded systems with less real-time requirements, especially for controllers with less RAM space.

[0049] In this embodiment, the states include the following three: Suspend state, which indicates that the periodic task is in a suspended state and has not yet been activated. Ready state, which indicates that the periodic task is in an activated and ready state and can be executed at any time. Running state, which indicates that the periodic task is currently executing. The Ready state includes the Running state, and the Running state has the highest priority.

[0050] like Figure 2 As shown, the step of initializing the periodic task in step S1 includes:

[0051] Step S1.1, respectively calculate the number of scheduling functions contained in each periodic task;

[0052] Step S1.2: Each periodic task is provided with a pointer for pointing to the next scheduling function to be executed. During initialization, the pointer of each periodic task is set to point to its first scheduling function.

[0053] Step S1.3: During initialization, the status of the periodic task to be executed after initialization is set to the Ready status, and the status of the remaining periodic tasks is set to the Suspend status.

[0054] In this embodiment, if Figure 3 As shown, step S2 includes:

[0055] Step S2.1: Determine whether there is a periodic task in the Running state. If yes, execute step S2.2; if not, execute step S2.3.

[0056] Step S2.2: Determine whether there is a periodic task in the current Ready state with a higher priority than the currently executing Running state periodic task. If so, change the state of the Running state periodic task to the Ready state and execute step S2.3. If not, execute step S2.4.

[0057] Step S2.3: Set the state of the periodic task with the highest priority in the Ready state to the Running state, and execute step S2.4;

[0058] Step S2.4: Execute the scheduling function pointed to by the pointer of the periodic task in the Running state. After the execution is completed, proceed to step S2.5;

[0059] Step S2.5: Determine whether all the scheduling functions in the periodic task in the current Running state have been executed. If not, execute step S2.6; if all have been executed, execute step S2.7;

[0060] Step S2.6: Increase the pointer of the periodic task in the Running state by 1, and return to step S2.1;

[0061] Step S2.7: Set the state of the periodic task in the Running state to the Suspend state, and re-point its pointer to the first scheduling function in the periodic task, and return to step S2.1.

[0062] In this embodiment, if Figure 4 As shown, step S3 includes:

[0063] Step S3.1, configure a timer and an interrupt counter; the timer generates an interrupt once every predetermined time, and each time an interrupt occurs, the interrupt counter increases by 1;

[0064] Step S3.2: Determine whether the counter remainder N is equal to 0. If so, proceed to step S3.3; if not, proceed to step S3.4;

[0065] Step S3.3, add 1 to the count of the periodic tasks with a period of N in the Ready state, and proceed to step S3.4;

[0066] Step S3.4: Determine whether the periodic task is in the Suspend state. If so, execute step S3.5;

[0067] If not, proceed to step S3.7;

[0068] Step S3.5: Determine whether the Ready status count of the periodic task is greater than 0. If it is greater than 0, proceed to step S3.6; if it is equal to 0, the periodic task remains in the Suspend state and proceed to step S3.7;

[0069] Step S3.6: decrement the Ready status count of the periodic task by 1, and change the status of the periodic task from Suspend to Ready, and proceed to step S3.7;

[0070] Step S3.7: All periodic tasks call steps S3.2 to S3.6 in sequence until all periodic tasks exit the interruption after executing the above steps.

[0071] The above embodiment is further described below with reference to a specific embodiment to provide a better understanding of the above embodiment.

[0072] During the initialization process, the scheduling function that needs to be executed every 1ms is placed in a periodic task with a period of 1ms (referred to as 1ms task), and the scheduling function that needs to be executed every 5ms is placed in a periodic task with a period of 5ms (referred to as 5ms task). The 1ms task will be activated every 1ms, and the 5ms task will be activated every 5ms. The task with the highest priority among all activated tasks will be executed first. Assuming that the current highest priority task is the 5ms task, it has 10 scheduling functions, then it will be executed in sequence starting from the first scheduling function of the 5ms task. During the execution of the third scheduling function, an interrupt occurs, that is, when a higher priority 1ms task is activated, the scheduling function in the 1ms task will be executed after the third scheduling function is executed. When the 1ms task is executed, the fourth scheduling function in the 5ms task will continue to be executed. After all 10 scheduling functions in the 5ms task are executed, the 5ms task enters a suspended state. When it is activated again, it will start executing from the first scheduling function in the 5ms task again.

[0073] Please continue to refer to Figure 3 As shown, the following takes the 1ms interrupt process as an example to further explain the interrupt processing process, so as to better understand the interrupt process described above:

[0074] Step S3.1: A timer generates an interrupt every 1ms, and an interrupt counter is incremented by 1 each time an interrupt occurs.

[0075] Step S3.2: Taking the 5ms task as an example, determine whether the remainder of the counter 5 is equal to 0, that is, every time there are 5 1ms interrupts, the 5ms task needs to be activated once. If it is equal to 0, proceed to step S3.3; if not, proceed directly to step S3.4.

[0076] Step S3.3: Increase the Ready status count of the 5ms task by 1, and execute step S3.4;

[0077] Step S3.4: Determine whether the 5ms task is in the Suspend state. If so, execute step S3.5. If not, it means that the 5ms task has not been completed since it was last in the Ready or Running state, and execute step S3.7.

[0078] Step S3.5, determine whether the Ready status count of the 5ms task is greater than 0. If it is greater than 0, it means that the 5ms task needs to be activated. At this time, execute step S3.6, reduce the Ready status count by 1, and change the 5ms task from Suspend status to Ready status, execute step S3.7. If it is equal to 0, maintain the Suspend status, and execute step S3.7.

[0079] In step S3.7, all tasks (such as 1ms, 5ms, 10ms tasks, etc.) execute steps S3.2 to S3.6 in sequence, and exit the interrupt when all tasks have completed the above steps.

[0080] It can be seen that the real-time operating system of this embodiment is configured with three function interfaces, which configure the timer to generate an interrupt every 1ms and call Figure 4 As shown in the operating system interrupt process (Os_Interrupt function), after all the initialization tasks in the main function (main function) are completed, call Figure 2 The operating system initialization function (Os_Init) shown in the following example is then called: Figure 3 The startup function shown (OS_Start function is an important function for starting multi-task scheduling in μC / OS-II operating system) can run the real-time operating system normally and perform task preemption.

[0081] On the other hand, the present invention further provides an electronic device, comprising a processor and a memory, wherein a computer program is stored in the memory, and when the computer program is executed by the processor, the method described above is implemented.

[0082] In other aspects, the present invention further provides a readable storage medium, wherein the readable storage medium stores a computer program, and when the computer program is executed by a processor, the method described above is implemented.

[0083] It should be noted that, in this document, the terms "comprises," "includes," or any other variations thereof are intended to encompass non-exclusive inclusion, such that a process, method, article, or apparatus comprising a series of elements includes not only those elements but also other elements not explicitly listed, or elements inherent to such process, method, article, or apparatus. In the absence of further limitations, an element defined by the phrase "comprising a ..." does not exclude the presence of other identical elements in the process, method, article, or apparatus comprising the element.

[0084] It should be noted that the flowcharts and block diagrams in the accompanying drawings show the possible architectures, functions and operations of the devices, methods and computer program products according to multiple embodiments of the present invention. In this regard, each box in the flowchart or block diagram can represent a part of a module, program or code, and the module, program segment or code part contains one or more executable instructions for implementing the specified logical function, and the module, program segment or code part contains one or more executable instructions for implementing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in an order different from that marked in the accompanying drawings. For example, two consecutive boxes can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram and / or flowchart, and the combination of boxes in the block diagram and / or flowchart, can be implemented by a dedicated hardware-based system for performing the specified function or action, or can be implemented by a combination of dedicated hardware and computer instructions.

[0085] Although the present invention has been described in detail through the above preferred embodiments, it should be understood that the above description is not intended to limit the present invention. After reading the above description, various modifications and substitutions of the present invention will become apparent to those skilled in the art. Therefore, the scope of protection of the present invention should be defined by the appended claims.

Claims

1. A method for implementing a real-time operating system that does not require a protection scene, characterized in that: include: Step S1, allocating a stack space for all periodic tasks for the execution of periodic tasks; Initialize the periodic tasks, each of which contains several states with different priorities and several scheduling functions; Step S2: activating and executing the periodic task according to the status of the periodic task; Step S3: When an interrupt occurs and a scheduling function in the currently executed periodic task is executed, it is allowed to be preempted by a periodic task with a higher priority.

2. The method for implementing a real-time operating system that does not require a protection scene as claimed in claim 1, characterized in that: The periodic tasks include: tasks with a period of 1 ms, 5 ms or 10 ms.

3. The method for implementing a real-time operating system that does not require a protection scene as claimed in claim 1, characterized in that: The byte space of the stack space is equal to the bytes of the largest stack used by all periodic tasks.

4. The method for implementing a real-time operating system that does not require a protection scene as claimed in claim 1, characterized in that: The states include: Suspend state means that the periodic task is in a suspended state and has not been activated; Ready status means that the periodic task is in the activated and ready state and can be executed at any time; Running status, indicating that the periodic task is being executed; The Ready state includes the Running state, and the Running state has the highest priority.

5. The method for implementing a real-time operating system that does not require protection of the scene as claimed in claim 4, characterized in that: The step of initializing the periodic task in step S1 includes: Step S1.1, respectively calculate the number of scheduling functions contained in each periodic task; Step S1.2, each periodic task is provided with a pointer corresponding to the next scheduling function to be executed, and during initialization, the pointer of each periodic task is pointed to its first scheduling function; Step S1.3: During initialization, the status of the periodic task to be executed after initialization is set to the Ready status, and the status of the remaining periodic tasks is set to the Suspend status.

6. The method for implementing a real-time operating system that does not require protection of the scene as claimed in claim 5, characterized in that: The step S2 comprises: Step S2.1, determine whether there is a periodic task in the Running state, if yes, execute step S2.2, if no, execute step S2.3; Step S2.2, determine whether there is a periodic task in the current Ready state with a higher priority than the currently executing Running state periodic task. If yes, change the state of the Running state periodic task to the Ready state and execute step S2.

3. If no, execute step S2.4; Step S2.3, setting the state of the periodic task with the highest priority among the periodic tasks in the Ready state to the Running state, and executing step S2.4; Step S2.4, execute the scheduling function pointed to by the pointer of the periodic task in the Running state, and after execution, proceed to step S2.5; Step S2.5, determine whether all the scheduling functions in the periodic task in the current Running state have been executed. If not, execute step S2.6; if all have been executed, execute step S2.7; Step S2.6, the pointer of the periodic task in the Running state is increased by 1, and the process returns to step S2.1; Step S2.7, set the state of the periodic task in the Running state to the Suspend state, and re-point its pointer to the first scheduling function in the periodic task, and return to step S2.

1.

7. The method for implementing a real-time operating system that does not require protection of the scene as claimed in claim 6, characterized in that: The step S3 comprises: Step S3.1, configure a timer and an interrupt counter; the timer generates an interrupt once every predetermined time, and each time an interrupt occurs, the interrupt counter increases by 1; Step S3.2, determine whether the counter remainder N is equal to 0, if so, proceed to step S3.3, if not, proceed to step S3.4; Step S3.3, add 1 to the count of the periodic tasks with a period of N in the Ready state, and proceed to step S3.4; Step S3.4, determine whether the periodic task is in the Suspend state, if so, execute step S3.5; If not, execute step S3.7; Step S3.5, determine whether the count of the Ready state of the periodic task is greater than 0, if it is greater than 0, proceed to step S3.6; if it is equal to 0, the periodic task remains in the Suspend state, proceed to step S3.7; Step S3.6, reduce the count of the Ready state of the periodic task by 1, and change the state of the periodic task from the Suspend state to the Ready state, and proceed to step S3.7; Step S3.7: All periodic tasks call steps S3.2 to S3.6 in sequence until all periodic tasks exit the interrupt after executing the above steps.

8. An electronic device, characterized in that: The method comprises a processor and a memory, wherein a computer program is stored in the memory, and when the computer program is executed by the processor, the method according to any one of claims 1 to 7 is implemented.

9. A readable storage medium, characterized in that: The readable storage medium stores a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1 to 7 is implemented.