Scheduling method of task waiting queue of message queue under Linux system

By introducing the task waiting queue scheduling policy attributes in the message queue attribute structure in the Linux system, it supports priority and first-in-first-out scheduling policies, the problem of single task waiting queue scheduling strategies in traditional Linux systems is solved, and programming flexibility and application scenarios are improved, and development costs are reduced.

CN120066699APending Publication Date: 2025-05-30JIANGSU AUTOMATION RESEARCH INSTITUTE
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202411969846.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-30
Publication Date
2025-05-30

AI Technical Summary

Technical Problem

In traditional Linux systems, the task waiting queue scheduling strategy of message queues is single, which is difficult to meet the needs of diversified system POSIX interface functions and flexible program development.

Method used

Introduce the task waiting queue scheduling policy attribute in the message queue attribute structure in the Linux system, and supports priority scheduling policies and first-in-first-out scheduling policies. This attribute determines the scheduling method of the task waiting queue.

Benefits of technology

It enriches the POSIX interface functions, improves programming flexibility, provides a wider range of application scenarios, and reduces labor and time costs during project development.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120066699A_ABST
    Figure CN120066699A_ABST
Patent Text Reader

Abstract

The invention discloses a scheduling method for a task waiting queue of a message queue under a Linux system. Judging in a kernel, if a specified macro is defined, using the method provided by the invention, and adding the attribute of a task waiting queue scheduling strategy in a message queue attribute structural body; creating a message queue according to a message queue attribute specified by the application program; after the message sending task is blocked, scheduling a message sending task waiting queue according to a task waiting queue scheduling strategy; after the message receiving task is blocked, scheduling a message receiving task waiting queue according to a task waiting queue scheduling strategy; and judging whether at most one task exists in each of the message sending task waiting queue and the message receiving task waiting queue, and if yes, allowing to set a task waiting queue scheduling strategy of the message queue. According to the invention, the problem that the scheduling strategy of the task waiting queue in the message queue is single is solved, the functions of the POSIX interface of the system are enriched, and the programming flexibility is improved.
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 particularly relates to a scheduling method for a task waiting queue of a message queue under a Linux system. Background Art

[0002] The message queue under the Linux system is a communication mechanism between processes. The message queue is a set of data organized in a linked list structure, maintained and stored by the kernel, and the specified data is indexed by the message type. Information can be passed between each calling task through the message queue. For example, when task A and task B pass message M through the message queue, task A can first send the message to message queue L, and then task B receives message M from message queue L.

[0003] Taking the process of calling task p_Send to send a message to message queue mq as an example to elaborate on the process of a task sending a message, as Figure 1 shown, if the message queue space is available, the system will insert the message into the specified message queue; if the message queue is full and the O_NONBLOCK flag is not set in the message queue descriptor, in the traditional Linux system, the system will insert the calling task into the message sending task waiting queue in a priority sorting manner, and the calling task enters the blocked state until the message queue space is available or the call is interrupted by a signal; among them, the tasks in the message sending task waiting queue are arranged in descending order of task priority; if the message queue is full and the O_NONBLOCK flag is set in the message queue descriptor, the calling task will not be blocked and directly return an error.

[0004] Taking the process of calling task p_Recv to receive a message from message queue mq as an example to elaborate on the process of a task receiving a message, as Figure 2 shown, if the message queue is not empty and the specified message buffer is sufficient for use, the calling task will receive the earliest message with the highest priority from the message queue; if the message queue is empty and the O_NONBLOCK flag is not set in the message queue descriptor, in the traditional Linux system, the system will insert the calling task into the message receiving task waiting queue in a priority sorting manner, and the calling task enters the blocked state until a message is added to the queue or the call is interrupted by a signal; among them, the tasks in the message receiving task waiting queue are arranged in descending order of task priority; if the message queue is empty and the O_NONBLOCK flag is set in the message queue descriptor, the calling task will not be blocked and directly return an error.

[0005] From the above, we can see that in the traditional Linux system environment, during the message queue call process, the system uses a priority scheduling strategy to schedule the task waiting queue, and the way to relieve task blocking is relatively simple. With the development of computer software application technology, during the message queue task waiting queue call process, the single priority scheduling strategy has been difficult to meet the needs of system POSIX interface function diversification and program development flexibility. Summary of the invention

