Message queue implementation method under FreeRTOS system and computer program product
By dynamically adjusting the memory pool size of the message queue in the FreeRTOS system, the problem of wasted memory management in message queues is solved, achieving more efficient memory usage and cost savings.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-12
- Publication Date
- 2026-04-07
AI Technical Summary
The message queue in the FreeRTOS system has a large overhead in memory management, which leads to memory waste and unnecessary consumption of resources.
The message pool size is dynamically adjusted using the realloc method. The source task checks the space before sending a message, and the target task checks the available space after retrieving a message, thus realizing the expansion and release of the message pool and avoiding frequent memory allocation and deallocation.
This reduces system memory usage, lowers hardware requirements, and saves costs.
Smart Images

Figure CN121807584A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of data transmission technology, and in particular to a message queue implementation method and computer program product under the FreeRTOS system. Background Technology
[0002] In the FreeRTOS system, dequeues can be used to pass messages between different tasks, which is a method of inter-task synchronization, but each message requires a certain amount of memory.
[0003] The message queue provided by the FreeRTOS system is implemented based on Queue_t. It requires pre-allocating space before use; allocating too little space will cause system malfunctions. Therefore, in practice, to meet functional requirements, more space is usually allocated, but this leads to memory waste. Furthermore, each memory allocation incurs control block overhead, and the more frequent the memory allocation, the greater the waste. Summary of the Invention
[0004] The purpose of this application is to provide a message queue implementation method and computer program product under the FreeRTOS system, so as to solve the technical problem that the inter-task message queue function built into the FreeRTOS system in the prior art has a large memory overhead. The various technical effects of the preferred technical solution among the many technical solutions provided in this application are described in detail below.
[0005] To achieve the above objectives, this application provides the following technical solutions:
[0006] The first aspect of this application provides a message queue implementation method under the FreeRTOS system. This method is applied to a source task sending messages to a target task, comprising: the source task determining whether there is still space in the message pool; if not, expanding the message pool using the `realloc` method, and then adding the message to the message pool; the source task determining whether the target task is waiting for the message; if so, waking up the target task and ending the source task's sending process; if the target task's waiting does not time out, determining whether there is a message in the message pool with the same ID as its own target task; if so, retrieving the message from the message pool; then determining whether the free space in the message pool has reached a preset threshold; if so, releasing the free space in the message pool using the `realloc` method, and then ending the target task's receiving process.
[0007] In some embodiments, the message queue implementation method under the FreeRTOS system further includes: constructing a message pool of a preset size, wherein the message pool is dynamically allocated memory, and all tasks share the same message pool.
[0008] In some embodiments, the message queue implementation method under the FreeRTOS system further includes: if there is still space in the message pool, the source task directly adds the message to the message pool.
[0009] In some embodiments, adding the message to the message pool includes: the source task obtaining a free message block from the message pool and adding the message to the free message block.
[0010] In some embodiments, the message queue implementation method under the FreeRTOS system further includes: if the target task is not waiting for the message, then directly terminate the sending process of the source task.
[0011] In some embodiments, the message queue implementation method under the FreeRTOS system further includes: if the target task times out, the receiving process of the target task is directly terminated.
[0012] In some embodiments, the message queue implementation method under the FreeRTOS system further includes: if there is no message with the same ID as the target task in the message pool, the target task enters a dormant state.
[0013] In some embodiments, the message queue implementation method under the FreeRTOS system further includes: if the free space of the message pool does not reach the preset threshold, the receiving process of the target task is directly terminated.
[0014] In some embodiments, the message includes the target task ID, message data, and message length.
[0015] A second aspect of this application provides a computer program product stored on a data carrier and designed to execute the message queue implementation method under the FreeRTOS system as described above.
[0016] Implementing one of the above-mentioned technical solutions of this application has the following advantages or beneficial effects: In this application, when the source task sends a message to the target task, it can check whether there is still space in the message pool. If the space is insufficient, the message pool can be expanded using the realloc method. Then, when the target task retrieves a message from the message pool, the free space of the message pool can be checked again. If the free space reaches a preset size, the message pool can be released using the realloc method. Thus, the size of the message pool can be dynamically adjusted in the sending and receiving processes. The embodiments of this application can reduce the system's memory overhead, reduce hardware requirements, and save costs. Attached Figure Description
[0017] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort. In the drawings:
[0018] Figure 1 This is a flowchart illustrating the message queue implementation method under the FreeRTOS system in this application embodiment;
[0019] Figure 2 This is a schematic diagram illustrating the interaction between the source task, message pool, and target task in an embodiment of this application. Detailed Implementation
[0020] To make the objectives, technical solutions, and advantages of this application clearer, various exemplary embodiments described below will be referenced to the accompanying drawings, which form part of the exemplary embodiments and depict various exemplary embodiments that may be adopted to implement this application. Unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this disclosure. It should be understood that they are merely examples of processes, methods, and apparatuses consistent with some aspects of this application disclosed as detailed in the appended claims, and other embodiments may be used, or structural and functional modifications may be made to the embodiments listed herein without departing from the scope and spirit of this application.
[0021] In the description of this application, it should be understood that the terms "center," "longitudinal," "lateral," etc., indicate the orientation or positional relationship based on the accompanying drawings, and are only for the convenience of describing this application and simplifying the description, and do not indicate or imply that the referred element must have a specific orientation, or be constructed and operated in a specific orientation. The terms "first," "second," etc., are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of indicated technical features. The term "multiple" means two or more. The terms "connected" and "linked" should be interpreted broadly, for example, they can be fixed connections, detachable connections, integral connections, mechanical connections, electrical connections, communication connections, direct connections, indirect connections through an intermediate medium, and can be the internal connection of two elements or the interaction relationship between two elements. The term "and / or" includes any and all combinations of one or more of the related listed items. Those skilled in the art can understand the specific meaning of the above terms in this application according to the specific circumstances.
[0022] To illustrate the technical solutions described in this application, specific embodiments are provided below, showing only the parts related to the embodiments of this application.
[0023] like Figures 1 to 2 As shown, this application provides a message queue implementation method under the FreeRTOS system. The message queue implementation method under the FreeRTOS system is applied to send messages from a source task to a target task, including the following steps (steps S1 to S3):
[0024] S1. The source task checks if there is enough space in the message pool. If not, it expands the message pool using the `realloc` function and then adds messages to the pool. The `realloc` function can be used to dynamically adjust the size of allocated memory blocks.
[0025] In some embodiments, the message queue implementation method under the FreeRTOS system may further include: constructing a message pool of a preset size, wherein the message pool is dynamically allocated memory, and all tasks share the same message pool. The message pool can simultaneously contain messages from multiple different tasks, such as messages from source task A and source task B.
[0026] In some embodiments, a message may include a target task ID, message data, and message length. The target task ID can be used to indicate the target task to which the message is to be sent, and the target task can retrieve the corresponding message from the message pool based on the target task ID.
[0027] In some embodiments, the message queue implementation method under the FreeRTOS system may further include: if there is still space in the message pool, the source task directly adds a message to the message pool.
[0028] In some embodiments, adding a message to a message pool may include: the source task obtaining a free message block from the message pool and adding the message to the free message block.
[0029] S2. The source task determines whether the target task is waiting for a message. If so, it wakes up the target task and then ends the sending process of the source task.
[0030] In some embodiments, the message queue implementation method under the FreeRTOS system may further include: if the target task is not waiting for a message, then directly terminate the sending process of the source task.
[0031] S3. If the target task waits without timeout, check if there is a message in the message pool with the same ID as the target task. If there is, take the message from the message pool. Then check if the free space in the message pool has reached the preset threshold. If so, release the free space in the message pool using the realloc method, and then end the target task receiving process.
[0032] In some embodiments, the message queue implementation method under the FreeRTOS system may further include: if the target task times out, the receiving process of the target task is directly terminated.
[0033] In some embodiments, the message queue implementation method under the FreeRTOS system may further include: if there is no message with the same ID as its target task in the message pool, the target task enters a sleep state. Thus, the target task can wait for the source task to wake it up next time.
[0034] In some embodiments, the message queue implementation method under the FreeRTOS system may further include: if the free space of the message pool does not reach a preset threshold, the receiving process of the target task is directly terminated.
[0035] Specifically, when the source task needs to send a message to the target task, it first looks for an idle and available message block in the message pool. After receiving the message, the target task will re-mark this message block as idle.
[0036] If the target task does not collect the message, the available resources in the message pool will decrease. When the source task needs to send a message, there will be no available space in the message pool, which will trigger expansion, that is, the message pool will be expanded using the realloc method.
[0037] After the target task receives the messages, it checks the currently idle message blocks. If the number of idle message blocks reaches a certain proportion, that is, if the free space in the message pool reaches a preset threshold, it will trigger memory release. Expanding and releasing the message pool does not involve multiple memory allocations and does not incur control block overhead; the message pool is a contiguous block of memory.
[0038] In summary, when the source task sends a message to the target task in this embodiment, it can pre-check the space of the message pool. If the space is insufficient, the capacity of the message pool can be expanded using the realloc method. Then, after the target task retrieves a message from the message pool, it will check the free space of the message pool again. If it finds that the free space in the message pool exceeds a certain proportion, it will release a portion of the memory, thereby achieving the effect of dynamically adjusting the size of the message pool in both the sending and receiving processes.
[0039] In this application, when the source task sends a message to the target task, it can check whether there is still space in the message pool. If the space is insufficient, the message pool can be expanded using the realloc method. Then, when the target task retrieves a message from the message pool, it can check the free space of the message pool again. If the free space reaches a preset size, the message pool can be released using the realloc method. This allows the size of the message pool to be dynamically adjusted during the sending and receiving processes. The embodiments of this application can reduce the system's memory overhead, reduce hardware requirements, and save costs.
[0040] Those skilled in the art will understand that all or part of the features / steps of the above-described method embodiments can be implemented by methods, data processing systems, or computer programs. These features may be implemented without hardware, entirely in software, or in a combination of hardware and software. The aforementioned computer program may be stored in one or more computer-readable storage media. When the computer program is executed (e.g., by a processor), it performs the steps of the message queue implementation method embodiments under the FreeRTOS system described above.
[0041] The aforementioned storage media capable of storing program code include: static hard disks, solid-state hard disks, random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), optical storage devices, magnetic storage devices, flash memory, magnetic disks or optical disks and / or combinations thereof, that is, they can be implemented by any type of volatile or non-volatile storage devices or combinations thereof.
[0042] This application also provides a processing device embodiment, including one or more processors and a memory; wherein the memory is used to store one or more computer programs, and the one or more processors are used to execute the one or more computer programs stored in the memory, so that the processors execute the features / steps of the message queue implementation method embodiment under the FreeRTOS system described above.
[0043] This application also provides a computer program product, which is stored on a data carrier and designed to execute the message queue implementation method under the FreeRTOS system as described above. Therefore, the computer program product according to this application produces the same advantages as those described in the detailed description of the device according to this application. The computer program product can be executed as computer-readable instruction code using any suitable programming language such as JAVA, C++, etc. Furthermore, the computer program product can be provided on a network, such as the Internet, or downloaded from a network, such as the Internet, by a network, such as the Internet, when needed. The computer program product can be implemented using a computer program, i.e., software, or using one or more dedicated electronic circuits, i.e., hardware, or in any mixed form, i.e., using software components and hardware components, or a software, hardware, or a hybrid of software and hardware.
[0044] The above description is merely a preferred embodiment of this application. Those skilled in the art will understand that various changes or equivalent substitutions can be made to these features and embodiments without departing from the spirit and scope of this application. Furthermore, under the teachings of this application, these features and embodiments can be modified to adapt to specific situations and materials without departing from the spirit and scope of this application. Therefore, this application is not limited to the specific embodiments disclosed herein, and all embodiments falling within the scope of the claims of this application are within the protection scope of this application.
Claims
1. A message queue implementation method under the FreeRTOS system, characterized in that, The message queue implementation method under the FreeRTOS system is applied to sending messages from a source task to a target task, including: The source task determines whether there is still space in the message pool. If not, it expands the message pool using the realloc method and then adds the message to the message pool. The source task determines whether the target task is waiting for the message. If so, it wakes up the target task and then ends the sending process of the source task. If the target task waits without timeout, it is determined whether there is a message with the same ID as the target task in the message pool. If there is, the message is taken from the message pool. Then, it is determined whether the free space of the message pool has reached a preset threshold. If so, the free space of the message pool is released using the realloc method, and then the receiving process of the target task ends.
2. The message queue implementation method under the FreeRTOS system according to claim 1, characterized in that, The message queue implementation method under the FreeRTOS system further includes: constructing a message pool of a preset size, wherein the message pool is dynamically allocated memory, and all tasks share the same message pool.
3. The message queue implementation method under the FreeRTOS system according to claim 1, characterized in that, The message queue implementation method under the FreeRTOS system further includes: if there is still space in the message pool, the source task directly adds the message to the message pool.
4. The message queue implementation method under the FreeRTOS system according to claim 1, characterized in that, Adding the message to the message pool includes: the source task obtaining a free message block from the message pool and adding the message to the free message block.
5. The message queue implementation method under the FreeRTOS system according to claim 1, characterized in that, The message queue implementation method under the FreeRTOS system further includes: if the target task is not waiting for the message, then the sending process of the source task is directly terminated.
6. The message queue implementation method under the FreeRTOS system according to claim 1, characterized in that, The message queue implementation method under the FreeRTOS system further includes: if the target task times out, the receiving process of the target task is directly terminated.
7. The message queue implementation method under the FreeRTOS system according to claim 1, characterized in that, The message queue implementation method under the FreeRTOS system further includes: if there is no message with the same ID as the target task in the message pool, the target task enters a dormant state.
8. The message queue implementation method under the FreeRTOS system according to claim 1, characterized in that, The message queue implementation method under the FreeRTOS system further includes: if the free space of the message pool does not reach the preset threshold, the receiving process of the target task is directly terminated.
9. The message queue implementation method under the FreeRTOS system according to claim 1, characterized in that, The message includes the target task ID, message data, and message length.
10. A computer program product, characterized in that, The computer program product is stored on a data carrier and is designed to execute the message queue implementation method under the FreeRTOS system as described in any one of claims 1-9.