Binary semaphore construction method based on POSIX interface
By adding semaphore type identification fields in the POSIX interface and modifying related functions, the creation and operation of binary semaphores are realized, which solves the problems of low efficiency and complex programming in the existing technology, simplifies the synchronization mechanism and improves performance.
Patent Information
- Application Number
- CN202411934685.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-25
- Publication Date
- 2025-04-25
AI Technical Summary
The existing POSIX standards do not directly provide the implementation of binary semaphores, resulting in low efficiency and complex programming in scenarios where high performance or specific synchronization functions are required.
By adding semaphore type identification fields to the semaphore structure and modifying sem_open, sem_post and sem_init functions, the creation and operation of binary semaphores are supported to ensure that the semaphore value is always 0 or 1.
Simplifies synchronization mechanisms, provides simpler mutex and resource access control mechanisms, reduces programming complexity, and improves performance in frequent locking and unlocking operations.
Smart Images

Figure CN119883666A_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the technical field of software program development, and in particular relates to a binary semaphore construction method based on a POSIX interface. Background Art
[0002] In modern operating systems, multithreaded programming is one of the key means to improve program performance and response speed. In order to effectively coordinate the execution and synchronization of multiple threads, semaphores, as a classic synchronization tool, have been widely used in multithreaded programming. Among the many types of semaphores, binary semaphores are the most basic form, which only contain two states, 0 and 1, and are usually used to implement the function of mutual exclusion locks.
[0003] POSIX (Portable Operating System Interface) is a set of standard interfaces designed to ensure compatibility of applications across Unix-like systems. The POSIX interface defines a variety of synchronization mechanisms, including semaphores. However, the existing POSIX standard does not directly provide an implementation of binary semaphores. Although the behavior of binary semaphores can be simulated through mechanisms such as mutex locks and conditional variables, there are still some limitations, such as low efficiency and complex programming. Therefore, in scenarios that require high performance or specific synchronization functions, using the specialized binary semaphore interface provided by the operating system can solve technical problems more specifically. Summary of the invention
[0004] The present invention aims to provide a binary semaphore construction method based on POSIX interface, so as to improve efficiency and reduce programming complexity.
[0005] In order to achieve the purpose of the present invention, the present invention provides a method for constructing a binary semaphore based on a POSIX interface, comprising the following steps:
[0006] Step 1, define the semaphore type identifier;
[0007] Step 1-1: Add a semaphore type identification field to the semaphore structure. This field is used to clearly distinguish different types of semaphores.
[0008] Step 1-2: define the semaphore type field newly added in step 1-1 as a constant to distinguish different semaphore categories.
[0009] Step 2: Modify the sem_open function to support the creation of binary semaphores;
[0010] Step 2-1, define additional parameters. When the sem_open function creates a semaphore, two additional parameters are introduced: a semaphore type parameter and a semaphore initial value parameter. The value range of the semaphore type parameter conforms to the semaphore type defined in step 1-2, and the initial value of the binary semaphore is limited to 0 or 1.
[0011] Step 2-2, when further creating a semaphore, obtain additional parameters, extract the semaphore type and initial value from the passed in parameters, and store them in memory;
[0012] Step 2-3, check the additional parameters. If the type parameter is not within the constant value range defined in step 1-2, an error is triggered and the function execution is terminated. If the semaphore type parameter is a binary parameter type and the semaphore initial value parameter value is not 0 or 1, the semaphore initial value is set to 1.
[0013] Step 2-4: Initialize the semaphore structure, set the semaphore type and initial value according to the obtained parameters, and create the semaphore.
[0014] Step 3: Modify the sem_post function to ensure that the binary semaphore value does not exceed 1;
[0015] Step 3-1, identify the semaphore type, and extract its type information from the target semaphore structure; if the extracted semaphore type is a binary semaphore, execute step 3-2; if the semaphore type is not a binary semaphore, terminate the function execution;
[0016] Step 3-2: If the semaphore type extracted in step 3-1 is a binary semaphore, check whether the value of the semaphore is 1; if it is 1, update the structure data; if not, set an error code.
[0017] Step 4, modify the sem_init function to set the semaphore type; set the semaphore type identification field newly added in step 1-1 to the counting semaphore, so as to realize the function of default initialization to the counting semaphore and prevent the problem caused by the variable not being assigned a value.
[0018] Compared with the prior art, the significant progress of the present invention is: 1) The present invention simplifies the synchronization mechanism. The binary semaphore provides a simpler mechanism to implement mutual exclusion locks and resource access control, reducing the reliance on complex lock mechanisms; 2) The present invention simplifies the programming interface. For scenarios requiring mutual exclusion, adding binary semaphores can provide a simpler programming interface for mutual exclusion operations, making it easier for developers to understand and use them correctly; 3) In the present invention, binary semaphores provide higher performance than existing counting semaphores in frequent locking and unlocking operations.
[0019] 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
[0020] 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:
[0021] Figure 1 is a flow chart of the steps of the present invention;
[0022] Figure 2 It is a flow chart of defining semaphore type identification of the present invention;
[0023] Figure 3 It is a flow chart of creating a semaphore in the sem_open function of the present invention;
[0024] Figure 4 It is the sem_post function flow chart of the present invention;
[0025] Figure 5 This is a screenshot of the test case main function of the present invention;
[0026] Figure 6 It is a screenshot of the test case thread_func function of the present invention;
[0027] Figure 7 It is a screenshot of the direct result of the test case 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] A binary semaphore construction method based on a POSIX interface of the present invention is combined with Figure 1 , including the following steps:
[0030] Step 1: Combine Figure 2 , define the semaphore type identifier;
[0031] Step 1-1: Add a semaphore type identification field to the semaphore structure. This field is used to clearly distinguish different types of semaphores.
[0032] Taking glibc2.28 as an example, modify the sysdeps / nptl / internaltypes.h file and add the following code to the new_sem structure (the new code is in italics):
[0033]
[0034]
[0035] Step 1-2, defining a constant for the semaphore type field added in step 1-1, so as to distinguish different semaphore categories;
[0036] The values include SEM_BINRARY (binary semaphore) and SEM_COUNTING (counting semaphore). Modify the sysdeps / nptl / internaltypes.h file and add the following code:
[0037]
[0038] Step 2: Combine Figure 3 , modify the sem_open function to support the creation of binary semaphores; step 2-1, define additional parameters, when the sem_open function creates a semaphore, introduce two additional parameters: a semaphore type parameter and a semaphore initial value parameter; the semaphore type parameter value range conforms to the semaphore type defined in step 1-2, and the initial value of the binary semaphore is limited to 0 or 1;
[0039] Step 2-2, when further creating a semaphore, obtain additional parameters, extract the semaphore type and initial value from the passed in parameters, and store them in memory;
[0040] Modify the sem_open function and add the following code:
[0041] Define temporary variables:
[0042]
[0043] Extract the parameters and store them in temporary variables:
[0044]
[0045]
[0046] Step 2-3, check the additional parameters. If the semaphore type parameter is not within the constant value range defined in step 1-2, set an error code and terminate the function execution. If the condition is met, continue to execute the function. If the semaphore type parameter is a binary parameter type and the semaphore initial value parameter value is not 0 or 1, set the semaphore initial value to 1. Otherwise, set an error code and terminate the function execution.
[0047] Add the following code:
[0048]
[0049] Step 2-4, initialize the semaphore structure, set the semaphore type and initial value according to the obtained parameters, and create the semaphore;
[0050] Add the following code:
[0051]
[0052] Step 3: Combine Figure 4 , modify the sem_post function to ensure that the binary semaphore value does not exceed 1;
[0053] Step 3-1, identify the semaphore type, extract its type information from the target semaphore structure, if the extracted semaphore type is a binary semaphore, execute step 3-2, if the semaphore type is not a binary semaphore, terminate the function execution;
[0054] Step 3-2, check whether the value of the semaphore is 1, if it is 1, update the structure data, if not 1, set the error code EOVERFLOW to indicate operation overflow, and end the function execution;
[0055] The specific modified code is as follows:
[0056]
[0057]
[0058] Step 4: Modify the sem_init function to set the semaphore type;
[0059] The semaphore type identification field newly added in step 1-1 is set to a counting semaphore, so as to realize the function of default initialization to a counting semaphore and prevent problems caused by unassigned variables;
[0060] , add the following code:
[0061]
[0062] In the process of constructing the target code, this embodiment requires defining the BSEM macro in order to implement the binary semaphore function.
[0063] Combination Figure 5 , Figure 6 , implement the test case of this embodiment. The running results are as follows Figure 7 As shown, in the test case, three threads are created, and they apply for binary semaphores at the same time. Their entry and exit order is that when one of the threads leaves the critical section, the remaining threads can enter the critical section, which realizes the effect of binary semaphore.
[0064] 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.
[0065] 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 method for constructing a binary semaphore based on a POSIX interface, characterized in that: The following steps are involved: Step 1, define the semaphore type identifier; Step 2: Modify the sem_open function to support the creation of binary semaphores; Step 3: Modify the sem_post function to ensure that the binary semaphore value does not exceed 1; Step 4: Modify the sem_init function to set the semaphore type.
2. A method for constructing a binary semaphore based on a POSIX interface according to claim 1, characterized in that: The step 1 comprises the following steps: Step 1-1: Add a semaphore type identification field to the semaphore structure. This field is used to clearly distinguish different types of semaphores. Step 1-2: define the semaphore type field newly added in step 1-1 as a constant to distinguish different semaphore categories.
3. A method for constructing a binary semaphore based on a POSIX interface according to claim 2, characterized in that: The step 2 comprises the following steps: Step 2-1, define additional parameters. When the sem_open function creates a semaphore, two additional parameters are introduced: a semaphore type parameter and a semaphore initial value parameter. The value range of the semaphore type parameter conforms to the semaphore type defined in step 1-2, and the initial value of the binary semaphore is limited to 0 or 1. Step 2-2, when further creating a semaphore, obtain additional parameters, extract the semaphore type and initial value from the passed in parameters, and store them in memory; Step 2-3, check the additional parameters. If the type parameter is not within the constant value range defined in step 1-2, an error is triggered and the function execution is terminated. If the semaphore type parameter is a binary parameter type and the semaphore initial value parameter value is not 0 or 1, the semaphore initial value is set to 1. Step 2-4: Initialize the semaphore structure, set the semaphore type and initial value according to the obtained parameters, and create the semaphore.
4. The method for constructing a binary semaphore based on a POSIX interface according to claim 1, characterized in that: The step 3 comprises the following steps: Step 3-1, identify the semaphore type, and extract its type information from the target semaphore structure; if the extracted semaphore type is a binary semaphore, execute step 3-2; if the semaphore type is not a binary semaphore, terminate the function execution; Step 3-2: If the semaphore type extracted in step 3-1 is a binary semaphore, check whether the value of the semaphore is 1; if it is 1, update the structure data; if not, set an error code.
5. The method for constructing a binary semaphore based on a POSIX interface according to claim 1, characterized in that: The step 4 is specifically as follows: setting the semaphore type identification field newly added in the step 1-1 to a counting semaphore, so as to realize the function of default initialization to a counting semaphore and prevent problems caused by unassigned variables.
6. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the program, the method according to any one of claims 1 to 5 is implemented.
7. A non-transitory computer-readable storage medium, characterized in that: The non-transitory computer-readable storage medium stores computer instructions, and the computer instructions are used to cause the computer to execute any one of claims 1 to 5.
8. 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 execute the method according to any one of claims 1 to 5.