[0006] The present invention aims to solve the problem raised in the background technology and proposes a scheduling method for a task waiting queue of a message queue under a Linux system.

[0007] In order to achieve the purpose of the present invention, the present invention discloses a method for scheduling a task waiting queue of a message queue in a Linux system, comprising the following steps:

[0008] Step 1: Initialization process;

[0009] Step 2: Create a message queue process;

[0010] Step 3: Message sending task waiting queue scheduling process;

[0011] Step 4: Message receiving task waiting queue scheduling process;

[0012] Step 5: Set the message queue properties process.

[0013] Furthermore, in step 1, it is determined in the kernel whether a specified macro is defined; if the specified macro is not defined, the priority scheduling method under the traditional Linux system is used to schedule the task waiting queue of the message queue; if the specified macro is defined, the task waiting queue scheduling policy attribute is added to the message queue attribute structure, and the task waiting queue scheduling policy attribute takes values ​​of 0 and 1, respectively representing the priority scheduling policy and the first-in-first-out scheduling policy.

[0014] Furthermore, in step 2, a message queue is created according to the message queue attributes specified by the application.

[0015] Further, in step 3, when the judgment result is a priority scheduling strategy, the message sending task waiting queue priority scheduling process is executed; when the judgment result is a first-in-first-out scheduling strategy, the message sending task waiting queue first-in-first-out scheduling process is executed.

[0016] Further, the priority scheduling process of the message sending task waiting queue is as follows: The message sending tasks are inserted into the message sending task waiting queue in the order of priority sorting, so that the tasks in the message sending task waiting queue are arranged in descending order of task priority; wait for the signal of message retrieval from the message queue; if the message queue space is available and the message sending task waiting queue is not empty, then take out the first task in the message sending task waiting queue, that is, the task with the highest priority, unblock the task, and insert the message sent by the task into the message queue.

[0017] Further, the first-in, first-out scheduling process of the message sending task waiting queue is as follows: The message sending tasks are directly added to the tail of the message sending task waiting queue, so that the tasks in the message sending task waiting queue are arranged in the order of task arrival from first to last; wait for the signal of message retrieval from the message queue; if the message queue space is available and the message sending task waiting queue is not empty, then take out the first task in the message sending task waiting queue, that is, the task that arrived first, unblock the task, and insert the message sent by the task into the message queue.

[0018] Further, in step 4, when the judgment result is the priority scheduling policy, execute the priority scheduling process of the message receiving task waiting queue; when the judgment result is the first-in, first-out scheduling policy, execute the first-in, first-out scheduling process of the message receiving task waiting queue.

[0019] Further, the priority scheduling process of the message receiving task waiting queue is as follows: The message receiving tasks are inserted into the message receiving task waiting queue in the order of priority sorting, so that the tasks in the message receiving task waiting queue are arranged in descending order of task priority; wait for the signal of the message arriving at the message queue; if the message queue is not empty and the message receiving task waiting queue is not empty, then take out the first task in the message receiving task waiting queue, that is, the task with the highest priority, unblock the task, and make the task receive the message in the message queue.

[0020] Further, the first-in, first-out scheduling process of the message receiving task waiting queue is as follows: The message receiving tasks are directly added to the tail of the message receiving task waiting queue, so that the tasks in the message receiving task waiting queue are arranged in the order of task arrival from first to last; wait for the signal of the message arriving at the message queue; if the message queue is not empty and the message receiving task waiting queue is not empty, then take out the first task in the message receiving task waiting queue, that is, the task that arrived first, unblock the task, and make the task receive the message in the message queue.

[0021] Further, in step 5, determine the number of tasks in the message sending task waiting queue and the message receiving task waiting queue of the message queue; if there is at most one task in the message sending task waiting queue and at most one task in the message receiving task waiting queue, set the task waiting queue scheduling policy attribute of the message queue according to the parameters of the application program; otherwise, the setting of the task waiting queue scheduling policy attribute of the message queue fails.

