First-in first-out semaphore scheduling method based on POSIX interface
By adopting the first-in-first-out semaphore scheduling method based on the POSIX interface in an embedded real-time operating system, the thread hunger problem caused by the POSIX semaphore scheduling strategy is solved, and a more fair and efficient system resource management is achieved.
Patent Information
- Application Number
- CN202411927036.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-25
- Publication Date
- 2025-04-18
AI Technical Summary
In embedded real-time operating systems, POSIX semaphore scheduling strategies usually adopt priority queues, which may cause some threads to wait for a long time or starve, affecting the fairness and efficiency of the system.
A first-in-first-out semaphore scheduling method based on the POSIX interface is designed. By modifying the user space POSIX interface and the kernel space futex synchronization mechanism, the first-in-first-out scheduling strategy is adopted to ensure that threads are processed in the order of requests.
This method can avoid thread hunger, improve system fairness and predictability, and provide a smoother and responsive user experience.
Smart Images

Figure CN119847696A_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the technical field of software program development, and in particular relates to a first-in-first-out semaphore scheduling method based on a POSIX interface. Background Art
[0002] Multithreaded concurrency control is a key technology in modern software systems, used to coordinate multiple threads' access to shared resources to ensure data consistency and system stability. An efficient concurrency control mechanism can significantly improve system performance and resource utilization, while an inefficient mechanism may lead to deadlock, starvation and other problems, affecting system reliability and user experience.
[0003] In the field of multithreaded programming, POSIX semaphores are an important synchronization and mutual exclusion mechanism that is widely used in various operating systems, databases, and network servers. POSIX semaphores are divided into named semaphores and unnamed semaphores, providing basic operations such as initialization, acquisition, release, and destruction, providing developers with a standardized way of inter-thread communication. However, in embedded real-time operating systems, the scheduling strategy of POSIX semaphores usually adopts priority queues, which may cause some threads to wait for a long time or become hungry, affecting the fairness and efficiency of the system.
[0004] As a simple and fair scheduling method, the first-in-first-out (FIFO) scheduling strategy processes tasks in the order of request to ensure that each task can be responded to in a timely manner. FIFO scheduling has been widely used in operating systems, network protocol stacks and other fields. It can effectively avoid starvation problems and improve the fairness and predictability of the system.
[0005] In summary, the scheduling strategy of POSIX semaphores in embedded real-time operating systems has certain limitations, while the first-in-first-out scheduling strategy has significant advantages in fairness and predictability. Therefore, combining POSIX semaphores and first-in-first-out scheduling strategies to design an efficient and fair semaphore scheduling mechanism has important theoretical significance and practical application value. Summary of the invention
[0006] The present invention aims to provide a first-in-first-out semaphore scheduling method based on a POSIX interface to realize an efficient and fair semaphore scheduling mechanism.
[0007] In order to achieve the object of the present invention, the present invention provides a first-in-first-out semaphore scheduling method based on a POSIX interface, comprising the following steps:
[0008] Step 1: Modify the user space POSIX interface;
[0009] Step 1-1, define the semaphore strategy type identifier;
[0010] Step 1-2, modify the sem_open function;
[0011] Step 1-3, modify the do_futex_wait function, select the corresponding system call by reading the semaphore scheduling policy, and then enter the kernel state for processing;
[0012] Step 1-4: Modify the sem_init function and set the semaphore policy type.
[0013] Step 2: Modify the kernel space futex synchronization mechanism;
[0014] Step 2-1, add the corresponding flag bit of FUTEX_PRIVATE_FLAG for the FIFO strategy;
[0015] Step 2-2: Modify the system call do_futex function of the futex synchronization mechanism.
[0016] Compared with the prior art, the significant progress of the present invention lies in: 1) Improved scheduling strategy. The present invention adopts a first-in-first-out scheduling strategy. Compared with the traditional priority queue scheduling strategy, the first-in-first-out scheduling strategy can ensure that threads are processed in the order of request, thereby avoiding the starvation phenomenon that may occur in some threads and improving the fairness and predictability of the system; 2) POSIX interface compatibility. The present invention is designed based on the POSIX interface and maintains compatibility with existing POSIX semaphores; 3) Improved user experience. By reducing starvation and waiting time, the present invention can provide a smoother and more responsive user experience and improve user satisfaction.
[0017] In order to more clearly illustrate the functional characteristics and structural parameters of the present invention, further description is given below in conjunction with the accompanying drawings and specific implementation methods. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] The drawings described herein are used to provide a further understanding of the present invention and constitute a part of this application. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation of the present invention. In the drawings:
[0019] Figure 1 It is a schematic flow chart of the steps of implementing the present invention;
[0020] Figure 2 It is a flow chart of creating a semaphore in the dsem_open function of the present invention;
[0021] Figure 3 It is a schematic diagram of the do_futex_wait function flow of the present invention;
[0022] Figure 4It is the flow chart of futex_wait_fifo function of the present invention;
[0023] Figure 5 It is the flow chart of futex_wait_queue_me_fifo function of the present invention;
[0024] Figure 6 It is the _queue_me_fifo function flow chart of the present invention;
[0025] Figure 7 It is a screenshot of the test case main function and set_thread_prioity function of the present invention;
[0026] Figure 8 It is a screenshot of the test case thread_func function of the present invention;
[0027] Fig. 9 It is a screenshot of the test case execution result of the present invention. DETAILED DESCRIPTION
[0028] The technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only 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 ordinary technicians in this field without making creative work are within the scope of protection of the present invention.
[0029] The present invention is a POSIX interface based first-in-first-out semaphore scheduling method, combined with Figure 1 , including the following steps:
[0030] Step 1: Modify the user space POSIX interface to add support for the first-in-first-out scheduling strategy;
[0031] Step 1-1, define the semaphore strategy type identifier;
[0032] Step 1-1-1, in the semaphore structure, add a semaphore strategy type identification field, which is used to store the semaphore scheduling strategy;
[0033] Taking glibc2.28 as an example, modify the sysdeps / nptl / intemaltypes.h file and add the following code to the new_sem structure (the new code is in italics):
[0034]
[0035] Step 1-1-2, defining a constant for the value of the semaphore scheduling strategy type field added in step 1-1-1 to distinguish different semaphore scheduling strategies;
[0036] The values include PTHREAD_WAITQ_PRIO (priority policy) and
[0037] PTHREAD_WAITQ_FIFO (first-in, first-out strategy). Modify the sysdeps / nptl / intemaltypes.h file and add the following code:
[0038]
[0039] Step 1-2: Combination Figure 2 , modify the sem_open function to support setting the scheduling policy when creating a semaphore;
[0040] Step 1-2-1, define additional parameters, when the sem_open function creates a semaphore, introduce a semaphore policy type parameter, the value range of which should conform to the constant defined in step 1-1-2;
[0041] Step 1-2-2, obtain additional parameters, extract the semaphore strategy type from the passed in parameters, and store it in memory;
[0042] Modify the sem_open function and add the following code:
[0043] Define temporary variables:
[0044]
[0045] Extract the parameters and store them in temporary variables:
[0046]
[0047]
[0048] Step 1-2-3, check the additional parameters. If the semaphore policy type parameter is within the constant value range defined in step 1-1-2, continue the function execution; otherwise, set an error code and terminate the function execution;
[0049] Add the following code:
[0050]
[0051] Step 1-2-4, initialize the semaphore structure, set the semaphore strategy type according to the obtained parameters, and create the semaphore;
[0052] Add the following code:
[0053]
[0054]
[0055] Steps 1-3: Combination Figure 3 , modify the do_futex_wait function, select the corresponding system call by reading the semaphore scheduling policy, and then enter the kernel state for processing;
[0056] Step 1-3-1, add the corresponding flag bit of FUTEX_PRIVATE_FLAG for the first-in-first-out scheduling strategy;
[0057] The definition is as follows:
[0058] #define FUTEX_WAIT_BITSET_FIFO 33
[0059] Step 1-3-2, add the system call of the first-in-first-out scheduling strategy;
[0060] Modify the sysdeps / unix / sysv / linux / lowlevellock-futex.h file and add the following code:
[0061] Step 1-3-3, modify the do_futex_wait function, obtain the semaphore scheduling policy, and when the semaphore scheduling policy is FIFO, call the system call added in step 1-3-2;
[0062] The code is modified as follows:
[0063]
[0064]
[0065] The futex_abstimed_wait_fifo_cancelable function in the above code is implemented as follows:
[0066]
[0067]
[0068] Step 1-4, modify the sem_init function, set the semaphore policy type to prevent problems caused by unassigned variables; initialize the semaphore policy type identification field added in step 1-1-1 to the priority policy by default to prevent problems caused by unassigned variables;
[0069] Add the following code:
[0070]
[0071] Step 2: Modify the kernel space futex synchronization mechanism to add support for the first-in-first-out scheduling strategy.
[0072] Step 2-1, add the corresponding flag bit of FUTEX_PRIVATE_FLAG for the first-in-first-out scheduling strategy;
[0073] In this embodiment, taking Loongson linux-4.19-1oongson-7-rt-20230207 as an example, modify the include / uapi / linux / futex.h file and add the following code:
[0074] #ifdef FIFOSEM
[0075] #define FUTEX_WAIT_BITSET_FIFO 33
[0076] #endif
[0077] Step 2-2, modify the system call do_futex function of the futex synchronization mechanism;
[0078] Step 2-2-1, in the do_futex function, add a processing branch corresponding to the flag defined in step 2-1;
[0079] Modify the kemel / futex.c file and add the following code:
[0080]
[0081]
[0082] Step 2-2-2, completing the operation of the newly added processing branch in step 2-2-1 on the semaphore using the FIFO strategy;
[0083] In this embodiment, the futex_wait_fifo function is implemented, combined with Figure 4 , the code is implemented as follows:
[0084]
[0085]
[0086]
[0087] Combination Figure 5, the function futex_wait_queue_me_fifo is implemented as follows:
[0088]
[0089]
[0090] Unify the priority of each thread that applies for a semaphore, and then insert the threads into the waiting queue in the order of application. When the semaphore is available, wake up the thread that first enters the queue;
[0091] Combination Figure 6 , implement the _queue_me_fifo function in this embodiment, the code is implemented as follows:
[0092]
[0093]
[0094] In the process of constructing the target code, this embodiment requires defining the FIFOSEM macro in order to implement the function of the semaphore first-in-first-out scheduling policy.
[0095] Combination Figure 7 , Figure 8 , implement the test case of this embodiment. The running results are as follows Fig. 9 As shown in the figure, in the test case, 6 threads are created with priorities 10, 20, 30, 40, 50, and 60 respectively. They apply for 1 semaphore at the same time. Their entry and exit order is regardless of priority. They enter and leave the critical section in the order of the wait function, thus achieving a first-in-first-out effect.
[0096] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device.
[0097] Although embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions and variations may be made to the embodiments without departing from the principles and spirit of the present invention, and that the scope of the present invention is defined by the appended claims and their equivalents.
Claims
1. A first-in-first-out semaphore scheduling method based on a POSIX interface, characterized in that: The following steps are involved: Step 1: Modify the user space POSIX interface; Step 2: Modify the kernel space futex synchronization mechanism.
2. A POSIX interface-based first-in-first-out semaphore scheduling method according to claim 1, characterized in that: The step 1 specifically comprises the following steps: Step 1-1, define the semaphore strategy type identifier; Step 1-2, modify the sem_open function; Step 1-3, modify the do_futex_wait function, select the corresponding system call by reading the semaphore scheduling policy, and then enter the kernel state for processing; Step 1-4: Modify the sem_init function and set the semaphore policy type.
3. A POSIX interface-based first-in-first-out semaphore scheduling method according to claim 2, characterized in that: The step 1-1 specifically includes the following steps: Step 1-1-1, in the semaphore structure, add a semaphore strategy type identification field, which is used to store the semaphore scheduling strategy; Step 1-1-2, define the value of the semaphore scheduling policy type field added in step 1-1-1 as a constant.
4. A POSIX interface-based first-in-first-out semaphore scheduling method according to claim 3, characterized in that: The steps 1-2 specifically include the following steps: Step 1-2-1, define additional parameters, when the sem_open function creates a semaphore, introduce a semaphore policy type parameter, the value range of which should conform to the constant defined in step 1-1-2; Step 1-2-2, when creating a semaphore, obtain additional parameters, extract the semaphore policy type from the passed in parameters, and store it in memory; Step 1-2-3, check the additional parameters. If the semaphore policy type parameter is within the constant value range defined in step 1-1-2, continue the function execution; otherwise, an error is triggered and the function execution is terminated; Step 1-2-4: Initialize the semaphore structure, set the semaphore policy type according to the obtained parameters, and create the semaphore.
5. A POSIX interface-based first-in-first-out semaphore scheduling method according to claim 2, characterized in that: The steps 1-3 specifically include the following steps: Step 1-3-1, add the corresponding flag bit of FUTEX_PRIVATE_FLAG for the first-in-first-out strategy; Step 1-3-2, add a system call when the semaphore policy is first-in-first-out; Step 1-3-3, modify the do_futex_wait function, obtain the semaphore scheduling policy, and when the semaphore scheduling policy is first-in-first-out, call the system call added in step 1-3-2.
6. A POSIX interface-based first-in-first-out semaphore scheduling method according to claim 2, characterized in that: The step 1-4 is specifically as follows: the semaphore policy type identification field newly added in the step 1-1-1 is initialized to the priority policy by default.
7. A POSIX interface-based first-in-first-out semaphore scheduling method according to claim 1, characterized in that: The step 2 specifically includes the following steps: Step 2-1, add the corresponding flag bit of FUTEX_PRIVATE_FLAG for the FIFO strategy; Step 2-2: Modify the system call do_futex function of the futex synchronization mechanism.
8. A POSIX interface-based first-in-first-out semaphore scheduling method according to claim 7, characterized in that: The step 2-2 specifically includes the following steps: Step 2-2-1, in the do_futex function, add a processing branch corresponding to the flag defined in step 2-1; Step 2-2-2, complete the operation of the newly added processing branch in step 2-2-1 on the semaphore using the first-in-first-out strategy.
9. The method for designing a POSIX interface-based first-in-first-out semaphore scheduling according to claim 8, characterized in that: The step 2-2-2 is specifically as follows: unifying the priorities of threads applying for semaphores, and then inserting the threads into the waiting queue in the order of application, and when the semaphore is available, waking up the thread that first enters the queue.
10. A computer program product comprising computer program instructions, characterized in that When the computer program instructions are executed on a computer, the computer is caused to perform the method according to any one of claims 1 to 9.
Citation Information
Cited By
Method and device for running Lua plug-in under RTEMS system
CN121116421A
Method and device for running Lua plug-in under RTEMS system
CN121116421B