[0022] Compared with the prior art, the significant progress of the present invention lies in that: a scheduling method for the task waiting queue of the message queue under the Linux system provided by the present invention enriches the POSIX interface function; programmers can flexibly select the priority scheduling policy or the first-in-first-out scheduling policy to schedule the task waiting queue of the message queue according to their needs, improving the programming flexibility and having a wider application scenario. At the same time, in the application scenario of message queue invocation, applying the method provided by the present invention can provide more choices for programmers to schedule the task waiting queue of the message queue, reducing the labor cost and time cost in the project development process and bringing significant economic benefits.

[0023] To more clearly illustrate the functional characteristics and structural parameters of the present invention, the following further explains in conjunction with the drawings and specific embodiments. BRIEF DESCRIPTION OF THE DRAWINGS

[0024] The drawings described herein are used to provide a further understanding of the present invention and form a part of this application. The schematic embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation to the present invention. In the drawings:

[0025] Figure 1 is a schematic diagram of the task sending message process under the traditional Linux system;

[0026] Figure 2 is a schematic diagram of the task receiving message process under the traditional Linux system;

[0027] Figure 3 is a schematic diagram of the working process of a scheduling method for the task waiting queue of the message queue under a Linux system;

[0028] Figure 4 is a schematic diagram of the initialization process;

[0029] Figure 5 is a schematic diagram of the process of creating a message queue;

[0030] Figure 6 is a schematic diagram of the scheduling process of the message sending task waiting queue;

[0031] Figure 7 is a schematic diagram of the scheduling process of the message receiving task waiting queue;

[0032] Figure 8 It is a schematic diagram of the process for setting message queue attributes. Specific implementation manners

[0033] Next, the technical solutions in the embodiments of the present invention will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments; based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present invention.

[0034] The present invention discloses a scheduling method for a task waiting queue of a message queue under a Linux system. The specific implementation manners are as follows:

[0035] Step 1, initialization process;

[0036] Step 2, message queue creation process;

[0037] Step 3, message sending task waiting queue scheduling process;

[0038] Step 4, message receiving task waiting queue scheduling process;

[0039] Step 5, message queue attribute setting process.

[0040] Specifically, in Step 1, it is judged in the kernel whether a specified macro is defined; if the specified macro is not defined, the priority scheduling method under the traditional Linux system is used to schedule the task waiting queue of the message queue; if the specified macro is defined, a task waiting queue scheduling policy attribute is added to the message queue attribute structure, and the values 0 and 1 of the task waiting queue scheduling policy attribute respectively represent the priority scheduling policy and the first-in first-out scheduling policy.

[0041] Specifically, in Step 2, a message queue is created according to the message queue attributes specified by the application program.

[0042] Specifically, in Step 3, when the judgment result is the priority scheduling policy, the priority scheduling process of the message sending task waiting queue is executed; when the judgment result is the first-in first-out scheduling policy, the first-in first-out scheduling process of the message sending task waiting queue is executed.

[0043] Specifically, the priority scheduling process of the message sending task waiting queue is as follows: Insert the message sending tasks into the message sending task waiting queue in the order of priority sorting, so that the tasks in the message sending task waiting queue are arranged in descending order of task priority; Wait for the signal to retrieve messages from the message queue; If the message queue space is available and the message sending task waiting queue is not empty, then retrieve the first task in the message sending task waiting queue, that is, the task with the highest priority, unblock the task, and insert the message sent by the task into the message queue.

[0044] Specifically, the first-in, first-out scheduling process of the message sending task waiting queue is as follows: Directly add the message sending tasks to the tail of the message sending task waiting queue, so that the tasks in the message sending task waiting queue are arranged in the order of task arrival from first to last; Wait for the signal to retrieve messages from the message queue; If the message queue space is available and the message sending task waiting queue is not empty, then retrieve the first task in the message sending task waiting queue, that is, the task that arrived first, unblock the task, and insert the message sent by the task into the message queue.

[0045] Specifically, in step 4, when the judgment result is the priority scheduling policy, execute the priority scheduling process of the message receiving task waiting queue; when the judgment result is the first-in, first-out scheduling policy, execute the first-in, first-out scheduling process of the message receiving task waiting queue.

[0046] Specifically, the priority scheduling process of the message receiving task waiting queue is as follows: Insert the message receiving tasks into the message receiving task waiting queue in the order of priority sorting, so that the tasks in the message receiving task waiting queue are arranged in descending order of task priority; Wait for the signal that messages arrive at the message queue; If the message queue is not empty and the message receiving task waiting queue is not empty, then retrieve the first task in the message receiving task waiting queue, that is, the task with the highest priority, unblock the task, and enable the task to receive the messages in the message queue.

[0047] Specifically, the first-in, first-out scheduling process of the message receiving task waiting queue is as follows: Directly add the message receiving tasks to the tail of the message receiving task waiting queue, so that the tasks in the message receiving task waiting queue are arranged in the order of task arrival from first to last; Wait for the signal that messages arrive at the message queue; If the message queue is not empty and the message receiving task waiting queue is not empty, then retrieve the first task in the message receiving task waiting queue, that is, the task that arrived first, unblock the task, and enable the task to receive the messages in the message queue.

[0048] Specifically, in step 5, the number of tasks in the message sending task waiting queue and the message receiving task waiting queue of the message queue is judged; if there is at most one task in the message sending task waiting queue and at most one task in the message receiving task waiting queue, the task waiting queue scheduling policy attribute of the message queue is set according to the parameters of the application program; otherwise, the setting of the task waiting queue scheduling policy attribute of the message queue fails.

[0049] Embodiment

[0050] In one embodiment, a scheduling method for the task waiting queue of a message queue under a Linux system includes an initialization process, a message queue creation process, a message sending task waiting queue scheduling process, a message receiving task waiting queue scheduling process, and a message queue attribute setting process. Among them, the message sending task waiting queue scheduling process, the message receiving task waiting queue scheduling process, and the message queue attribute setting process have no dependency relationship and can be executed independently. The method provided in this embodiment includes the following specific steps:

[0051] Step 1, as Figure 3 shown, when an application program calls the message queue, the initialization process is first executed. The specific steps of the initialization process are as Figure 4 shown. First, it is judged in the kernel whether a specified macro is defined. If the specified macro is not defined, the message queue attribute structure body is not modified, and the traditional priority scheduling policy under the Linux system is used to schedule the task waiting queue of the message queue; if the specified macro is defined, the process of the present invention is continued, and a task waiting queue scheduling policy attribute is added to the message queue attribute structure body. When the value of this attribute is 0, it represents the priority scheduling policy; when the value of this attribute is 1, it represents the first-in, first-out scheduling policy;

[0052] Step 2, as Figure 5 shown, a message queue mq is created according to the message queue attributes specified by the application program;

[0053] Step 3, as Figure 3 shown, the application program can choose to perform operations such as sending messages, receiving messages, and modifying message queue attributes according to requirements. After the application program sends messages, receives messages, returns successfully or returns an error, or modifies the message queue attributes, without closing or deleting the message queue, the application program can continue to choose to perform operations such as sending messages, receiving messages, or modifying message attributes; when the application program sends messages, if the message sending task p_Send does not return successfully and does not return an error, it will enter the blocked state and execute step 4; when the message receiving task p_Recv does not return successfully and does not return an error, it will enter the blocked state and execute step 11; when the application program modifies the message queue attributes, step 18 is executed; if the message queue mq is closed or deleted, step 20 is executed;

[0054] Step 4, execute the message sending task waiting queue scheduling process. As Figure 6 shown, judge the scheduling policy of the message sending task waiting queue wq_Send. If the scheduling policy of wq_Send is the priority scheduling policy, then execute Step 5; otherwise, execute Step 8;

[0055] Step 5, as Figure 6 shown, the message sending task waiting queue priority scheduling process inserts p_Send into the queue wq_Send in the order of priority sorting. At this time, the tasks in wq_Send are arranged in the order of task priority from high to low; execute Step 6;

[0056] Step 6, as Figure 6 shown, wait for the message retrieval signal in mq. If the message in mq is retrieved and there is available space in mq, then execute Step 7;

[0057] Step 7, as Figure 6 shown, retrieve the first task (the task with the highest priority) in wq_Send, unblock the task, and insert the message sent by the task into the message queue mq;

[0058] Step 8, as Figure 6 shown, the message sending task waiting queue first-in-first-out scheduling process directly adds p_Send to the tail of the queue wq_Send. At this time, the tasks in wq_Send are arranged in the order of task arrival from first to last; execute Step 10;

[0059] Step 9, as Figure 6 shown, wait for the signal of message retrieval in mq. If the message in mq is retrieved and there is available space in mq, then execute Step 10;

[0060] Step 10, as Figure 6 shown, retrieve the first task (the task that arrived first) in wq_Send, unblock the task, and insert the message sent by the task into the message queue mq;

[0061] Step 11, execute the message receiving task waiting queue scheduling process. As Figure 7 shown, judge the scheduling policy of the message receiving task waiting queue wq_Recv. If the scheduling policy of wq_Recv is the priority scheduling policy, then execute Step 12; otherwise, execute Step 15;

[0062] Step 12, as Figure 7As shown in the figure, for the priority scheduling process of the message reception task waiting queue, p_Recv is inserted into the queue wq_Recv in the order of priority. At this time, the tasks in wq_Recv are arranged in descending order of task priority; step 13 is executed;

[0063] Step 13, as Figure 7 shown in the figure, wait for the signal that a message arrives at the message queue mq. If a message arrives at mq and mq is not empty, then step 14 is executed;

[0064] Step 14, as Figure 7 shown in the figure, take out the first task (the task with the highest priority) in wq_Recv, unblock the task, and make the task receive the message in the message queue mq;

[0065] Step 15, as Figure 7 shown in the figure, for the first-in, first-out scheduling process of the message reception task waiting queue, directly add p_Recv to the tail of the queue wq_Recv. At this time, the tasks in wq_Recv are arranged in the order of task arrival from first to last; step 16 is executed;

[0066] Step 16, as Figure 7 shown in the figure, wait for the signal that a message arrives at the message queue mq. If a message arrives at mq and mq is not empty, step 17 is executed;

[0067] Step 17, as Figure 7 shown in the figure, take out the first task (the task that arrived first) in wq_Recv, unblock the task, and make the task receive the message in the message queue mq;

[0068] Step 18, as Figure 8 shown in the figure, for the process of setting the message queue attributes, judge the number of tasks in the message sending task waiting queue wq_Send and the message reception task waiting queue wq_Recv of mq. If there is at most one task in wq_Send and at most one task in wq_Recv, then set the scheduling policy attributes of the task waiting queue of the message queue according to the parameters of the application program, and then execute step 3; otherwise execute step 19;

[0069] Step 19, as Figure 8 shown in the figure, the modification of the scheduling policy attributes of the task waiting queue of the message queue fails, and step 3 is executed;

[0070] Step 20, as Figure 3 shown in the figure, end all processes.

[0071] Thus, in the Linux system environment, by applying the method provided by the present invention, users can select to use the priority scheduling policy or the first-in-first-out scheduling policy according to their needs to schedule the task waiting queue of the message queue, enriching the POSIX interface function and improving the programming flexibility.

[0072] It should be noted that in this article, relational terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the term "comprising", "including" or any other variant thereof is intended to cover non-exclusive inclusion, so that a process, method, article or device comprising a series of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article or device.

[0073] Although the embodiments of the present invention have been shown and described, for those of ordinary skill in the art, it can be understood that various changes, modifications, substitutions and variations can be made to these embodiments without departing from the principle and spirit of the present invention. The scope of the present invention is defined by the appended claims and their equivalents.

Claims

1. A method for scheduling a task waiting queue of a message queue in a Linux system, characterized in that: The following steps are involved: Step 1, initialization process; Step 2: Create a message queue process; Step 3: Message sending task waiting queue scheduling process; Step 4: Message receiving task waiting queue scheduling process; Step 5: Set the message queue property process.

2. The method for scheduling a task waiting queue of a message queue under a Linux system according to claim 1, characterized in that: In step 1, it is determined in the kernel whether the specified macro is defined; if the specified macro is not defined, the priority scheduling method under the traditional Linux system is used to schedule the task waiting queue of the message queue; if the specified macro is defined, the task waiting queue scheduling policy attribute is added to the message queue attribute structure, and the task waiting queue scheduling policy attribute takes values ​​of 0 and 1, respectively representing the priority scheduling policy and the first-in-first-out scheduling policy.

3. The method for scheduling a task waiting queue of a message queue under a Linux system according to claim 1, characterized in that: In step 2, a message queue is created according to the message queue properties specified by the application.

4. The method for scheduling a task waiting queue of a message queue under a Linux system according to claim 1, characterized in that: In step 3, when the judgment result is a priority scheduling strategy, the message sending task waiting queue priority scheduling process is executed; when the judgment result is a first-in-first-out scheduling strategy, the message sending task waiting queue first-in-first-out scheduling process is executed.

5. The method for scheduling a task waiting queue of a message queue in a Linux system according to claim 4, characterized in that: The priority scheduling process of the message sending task waiting queue is as follows: inserting the message sending task into the message sending task waiting queue in a priority sorting manner, so that the tasks in the message sending task waiting queue are arranged in descending order of task priority; waiting for the signal of taking out the message in the message queue; if the message queue space is available and the message sending task waiting queue is not empty, then taking out the first task in the message sending task waiting queue, that is, the task with the highest priority, unblocking the task, and inserting the message sent by the task into the message queue.

6. The method for scheduling task waiting queues of message queues in a Linux system according to claim 4, characterized in that: The first-in-first-out scheduling process of the message sending task waiting queue is as follows: directly add the message sending task to the tail of the message sending task waiting queue, so that the tasks in the message sending task waiting queue are arranged in the order of task arrival from first to last; wait for the signal of taking out the message in the message queue; if the message queue space is available and the message sending task waiting queue is not empty, take out the first task in the message sending task waiting queue, that is, the task that arrives first, unblock the task, and insert the message sent by the task into the message queue.

7. The method for scheduling task waiting queues of message queues in a Linux system according to claim 1, characterized in that: In step 4, when the judgment result is a priority scheduling strategy, the message receiving task waiting queue priority scheduling process is executed; when the judgment result is a first-in-first-out scheduling strategy, the message receiving task waiting queue first-in-first-out scheduling process is executed.

8. The method for scheduling task waiting queues of message queues in Linux system according to claim 7, characterized in that: The message receiving task waiting queue priority scheduling process is as follows: inserting the message receiving task into the message receiving task waiting queue in a priority sorting manner, so that the tasks in the message receiving task waiting queue are arranged in descending order of task priority; Wait for the signal that the message arrives at the message queue; if the message queue is not empty and the message receiving task waiting queue is not empty, take out the first task in the message receiving task waiting queue, that is, the task with the highest priority, unblock the task, and enable the task to receive the message in the message queue.

9. The method for scheduling task waiting queues of message queues in a Linux system according to claim 7, characterized in that: The first-in-first-out scheduling process of the message receiving task waiting queue is as follows: directly adding the message receiving task to the tail of the message receiving task waiting queue, so that the tasks in the message receiving task waiting queue are arranged in the order of the task arrival from the first to the last; Wait for the signal that the message arrives at the message queue; if the message queue is not empty and the message receiving task waiting queue is not empty, then take out the first task in the message receiving task waiting queue, that is, the task that arrives first, unblock the task, and enable the task to receive the message in the message queue.

10. The method for scheduling task waiting queues of message queues in a Linux system according to claim 1, characterized in that: In step 5, determine the number of tasks in the message sending task waiting queue and the message receiving task waiting queue of the message queue; if there is at most one task in the message sending task waiting queue and at most one task in the message receiving task waiting queue, set the task waiting queue scheduling policy attributes of the message queue according to the application parameters; otherwise, fail to set the task waiting queue scheduling policy attributes of the message queue.

Citation Information

Cited By

  • Message processing method and electronic equipment

    CN121050905A